Each array of processing units to execute defines a number of processing units (which may be one or more scripts) that will be executed in parallel. In the case of processing units that connect to search engines, each processing unit is expected to connect to a single search engine, and therefore specifies the engine id of the engine configuration to use. If the processing unit is not connecting to a search engine, the engine id can be omitted.
When the processing unit returns results, they are attached to a label. This allows different processing units to add results to different labels. Subsequent processing units will have access to these results (and the corresponding label) and will be able to process them further (for example merge them).
Thus each processing unit can be thought of as below:
processingUnit = { "label": "<label>", "engine": "<engine id>", "scripts": ArrayOfScriptsToExecute }
The array of scripts defines a number of scripts to execute in order. Any results will be attached to the label and available in the next script, allowing (in the case of search engine connections) a script to query the engine, and a subsequent script to perform a further query if the first returned no results. The scripts specify the name of the JavaScript file to execute and a configuration to be passed when the script is executed.
Thus, each script definition looks like:
script = { "script": "<JavaScript file>", "config": { } }
Processing unit scripts must conform to a particular format shown below:
/** * Copyright Search Technologies 2016 * Processing unit script template */ 'use strict'; /** * Add any requires/constants here */ /** * @param config the main (SEIA server) configuration * @param scriptConfig the script configuration from the */ module.exports = function(config, scriptConfig) { /** * Any instance initialisation here */ /** * @param engine the engine instance (if an engine was configured) * @param request the body of the incoming request to the endpoint * @param pResults the results from the previous parallel executions * @param qpl qpl from the previous script execution * @param results results from the previous script execution * @param callback the callback function to return results. The callback is of the form * callback(err, qpl, results) * @param groups the groups from group expansion (if configured) */ this.process = function(engine, request, pResults, qpl, results, callback, groups) { // Perform some processing .... // Perform the query at the engine, passing the callback so the results can be returned to the user engine.execute(request, function(err, results){ callback(err, qpl, results); }); } };
When the SEIA server is started up, an instance of script is created. The instantiation is passed to the configuration of the SEIA server and the configuration defined in the script block of the processing unit. At this time, any one-off initialization my be performed.
The shipped configuration for the search endpoint is configured as below:
"processing": [ [ { "label": "search", "engine": "elastic", "scripts": [ { "script": "scripts/search", "config": { "expansion" : { "thesaurus": "thesaurus", "stripPunct": false, "reload": 600000 }, "compositeSearchFields": { "grank1": 4.0, "grank2": 2.0, "content": 1.0, "title": 1.0, "url": 1.0, "description": 1.0 }, "security": { "aclField": "acls" } } } ] } ] ]
This causes a single call to the search.js script when the endpoint is called. The results are written under the search label.
The shipped script for the search endpoint is shown below:
/** * Copyright Search Technologies 2016 */ 'use strict'; var Qpl = require('qpl').Core, logger = require('../../../app/utilities/log.server.utilities').get('server'); module.exports = function(config, scriptConfig) { var thesaurus = require('qpl').MongoThesaurus.load(config.db.uri, scriptConfig.expansion.stripPunct, scriptConfig.expansion.thesaurus, scriptConfig.expansion.reload); this.process = function(engine, request, pResults, qpl, results, callback, groups) { var qry = request.q; logger.debug('Processing: ' + qry); if (groups !== undefined) logger.debug('Groups: ' + groups); // Handle an 'everything' query (including Solr style) if (Qpl.isEmpty(qry) || qry === '*' || qry === '*:*') { // Just query a wildcard of everything request.qpl = Qpl.wildcard('*'); } else { // A real query - tokenise it var tokens = Qpl.tokenize(qry); // Use the thesaurus var singleExpansions = thesaurus.expandSingle(tokens); var multipleExpansions = thesaurus.expandMultiple(tokens); // And create a composite var fields = scriptConfig.compositeSearchFields; request.qpl = Qpl.or([ Qpl.and(Qpl.compositeOr(fields, singleExpansions)), Qpl.and(Qpl.compositeOr(fields, multipleExpansions), 5) ]); } // Add the security if required if (groups !== undefined) request.securityFilter = Qpl.securityFilter(scriptConfig.security.aclField, groups); // Perform the query at the engine, passing the callback so the results can be returned to the user engine.execute(request, function(err, results){ callback(err, qpl, results); }); } };
The script requires QPL and, on initialization, loads a MongoThesaurus from QPL, using the Mongo URI passed in from the SEIA server configuration and the thesaurus parameters passed in from the script configuration.
When the endpoint is called, the process() function is called. This receives a reference to the engine, the body of the request to the endpoint, the qpl and results (both undefined) from previous scripts and the call. The query is extracted from the request, the desired QPL built and then the request is passed to the engine (via the execute() function). Once the engine completes, the callback() function is used to pass the results back to the caller.
An endpoint for federation might be configured as below:
{ "endpoint": "federated", "enabled": true, "description": "Default federated search endpoint", "groupExpansion": { "enabled": false, "url": "http://localhost:50505/groupExpansion?username=${user}&json=1" }, "processing": [ [ // Execute the searches in parallel { "label": "search", "engine": "elastic", "scripts": [ { "script": "../search/scripts/search", "config": { "expansion" : { "thesaurus": "thesaurus", "stripPunct": false, "reload": 600000 }, "compositeSearchFields": { "grank1": 4.0, "grank2": 2.0, "content": 1.0, "title": 1.0, "url": 1.0, "description": 1.0 }, "security": { "aclField": "acls" } } } ] }, { "label": "search1", "engine": "elastic1", "scripts": [ { "script": "../search/scripts/search", "config": { "expansion" : { "thesaurus": "thesaurus", "stripPunct": false, "reload": 600000 }, "compositeSearchFields": { "grank1": 4.0, "grank2": 2.0, "content": 1.0, "title": 1.0, "url": 1.0, "description": 1.0 }, "security": { "aclField": "acls" } } } ] } ], [ // Then merge the results { "label": "merged", "scripts": [ { "script": "scripts/merge" } ] } ] ] }
Located in its own directory, this configuration utilizes two (parallel) calls to two different engines (elastic and elastic1 as configured for the engine) using the search.js script from the simple example above.
An example results merging script is shown below:
/** * Copyright Search Technologies 2017 * Created by Steve Denny on 27/02/2017. * * Example federation merge script */ 'use strict'; module.exports = function(/*config, scriptConfig*/) { this.process = function(engine, request, pResults, qpl, results, callback /*, groups*/) { console.log(JSON.stringify(pResults, null, 2)); // Get the two sets of results from the previous queries var search = pResults.search; var search1 = pResults.search1; // Perform a very simple merge, interleaving the results. // TODO: A proper implementation should consider (at the very least) the sort order var merged = { offset: search.offset, len: Math.max(search.len, search1.len), count: search.count + search1.count, hits: [] }; // copy the hits to the merged data var i = 0; while (merged.hits.length < merged.len) { if (i<search.hits.length) merged.hits.push(search.hits[i]); if (i<search1.hits.length) merged.hits.push(search1.hits[i]); i++; } // Remove the unmerged data delete pResults.search; delete pResults.search1; // Send the results back callback(undefined /*error*/, qpl, merged); } };
The script retrieves the search results from the results from the previous script execution (pResults), via their assigned labels (search and search1).