Skip to content

Commit

Permalink
fix(query) add minimum_should_match = 1 to the "should" query (#8)
Browse files Browse the repository at this point in the history
The problem appeared when the `$or` filter was used
alongside other filters (outside the `$or` group).
Expected behaviour is that at least one of the filters
in the `$or` group AND all the filters
outside the `$or` group should be met.
Elasticsearch by default returns correct results when there is only
the `$or` (Es: should) filter. However, when there are other filters
present, should seems to be ignored.

Setting `minimum_should_match = 1` fixes the problem.
  • Loading branch information
jciolek authored and daffl committed Mar 19, 2017
1 parent f3246a6 commit e6e333f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ export function parseQuery (query, idProp) {
.filter(parsed => !!parsed)
.map(parsed => ({ bool: parsed }))
);
result.minimum_should_match = 1;

return result;
}
Expand Down
17 changes: 17 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,23 @@ describe('Elasticsearch Service', () => {
expect(results[0].name).to.equal('Bob');
});
});

it('can $or correctly with other filters', () => {
return app.service(serviceName)
.find({
query: {
$or: [
{ name: 'Moody' },
{ name: 'Douglas' }
],
bio: { $match: 'JavaScript legend' }
}
})
.then(result => {
expect(result.length).to.equal(1);
expect(result[0].name).to.equal('Douglas');
});
});
});
});

Expand Down
4 changes: 3 additions & 1 deletion test/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,8 @@ describe('Elasticsearch utils', () => {
]
}
}
]
],
minimum_should_match: 1
};
expect(parseQuery(query, '_id')).to
.deep.equal(expectedResult);
Expand Down Expand Up @@ -602,6 +603,7 @@ describe('Elasticsearch utils', () => {
}
}
],
minimum_should_match: 1,
filter: [
{ terms: { age: [ 12, 13 ] } },
{ term: { user: 'Obi Wan' } }
Expand Down

0 comments on commit e6e333f

Please sign in to comment.