UNPKG

@attivio/suit

Version:

Attivio SUIT, the Search UI Toolkit, is a library for creating search clients for searching the Attivio platform.

294 lines (255 loc) 12.6 kB
'use strict'; exports.__esModule = true; exports.default = undefined; var _SimpleQueryRequest = require('./SimpleQueryRequest'); var _SimpleQueryRequest2 = _interopRequireDefault(_SimpleQueryRequest); var _QueryResponse = require('./QueryResponse'); var _QueryResponse2 = _interopRequireDefault(_QueryResponse); var _AuthUtils = require('../util/AuthUtils'); var _AuthUtils2 = _interopRequireDefault(_AuthUtils); var _FetchUtils = require('../util/FetchUtils'); var _FetchUtils2 = _interopRequireDefault(_FetchUtils); var _ObjectUtils = require('../util/ObjectUtils'); var _ObjectUtils2 = _interopRequireDefault(_ObjectUtils); var _QueryRequestToElastic = require('../util/QueryRequestToElastic'); var _QueryRequestToElastic2 = _interopRequireDefault(_QueryRequestToElastic); var _QueryRequestToSolr = require('../util/QueryRequestToSolr'); var _QueryRequestToSolr2 = _interopRequireDefault(_QueryRequestToSolr); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } /** * Encapsulates the default Attivio search behavior. */ var Search = function () { /** * Construct a Search object. * * @param baseUri the base URI for the Attivio instance to call when searching * (including the protocol, hostname or IP address, and port number, * with no trailing slash) */ function Search(baseUri, searchEngineType, customOptions) { _classCallCheck(this, Search); this.baseUri = baseUri; this.searchEngineType = searchEngineType; this.customOptions = customOptions; } Search.prototype.search = function search(request, updateResults) { if (!request.restParams || request.restParams.size === 0) { request.restParams = new Map([['join.rollup', ['TREE']], ['includeMetadataInResponse', ['true']], ['geo.field', ['position']], ['geo.units', ['DEGREES']], ['l.stopwords.mode', ['OFF']], ['l.acronyms.mode', ['OFF']], ['l.acronymBoost', ['25']], ['l.synonyms.mode', ['OFF']], ['l.synonyms.boost', ['25']]]); } // Do the search on behalf of the logged-in user. // If the user is authenticated using the servlet, // this will be replaced with that username. var username = _AuthUtils2.default.getLoggedInUserId(); if (username && username.length > 0) { request.username = _AuthUtils2.default.getLoggedInUserId(); } var uri = this.baseUri + '/rest/searchApi/search'; var jsonRequest = Object.assign({}, request); jsonRequest.restParams = _ObjectUtils2.default.strMapToObj(request.restParams); if (this.searchEngineType === 'elastic') { _QueryRequestToElastic2.default.convert(jsonRequest, '' + this.baseUri, this.customOptions, function (err, searchResponse) { if (err) { updateResults(null, err); } updateResults(searchResponse, null); }); } else if (this.searchEngineType === 'solr') { _QueryRequestToSolr2.default.convert(jsonRequest, '' + this.baseUri, this.customOptions, function (err, searchResponse) { if (err) { updateResults(null, err); } updateResults(searchResponse, null); }); } else { var callback = function callback(response, error) { var searchResponse = response ? _QueryResponse2.default.fromJson(response) : null; updateResults(searchResponse, error); }; _FetchUtils2.default.fetch(uri, jsonRequest, callback, 'POST', 'An error occurred while searching.'); } }; /** * Perform a search against the Attivio index. * * @param query the query to perform * @param queryLanguage the language to use, either "simple" or "advanced" * @param offset the index of the first document to return * @param count the number of documents to return (e.g. page size) * @param updateResults will be called when the search is complete with the results or an error */ Search.prototype.simpleSearch = function simpleSearch(query, queryLanguage, offset, count, updateResults) { var request = new _SimpleQueryRequest2.default(); request.rows = count; request.query = query; request.queryLanguage = queryLanguage; this.search(request, updateResults); }; Search.prototype.updateRealtimeField = function updateRealtimeField(docId, fieldName, fieldValues, onCompletion, onError) { var _this = this; return new Promise(function (resolve, reject) { // Get session var connectUri = _this.baseUri + '/rest/ingestApi/connect'; fetch(connectUri, { credentials: 'include' }).then(function (connectResult) { connectResult.json().then(function (json) { var sessionId = json; var updateUri = _this.baseUri + '/rest/ingestApi/updateRealTimeField/' + sessionId; var headers = new Headers({ Accept: 'application/json', 'Content-Type': 'application/json' }); var jsonRequest = { id: docId, fieldName: fieldName, values: fieldValues }; var body = JSON.stringify(jsonRequest); var params = { method: 'POST', headers: headers, body: body, credentials: 'include' }; var updateFetchRequest = new Request(updateUri, params); fetch(updateFetchRequest).then(function (updateResult) { if (updateResult.ok) { onCompletion(); // Now need to refresh the update var refreshUri = _this.baseUri + '/rest/ingestApi/refresh/' + sessionId; fetch(refreshUri, { credentials: 'include' }).then(function (refreshResult) { if (refreshResult.ok) { // Now need to close the session var disconnectUri = _this.baseUri + '/rest/ingestApi/disconnect/' + sessionId; fetch(disconnectUri, { credentials: 'include' }).then(function (disconnectResult) { if (disconnectResult.ok) { resolve(); } else { // The request came back other than a 200-type response code disconnectResult.text().then(function (msg) { reject(new Error('Error disconnecting from the ingest API: ' + msg)); }).catch(function () { onError('Error disconnecting from the ingest API: ' + disconnectResult.statusText); reject(new Error('Error disconnecting from the ingest API: ' + disconnectResult.statusText)); }); } }).catch(function (error) { onError(error); reject(new Error('Failed to disconnect from the ingest API: ' + error)); }); } else { // The request came back other than a 200-type response code refreshResult.text().then(function (msg) { reject(new Error('Failed to refresh the update: ' + msg)); }).catch(function () { onError('Failed to refresh the update: ' + refreshResult.statusText); reject(new Error('Failed to refresh the update: ' + refreshResult.statusText)); }); } }).catch(function (error) { onError(error); reject(new Error('Failed to refresh the update: ' + error)); }); } else { // The request came back other than a 200-type response code updateResult.text().then(function (msg) { reject(new Error('Failed to update the field: ' + msg)); }).catch(function (error) { onError('Failed to update the field: ' + error); reject(new Error('Failed to update the field: ' + error)); }); } }).catch(function (error) { // Catch network-type errors from the updating fetch() call onError('Failed to update the field: ' + error); reject(new Error('Failed to update the field: ' + error)); }); }).catch(function (error) { onError('Failed to connect to the ingest API: ' + error); reject(new Error('Failed to connect to the ingest API: ' + error)); }); }).catch(function (error) { onError('Failed to connect to the ingest API: ' + error); reject(new Error('Failed to connect to the ingest API: ' + error)); }); }); }; Search.prototype.addOrDeleteDocument = function addOrDeleteDocument(jsonRequest, callback) { var _this2 = this; return new Promise(function (resolve, reject) { // Get session var connectUri = _this2.baseUri + '/rest/ingestApi/connect'; fetch(connectUri, { credentials: 'include' }).then(function (connectResult) { connectResult.json().then(function (json) { var sessionId = json; var updateUri = _this2.baseUri + '/rest/ingestApi/feedDocuments/' + sessionId; var headers = new Headers({ Accept: 'application/json', 'Content-Type': 'application/json' }); var body = JSON.stringify(jsonRequest); var params = { method: 'POST', headers: headers, body: body, credentials: 'include' }; var updateFetchRequest = new Request(updateUri, params); fetch(updateFetchRequest).then(function (updateResult) { if (updateResult.ok) { // Now need to refresh the update var refreshUri = _this2.baseUri + '/rest/ingestApi/refresh/' + sessionId; fetch(refreshUri, { credentials: 'include' }).then(function (refreshResult) { if (refreshResult.ok) { // Now need to close the session var disconnectUri = _this2.baseUri + '/rest/ingestApi/disconnect/' + sessionId; fetch(disconnectUri, { credentials: 'include' }).then(function (disconnectResult) { if (disconnectResult.ok) { callback(); resolve(); } else { // The request came back other than a 200-type response code disconnectResult.text().then(function (msg) { reject(new Error('Error disconnecting from the ingest API: ' + msg)); }).catch(function () { reject(new Error('Error disconnecting from the ingest API: ' + disconnectResult.statusText)); }); } }).catch(function (error) { reject(new Error('Failed to disconnect from the ingest API: ' + error)); }); } else { // The request came back other than a 200-type response code refreshResult.text().then(function (msg) { reject(new Error('Failed to refresh the update: ' + msg)); }).catch(function () { reject(new Error('Failed to refresh the update: ' + refreshResult.statusText)); }); } }).catch(function (error) { reject(new Error('Failed to refresh the update: ' + error)); }); } else { // The request came back other than a 200-type response code updateResult.text().then(function (msg) { reject(new Error('Failed to update the field: ' + msg)); }).catch(function (error) { reject(new Error('Failed to update the field: ' + error)); }); } }).catch(function (error) { // Catch network-type errors from the updating fetch() call reject(new Error('Failed to update the field: ' + error)); }); }).catch(function (error) { reject(new Error('Failed to connect to the ingest API: ' + error)); }); }).catch(function (error) { reject(new Error('Failed to connect to the ingest API: ' + error)); }); }); }; return Search; }(); exports.default = Search; module.exports = exports['default'];