Skip to content

Commit

Permalink
optional custom id field for lunr2 (and other) integrations (#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
cigolpl committed Jul 29, 2022
1 parent fc9c381 commit 11bc8e5
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 24 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ If native full text search is not enough then you can integrate with external fu

How it works:

- each item of your data needs to have `id` field
- each item of your data needs to have `id` field. It can be also custom field but it needs to be defined.
- `native_search_enabled` option in configuration should be disabled
- index data once in your search and itemsjs
- make search in your custom search and provide `ids` data into itemsjs
Expand Down
19 changes: 11 additions & 8 deletions dist/itemsjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -20133,13 +20133,14 @@ var FastBitSet = require('fastbitset');
*/


var Facets = function Facets(items, config) {
var Facets = function Facets(items, configuration) {
var _this = this;

config = config || {};
configuration = configuration || {};
configuration.aggregations = configuration.aggregations || {};
this.items = items;
this.config = config;
this.facets = helpers.index(items, _.keys(config));
this.config = configuration.aggregations;
this.facets = helpers.index(items, _.keys(configuration.aggregations));
this._items_map = {};
this._ids = [];
var i = 1;
Expand All @@ -20156,8 +20157,10 @@ var Facets = function Facets(items, config) {

if (items) {
items.forEach(function (v) {
if (v.id && v._id) {
_this.ids_map[v.id] = v._id;
var custom_id_field = configuration.custom_id_field || 'id';

if (v[custom_id_field] && v._id) {
_this.ids_map[v[custom_id_field]] = v._id;
}
});
}
Expand Down Expand Up @@ -20849,7 +20852,7 @@ module.exports = function itemsjs(items, configuration) {
} // index facets


var facets = new Facets(items, configuration.aggregations);
var facets = new Facets(items, configuration);
return {
/**
* per_page
Expand Down Expand Up @@ -20894,7 +20897,7 @@ module.exports = function itemsjs(items, configuration) {
reindex: function reindex(newItems) {
items = newItems;
fulltext = new Fulltext(items, configuration);
facets = new Facets(items, configuration.aggregations);
facets = new Facets(items, configuration);
}
};
};
Expand Down
2 changes: 1 addition & 1 deletion dist/itemsjs.min.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions docs/lunr2-integration.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const data = [{
// configuration for itemsjs faceted search
const configuration = {
native_search_enabled: false,
custom_id_field: 'id', // 'id' is a default one but we can also use 'uuid' and other if necessary
aggregations: {
category: {
title: 'Categories',
Expand Down
1 change: 1 addition & 0 deletions docs/minisearch-integration.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ miniSearch.addAll(data);
// configuration for itemsjs faceted search
const configuration = {
native_search_enabled: false,
custom_id_field: 'id', // 'id' is a default one but we can also use 'uuid' and other if necessary
aggregations: {
category: {
title: 'Categories',
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "itemsjs",
"version": "2.1.18",
"version": "2.1.19",
"description": "Created to perform fast search on small json dataset (up to 1000 elements).",
"main": "lib/index.js",
"scripts": {
Expand Down
15 changes: 9 additions & 6 deletions src/facets.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ const FastBitSet = require('fastbitset');
/**
* responsible for making faceted search
*/
const Facets = function(items, config) {
const Facets = function(items, configuration) {

config = config || {};
configuration = configuration || {};
configuration.aggregations = configuration.aggregations || {};
this.items = items;
this.config = config;
this.facets = helpers.index(items, _.keys(config));
this.config = configuration.aggregations;
this.facets = helpers.index(items, _.keys(configuration.aggregations));

this._items_map = {};
this._ids = [];
Expand All @@ -27,8 +28,10 @@ const Facets = function(items, config) {

if (items) {
items.forEach(v => {
if (v.id && v._id) {
this.ids_map[v.id] = v._id;

const custom_id_field = configuration.custom_id_field || 'id';
if (v[custom_id_field] && v._id) {
this.ids_map[v[custom_id_field]] = v._id;
}
});
}
Expand Down
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module.exports = function itemsjs(items, configuration) {
}

// index facets
let facets = new Facets(items, configuration.aggregations);
let facets = new Facets(items, configuration);

return {
/**
Expand Down Expand Up @@ -65,7 +65,7 @@ module.exports = function itemsjs(items, configuration) {
reindex: function(newItems) {
items = newItems;
fulltext = new Fulltext(items, configuration);
facets = new Facets(items, configuration.aggregations);
facets = new Facets(items, configuration);
}
};
};
20 changes: 15 additions & 5 deletions tests/facetsSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ describe('conjunctive search', function() {
}
};

const facets = new Facets(items, aggregations);
const facets = new Facets(items, {
aggregations: aggregations
});
const itemsjs = require('./../index')(items, {
aggregations: aggregations
});
Expand Down Expand Up @@ -240,7 +242,9 @@ describe('disjunctive search', function() {
}
};

const facets = new Facets(items, aggregations);
const facets = new Facets(items, {
aggregations: aggregations
});

it('returns facets', function test(done) {

Expand Down Expand Up @@ -305,7 +309,9 @@ describe('disjunctive and conjunctive search', function() {
}
};

const facets = new Facets(items, aggregations);
const facets = new Facets(items, {
aggregations: aggregations
});

it('returns facets', function test(done) {

Expand Down Expand Up @@ -371,7 +377,9 @@ describe('generates facets crossed with query', function() {
}
};

const facets = new Facets(items, aggregations);
const facets = new Facets(items, {
aggregations: aggregations
});
const itemsjs = require('./../index')(items, {
aggregations: aggregations,
searchableFields: ['actors'],
Expand Down Expand Up @@ -449,7 +457,9 @@ describe('generates symetrical disjunctive facets (SergeyRe)', function() {
{ a: 2, b: 4 }
];

const facets = new Facets(items, aggregations);
const facets = new Facets(items, {
aggregations: aggregations
});
const itemsjs = require('./../index')(items, {
aggregations: aggregations,
});
Expand Down
33 changes: 33 additions & 0 deletions tests/searchSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,4 +316,37 @@ describe('custom fulltext integration', function() {
assert.equal(result.data.items.length, 2);
done();
});

it('makes faceted search after separated quasi fulltext with custom id field', function test(done) {

let i = 10;
const temp_movies = movies.map(v => {

v.uuid = i;
i += 10;
delete v.id;
return v;
});

configuration.custom_id_field = 'uuid';

itemsjs = require('./../index')(temp_movies, configuration);

let result = itemsjs.search({
ids: temp_movies.map(v => v.uuid).slice(0, 1),
});

assert.equal(result.data.items[0].uuid, 10);
assert.equal(result.data.items[0]._id, 1);
assert.equal(result.data.items.length, 1);

result = itemsjs.search({
ids: [50, 20]
});

assert.equal(result.data.items[0].uuid, 50);
assert.equal(result.data.items[0]._id, 5);
assert.equal(result.data.items.length, 2);
done();
});
});

0 comments on commit 11bc8e5

Please sign in to comment.