itemsjs
Version:
Created to perform fast search on small json dataset (up to 1000 elements).
70 lines (57 loc) • 1.7 kB
JavaScript
var service = require('./lib');
var helpers = require('./helpers');
var Fulltext = require('./fulltext');
var Facets = require('./facets');
module.exports = function itemsjs(items, configuration) {
configuration = configuration || {}; // upsert id to items
// throw error in tests if id does not exists
var fulltext;
if (configuration.native_search_enabled !== false) {
fulltext = new Fulltext(items, configuration);
} // index facets
var facets = new Facets(items, configuration.aggregations);
return {
/**
* per_page
* page
* query
* sort
* filters
*/
search: function search(input) {
input = input || {};
/**
* merge configuration aggregation with user input
*/
input.aggregations = helpers.mergeAggregations(configuration.aggregations, input);
return service.search(items, input, configuration, fulltext, facets);
},
/**
* returns list of similar elements to specified item id
* id
*/
similar: function similar(id, options) {
return service.similar(items, id, options);
},
/**
* returns list of elements for specific aggregation i.e. list of tags
* name (aggregation name)
* query
* per_page
* page
*/
aggregation: function aggregation(input) {
return service.aggregation(items, input, configuration, fulltext, facets);
},
/**
* reindex items
* reinitialize fulltext search
*/
reindex: function reindex(newItems) {
items = newItems;
fulltext = new Fulltext(items, configuration);
facets = new Facets(items, configuration.aggregations);
}
};
};
;