Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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.4.4",
"version": "2.4.5",
"description": "Created to perform fast search on small json dataset (up to 1000 elements).",
"type": "module",
"scripts": {
Expand Down
6 changes: 3 additions & 3 deletions src/facets.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
3 changes: 2 additions & 1 deletion src/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
19 changes: 17 additions & 2 deletions src/utils/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);
}
});
};
Expand Down
25 changes: 13 additions & 12 deletions src/utils/facetsCore.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
56 changes: 56 additions & 0 deletions tests/searchSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
});
});
Loading