Skip to content

Commit 11bc8e5

Browse files
authored
optional custom id field for lunr2 (and other) integrations (#116)
1 parent fc9c381 commit 11bc8e5

File tree

10 files changed

+75
-24
lines changed

10 files changed

+75
-24
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ If native full text search is not enough then you can integrate with external fu
153153

154154
How it works:
155155

156-
- each item of your data needs to have `id` field
156+
- each item of your data needs to have `id` field. It can be also custom field but it needs to be defined.
157157
- `native_search_enabled` option in configuration should be disabled
158158
- index data once in your search and itemsjs
159159
- make search in your custom search and provide `ids` data into itemsjs

dist/itemsjs.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20133,13 +20133,14 @@ var FastBitSet = require('fastbitset');
2013320133
*/
2013420134

2013520135

20136-
var Facets = function Facets(items, config) {
20136+
var Facets = function Facets(items, configuration) {
2013720137
var _this = this;
2013820138

20139-
config = config || {};
20139+
configuration = configuration || {};
20140+
configuration.aggregations = configuration.aggregations || {};
2014020141
this.items = items;
20141-
this.config = config;
20142-
this.facets = helpers.index(items, _.keys(config));
20142+
this.config = configuration.aggregations;
20143+
this.facets = helpers.index(items, _.keys(configuration.aggregations));
2014320144
this._items_map = {};
2014420145
this._ids = [];
2014520146
var i = 1;
@@ -20156,8 +20157,10 @@ var Facets = function Facets(items, config) {
2015620157

2015720158
if (items) {
2015820159
items.forEach(function (v) {
20159-
if (v.id && v._id) {
20160-
_this.ids_map[v.id] = v._id;
20160+
var custom_id_field = configuration.custom_id_field || 'id';
20161+
20162+
if (v[custom_id_field] && v._id) {
20163+
_this.ids_map[v[custom_id_field]] = v._id;
2016120164
}
2016220165
});
2016320166
}
@@ -20849,7 +20852,7 @@ module.exports = function itemsjs(items, configuration) {
2084920852
} // index facets
2085020853

2085120854

20852-
var facets = new Facets(items, configuration.aggregations);
20855+
var facets = new Facets(items, configuration);
2085320856
return {
2085420857
/**
2085520858
* per_page
@@ -20894,7 +20897,7 @@ module.exports = function itemsjs(items, configuration) {
2089420897
reindex: function reindex(newItems) {
2089520898
items = newItems;
2089620899
fulltext = new Fulltext(items, configuration);
20897-
facets = new Facets(items, configuration.aggregations);
20900+
facets = new Facets(items, configuration);
2089820901
}
2089920902
};
2090020903
};

dist/itemsjs.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/lunr2-integration.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ const data = [{
3232
// configuration for itemsjs faceted search
3333
const configuration = {
3434
native_search_enabled: false,
35+
custom_id_field: 'id', // 'id' is a default one but we can also use 'uuid' and other if necessary
3536
aggregations: {
3637
category: {
3738
title: 'Categories',

docs/minisearch-integration.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ miniSearch.addAll(data);
4040
// configuration for itemsjs faceted search
4141
const configuration = {
4242
native_search_enabled: false,
43+
custom_id_field: 'id', // 'id' is a default one but we can also use 'uuid' and other if necessary
4344
aggregations: {
4445
category: {
4546
title: 'Categories',

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "itemsjs",
3-
"version": "2.1.18",
3+
"version": "2.1.19",
44
"description": "Created to perform fast search on small json dataset (up to 1000 elements).",
55
"main": "lib/index.js",
66
"scripts": {

src/facets.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ const FastBitSet = require('fastbitset');
55
/**
66
* responsible for making faceted search
77
*/
8-
const Facets = function(items, config) {
8+
const Facets = function(items, configuration) {
99

10-
config = config || {};
10+
configuration = configuration || {};
11+
configuration.aggregations = configuration.aggregations || {};
1112
this.items = items;
12-
this.config = config;
13-
this.facets = helpers.index(items, _.keys(config));
13+
this.config = configuration.aggregations;
14+
this.facets = helpers.index(items, _.keys(configuration.aggregations));
1415

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

2829
if (items) {
2930
items.forEach(v => {
30-
if (v.id && v._id) {
31-
this.ids_map[v.id] = v._id;
31+
32+
const custom_id_field = configuration.custom_id_field || 'id';
33+
if (v[custom_id_field] && v._id) {
34+
this.ids_map[v[custom_id_field]] = v._id;
3235
}
3336
});
3437
}

src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ module.exports = function itemsjs(items, configuration) {
1717
}
1818

1919
// index facets
20-
let facets = new Facets(items, configuration.aggregations);
20+
let facets = new Facets(items, configuration);
2121

2222
return {
2323
/**
@@ -65,7 +65,7 @@ module.exports = function itemsjs(items, configuration) {
6565
reindex: function(newItems) {
6666
items = newItems;
6767
fulltext = new Fulltext(items, configuration);
68-
facets = new Facets(items, configuration.aggregations);
68+
facets = new Facets(items, configuration);
6969
}
7070
};
7171
};

tests/facetsSpec.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ describe('conjunctive search', function() {
5353
}
5454
};
5555

56-
const facets = new Facets(items, aggregations);
56+
const facets = new Facets(items, {
57+
aggregations: aggregations
58+
});
5759
const itemsjs = require('./../index')(items, {
5860
aggregations: aggregations
5961
});
@@ -240,7 +242,9 @@ describe('disjunctive search', function() {
240242
}
241243
};
242244

243-
const facets = new Facets(items, aggregations);
245+
const facets = new Facets(items, {
246+
aggregations: aggregations
247+
});
244248

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

@@ -305,7 +309,9 @@ describe('disjunctive and conjunctive search', function() {
305309
}
306310
};
307311

308-
const facets = new Facets(items, aggregations);
312+
const facets = new Facets(items, {
313+
aggregations: aggregations
314+
});
309315

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

@@ -371,7 +377,9 @@ describe('generates facets crossed with query', function() {
371377
}
372378
};
373379

374-
const facets = new Facets(items, aggregations);
380+
const facets = new Facets(items, {
381+
aggregations: aggregations
382+
});
375383
const itemsjs = require('./../index')(items, {
376384
aggregations: aggregations,
377385
searchableFields: ['actors'],
@@ -449,7 +457,9 @@ describe('generates symetrical disjunctive facets (SergeyRe)', function() {
449457
{ a: 2, b: 4 }
450458
];
451459

452-
const facets = new Facets(items, aggregations);
460+
const facets = new Facets(items, {
461+
aggregations: aggregations
462+
});
453463
const itemsjs = require('./../index')(items, {
454464
aggregations: aggregations,
455465
});

tests/searchSpec.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,4 +316,37 @@ describe('custom fulltext integration', function() {
316316
assert.equal(result.data.items.length, 2);
317317
done();
318318
});
319+
320+
it('makes faceted search after separated quasi fulltext with custom id field', function test(done) {
321+
322+
let i = 10;
323+
const temp_movies = movies.map(v => {
324+
325+
v.uuid = i;
326+
i += 10;
327+
delete v.id;
328+
return v;
329+
});
330+
331+
configuration.custom_id_field = 'uuid';
332+
333+
itemsjs = require('./../index')(temp_movies, configuration);
334+
335+
let result = itemsjs.search({
336+
ids: temp_movies.map(v => v.uuid).slice(0, 1),
337+
});
338+
339+
assert.equal(result.data.items[0].uuid, 10);
340+
assert.equal(result.data.items[0]._id, 1);
341+
assert.equal(result.data.items.length, 1);
342+
343+
result = itemsjs.search({
344+
ids: [50, 20]
345+
});
346+
347+
assert.equal(result.data.items[0].uuid, 50);
348+
assert.equal(result.data.items[0]._id, 5);
349+
assert.equal(result.data.items.length, 2);
350+
done();
351+
});
319352
});

0 commit comments

Comments
 (0)