Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added support for minimum_should_match override param #244

Merged
merged 1 commit into from
Oct 25, 2019
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 bodybuilder.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ declare namespace bodybuilder {
options: object,
subfilters: QuerySubFilterFn
): B;
queryMinimumShouldMatch(param: string | number): B;
queryMinimumShouldMatch(param: string | number, override?: boolean): B;
getQuery(): object;
hasQuery(): boolean;
}
Expand Down Expand Up @@ -241,7 +241,7 @@ declare namespace bodybuilder {
options: object,
subfilters: FilterSubFilterFn
): B;
filterMinimumShouldMatch(param: string | number): B;
filterMinimumShouldMatch(param: string | number, override?: boolean): B;
getFilter(): object;
hasFilter(): boolean;
}
Expand Down
41 changes: 30 additions & 11 deletions package-lock.json

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

8 changes: 5 additions & 3 deletions src/filter-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ export default function filterBuilder (options) {
filters
)

function addMinimumShouldMatch(str) {
function addMinimumShouldMatch(str, override) {
filters.minimum_should_match = str
filters.minimum_should_match_override = override
}

return {
Expand Down Expand Up @@ -90,10 +91,11 @@ export default function filterBuilder (options) {
*
* @param {any} param minimum_should_match parameter. For possible values
* see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-minimum-should-match.html
* @param {boolean} override allows minimum_should_match to be overridden, ignoring internal constraints
* @return {bodybuilder} Builder.
*/
filterMinimumShouldMatch (param) {
addMinimumShouldMatch(param)
filterMinimumShouldMatch (param, override) {
addMinimumShouldMatch(param, !!override)
return this
},

Expand Down
8 changes: 5 additions & 3 deletions src/query-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ export default function queryBuilder (options) {

const makeQuery = pushQuery.bind(options || {}, query)

function addMinimumShouldMatch(str) {
function addMinimumShouldMatch(str, override = false) {
query.minimum_should_match = str
query.minimum_should_match_override = override
}

return {
Expand Down Expand Up @@ -106,10 +107,11 @@ export default function queryBuilder (options) {
*
* @param {any} param minimum_should_match parameter. For possible values
* see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-minimum-should-match.html
* @param {boolean} override allows minimum_should_match to be overridden, ignoring internal constraints
* @return {bodybuilder} Builder.
*/
queryMinimumShouldMatch (param) {
addMinimumShouldMatch(param)
queryMinimumShouldMatch (param, override) {
addMinimumShouldMatch(param, !!override)
return this
},

Expand Down
4 changes: 2 additions & 2 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ export function toBool (filters) {
cleaned.must_not = filters.not
}
if (
unwrapped.minimum_should_match &&
filters.or.length > 1
(unwrapped.minimum_should_match &&
filters.or.length > 1) || filters.minimum_should_match_override
) {
cleaned.minimum_should_match = unwrapped.minimum_should_match
}
Expand Down
49 changes: 49 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,55 @@ test('bodybuilder | minimum_should_match filter', (t) => {
})
})

test('bodybuilder | minimum_should_match with 1 filter', (t) => {
t.plan(1)

const result = bodyBuilder()
.orFilter('term', 'user', 'kimchy')
.filterMinimumShouldMatch(2)
.build()

t.deepEqual(result,
{
query: {
bool: {
filter: {
bool: {
should: [
{term: {user: 'kimchy'}}
]
}
}
}
}
})
})

test('bodybuilder | minimum_should_match with 1 filter + override', (t) => {
t.plan(1)

const result = bodyBuilder()
.orFilter('term', 'user', 'kimchy')
.filterMinimumShouldMatch(1, true)
.build()

t.deepEqual(result,
{
query: {
bool: {
filter: {
bool: {
should: [
{term: {user: 'kimchy'}}
],
minimum_should_match: 1
}
}
}
}
})
})

test('bodybuilder | minimum_should_match query', (t) => {
t.plan(1)

Expand Down
6 changes: 6 additions & 0 deletions typing-tests/bodybuilder-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,12 @@ bodybuilder()
.filterMinimumShouldMatch(1)
.queryMinimumShouldMatch(2)
.build();
bodybuilder()
.orQuery('term', 'user', 'kimchy')
.orFilter('term', 'user', 'kimchy')
.filterMinimumShouldMatch(1, true)
.queryMinimumShouldMatch(2, true)
.build();
bodybuilder()
.query('bool', b => b.orQuery('match', 'title', 'Solr').orQuery('match', 'title', 'Elasticsearch'))
.query('match', 'authors', 'clinton gormely')
Expand Down