Skip to content

Commit d0a63a6

Browse files
authored
fix error when filter value not found in items set (#130)
* fix error when filter value not found in items set * added conjunction false test * add test for single non existing value
1 parent faa006e commit d0a63a6

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

src/helpers.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ const combination_indexes = function(facets, filters) {
3939
const filter_val = disjunctive_filter[1];
4040

4141
filter_keys.push(filter_key);
42-
facet_union = facet_union.new_union(facets['bits_data'][filter_key][filter_val]);
42+
facet_union = facet_union.new_union(facets['bits_data'][filter_key][filter_val] || new FastBitSet([]));
4343
indexes[filter_key] = facet_union;
4444
});
4545
}
@@ -311,7 +311,7 @@ const facets_ids = function(facets_data, filters) {
311311
filters.forEach(filter => {
312312

313313
++i;
314-
output = output.new_union(facets_data[field][filter]);
314+
output = output.new_union(facets_data[field][filter] || new FastBitSet([]));
315315
});
316316
});
317317

tests/searchSpec.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
const assert = require('assert');
4+
const { clone } = require('lodash');
45
const items = require('./fixtures/items.json');
56
const movies = require('./fixtures/movies.json');
67
let itemsjs = require('./../src/index')();
@@ -195,6 +196,60 @@ describe('search', function() {
195196
done();
196197
});
197198

199+
it('makes search with non existing filter value with conjunction true should return no results', function test(done) {
200+
201+
const itemsjs = require('./../index')(items, configuration);
202+
203+
const result = itemsjs.search({
204+
filters: {
205+
category: ['drama', 'thriller']
206+
}
207+
});
208+
209+
assert.equal(result.data.items.length, 0);
210+
assert.equal(result.data.aggregations.tags.buckets[0].doc_count, 0);
211+
212+
done();
213+
});
214+
215+
it('makes search with non existing filter value with conjunction false should return results', function test(done) {
216+
217+
const localConfiguration = clone(configuration);
218+
localConfiguration.aggregations.category.conjunction = false;
219+
220+
const itemsjs = require('./../index')(items, localConfiguration);
221+
222+
const result = itemsjs.search({
223+
filters: {
224+
category: ['drama', 'thriller']
225+
}
226+
});
227+
228+
assert.equal(result.data.items.length, 2);
229+
assert.equal(result.data.aggregations.tags.buckets[0].doc_count, 2);
230+
231+
done();
232+
});
233+
234+
it('makes search with non existing single filter value with conjunction false should return no results', function test(done) {
235+
236+
const localConfiguration = clone(configuration);
237+
localConfiguration.aggregations.category.conjunction = false;
238+
239+
const itemsjs = require('./../index')(items, configuration);
240+
241+
const result = itemsjs.search({
242+
filters: {
243+
category: ['thriller']
244+
}
245+
});
246+
247+
assert.equal(result.data.items.length, 0);
248+
assert.equal(result.data.aggregations.tags.buckets[0].doc_count, 0);
249+
250+
done();
251+
});
252+
198253
it('throws an error if name does not exist', function test(done) {
199254

200255
const itemsjs = require('./../index')(items, {

0 commit comments

Comments
 (0)