From 459f676103d57e6668890d60395b96513d646064 Mon Sep 17 00:00:00 2001 From: Mateusz Rzepa Date: Mon, 16 Nov 2026 17:28:43 +0100 Subject: [PATCH 1/2] Fix filters with values missing from the index - AND filter whose first value is not indexed was ignored, so results depended on the order of values (fixes #155) - filters combined with not_filters always returned no items - ids/_ids not present in the index came back as undefined items - runtime facet values with spaces or colons matched nothing --- src/facets.js | 6 ++--- src/lib.js | 3 ++- src/utils/config.js | 19 ++++++++++++-- src/utils/facetsCore.js | 25 +++++++++--------- tests/searchSpec.js | 56 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 91 insertions(+), 18 deletions(-) diff --git a/src/facets.js b/src/facets.js index 12b397c..c7a5a01 100644 --- a/src/facets.js +++ b/src/facets.js @@ -75,9 +75,9 @@ export class Facets { } internal_ids_from_ids_map(ids) { - return ids.map((v) => { - return this.ids_map[v]; - }); + return ids + .map((v) => this.ids_map[v]) + .filter((v) => v !== undefined); } index() { diff --git a/src/lib.js b/src/lib.js index f6cbc3f..b170fab 100644 --- a/src/lib.js +++ b/src/lib.js @@ -74,7 +74,8 @@ export function search(items, input, configuration, fulltext, facets) { facets_time = new Date().getTime() - facets_time; if (query_ids) { - filtered_indexes_bitmap = query_ids; + // ids missing from the index would come back as undefined items + filtered_indexes_bitmap = filtered_indexes_bitmap.new_intersection(query_ids); } if (facet_result.ids) { diff --git a/src/utils/config.js b/src/utils/config.js index b1e7613..fedfffd 100644 --- a/src/utils/config.js +++ b/src/utils/config.js @@ -48,6 +48,21 @@ export const input_to_facet_filters = function (input, config) { return filters; }; +// 'tags:a' => ['tags', 'a'], 'city:"New York"' => ['city', 'New York'] +const parse_term = function (term) { + const i = term.indexOf(':'); + if (i === -1) { + return [term]; + } + + let value = term.slice(i + 1); + if (value.length > 1 && value.startsWith('"') && value.endsWith('"')) { + value = value.slice(1, -1).replace(/\\"/g, '"'); + } + + return [term.slice(0, i), value]; +}; + export const parse_boolean_query = function (query) { const result = booleanParser.parseBooleanQuery(query); @@ -59,11 +74,11 @@ export const parse_boolean_query = function (query) { return v3; }); } else { - return v2.split(':'); + return parse_term(v2); } }); } else { - return v1.split(':'); + return parse_term(v1); } }); }; diff --git a/src/utils/facetsCore.js b/src/utils/facetsCore.js index 1d95cd8..bec0515 100644 --- a/src/utils/facetsCore.js +++ b/src/utils/facetsCore.js @@ -103,22 +103,23 @@ export const matrix = function (facets, filters = []) { temp_facet.is_temp_copied = true; - let conjunctive_index; + let conjunctive_index = null; const disjunctive_indexes = combination_indexes(facets, filters); filters.forEach((filter) => { - if (!Array.isArray(filter[0])) { - const [filter_key, filter_val] = filter; - const facetData = temp_facet.bits_data_temp[filter_key]?.[filter_val]; - - if (conjunctive_index && facetData) { - conjunctive_index = facetData.new_intersection(conjunctive_index); - } else if (conjunctive_index && !facetData) { - conjunctive_index = new FastBitSet([]); - } else { - conjunctive_index = facetData; - } + // only [key, value]; OR groups and [key, '-', value] are handled below + if (Array.isArray(filter[0]) || filter.length !== 2) { + return; } + + const [filter_key, filter_val] = filter; + // a value missing from the index matches nothing + const facetData = + temp_facet.bits_data_temp[filter_key]?.[filter_val] || new FastBitSet(); + + conjunctive_index = conjunctive_index + ? conjunctive_index.new_intersection(facetData) + : facetData; }); if (conjunctive_index) { diff --git a/tests/searchSpec.js b/tests/searchSpec.js index b9f8e58..1ab3f84 100644 --- a/tests/searchSpec.js +++ b/tests/searchSpec.js @@ -608,3 +608,59 @@ describe('custom fulltext integration', function () { done(); }); }); + +describe('filter values missing from the index', function () { + const products = [ + { id: 1, name: 'Shirt', tags: ['cotton'], color: 'red', city: 'New York' }, + { id: 2, name: 'Hat', tags: ['wool'], color: 'blue', city: 'Paris' }, + { id: 3, name: 'Scarf', tags: ['cotton', 'wool'], color: 'red', city: 'Paris' }, + ]; + + const itemsjs = itemsJS(products, { + aggregations: { + tags: { conjunction: true }, + color: { conjunction: true }, + city: { conjunction: false }, + }, + }); + + const names = (result) => result.data.items.map((item) => item.name); + + it('returns nothing for a missing AND value, whatever its position (#155)', function () { + for (const tags of [['silk'], ['silk', 'cotton'], ['cotton', 'silk']]) { + const result = itemsjs.search({ filters: { tags } }); + + assert.deepEqual(names(result), [], JSON.stringify(tags)); + assert.deepEqual( + result.data.aggregations.tags.buckets.map((b) => b.doc_count), + [0, 0], + JSON.stringify(tags) + ); + } + + const result = itemsjs.search({ filters: { tags: ['silk'], color: ['red'] } }); + assert.deepEqual(names(result), []); + }); + + it('combines filters with not_filters', function () { + const result = itemsjs.search({ + filters: { tags: ['cotton'] }, + not_filters: { tags: ['wool'] }, + }); + + assert.deepEqual(names(result), ['Shirt']); + }); + + it('skips ids that are not in the index', function () { + assert.deepEqual(names(itemsjs.search({ ids: [1, 999] })), ['Shirt']); + assert.deepEqual(names(itemsjs.search({ _ids: [999] })), []); + }); + + it('matches runtime facet values with spaces', function () { + const result = itemsjs.search({ + facets: { city: { selected: ['New York'] } }, + }); + + assert.deepEqual(names(result), ['Shirt']); + }); +}); From 2d5621890804424743ab801c2fb0645dfc8bdde2 Mon Sep 17 00:00:00 2001 From: Mateusz Rzepa Date: Mon, 16 Nov 2026 17:34:40 +0100 Subject: [PATCH 2/2] Bump 2.4.5 --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 2a60100..81a862e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "itemsjs", - "version": "2.4.4", + "version": "2.4.5", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "itemsjs", - "version": "2.4.4", + "version": "2.4.5", "license": "Apache-2.0", "devDependencies": { "boolean-parser": "^0.0.2", diff --git a/package.json b/package.json index 7e4b186..40b1721 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "itemsjs", - "version": "2.4.4", + "version": "2.4.5", "description": "Created to perform fast search on small json dataset (up to 1000 elements).", "type": "module", "scripts": {