@attivio/suit
Version:
Attivio SUIT, the Search UI Toolkit, is a library for creating search clients for searching the Attivio platform.
267 lines (243 loc) • 11.8 kB
JavaScript
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
import SimpleQueryRequest from './SimpleQueryRequest';
import QueryResponse from './QueryResponse';
import AuthUtils from '../util/AuthUtils';
import FetchUtils from '../util/FetchUtils';
import ObjectUtils from '../util/ObjectUtils';
import QueryRequestToElastic from '../util/QueryRequestToElastic';
import QueryRequestToSolr from '../util/QueryRequestToSolr';
/**
* 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 = AuthUtils.getLoggedInUserId();
if (username && username.length > 0) {
request.username = AuthUtils.getLoggedInUserId();
}
var uri = this.baseUri + '/rest/searchApi/search';
var jsonRequest = Object.assign({}, request);
jsonRequest.restParams = ObjectUtils.strMapToObj(request.restParams);
if (this.searchEngineType === 'elastic') {
QueryRequestToElastic.convert(jsonRequest, '' + this.baseUri, this.customOptions, function (err, searchResponse) {
if (err) {
updateResults(null, err);
}
updateResults(searchResponse, null);
});
} else if (this.searchEngineType === 'solr') {
QueryRequestToSolr.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 ? QueryResponse.fromJson(response) : null;
updateResults(searchResponse, error);
};
FetchUtils.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 SimpleQueryRequest();
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;
}();
export { Search as default };