@asymmetrik/elastic-querybuilder
Advanced tools
Comparing version 0.9.6 to 0.9.7
@@ -112,5 +112,6 @@ const QueryBuilder = require('../src/index'); | ||
test('should place should filters inside a filter query', () => { | ||
test('should place should filters inside a filter query if there is a must', () => { | ||
const query = new QueryBuilder() | ||
.raw('query.bool.boost', 1.2) | ||
.must('match', 'city', 'South Park') | ||
.should('match', 'name', 'Kenny') | ||
@@ -128,2 +129,5 @@ .should('match', 'alias', 'Mysterion') | ||
bool: { | ||
must: { | ||
match: { city: 'South Park' } | ||
}, | ||
should: [ | ||
@@ -140,2 +144,24 @@ { match: { name: 'Kenny' }}, | ||
test('should place should filters at the top level if there are no other queries', () => { | ||
const query = new QueryBuilder() | ||
.raw('query.bool.boost', 1.2) | ||
.should('match', 'name', 'Kenny') | ||
.should('match', 'alias', 'Mysterion') | ||
.build(); | ||
expect(query).toEqual({ | ||
from: 0, | ||
size: 15, | ||
query: { | ||
bool: { | ||
boost: 1.2, | ||
should: [ | ||
{ match: { name: 'Kenny' }}, | ||
{ match: { alias: 'Mysterion' }} | ||
] | ||
} | ||
} | ||
}); | ||
}); | ||
test('should be able to build a compound boolean query', () => { | ||
@@ -142,0 +168,0 @@ const query = new QueryBuilder() |
{ | ||
"name": "@asymmetrik/elastic-querybuilder", | ||
"version": "0.9.6", | ||
"version": "0.9.7", | ||
"description": "A query builder for Elasticsearch.", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -164,4 +164,8 @@ const { | ||
*/ | ||
hasShould () { | ||
return this._queries.some((query) => query.type === BOOL.SHOULD); | ||
shouldUseFilter () { | ||
const types = this._queries.reduce((all, query) => { | ||
if (all.indexOf(query.type) === -1) { all.push(query.type); } | ||
return all; | ||
}, []); | ||
return types.indexOf(BOOL.SHOULD) > -1 && types.length > 1; | ||
} | ||
@@ -168,0 +172,0 @@ |
@@ -70,3 +70,3 @@ const BaseBuilder = require('./BaseBuilder'); | ||
// here: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html | ||
const path = this.hasShould() ? 'query.bool.filter' : 'query'; | ||
const path = this.shouldUseFilter() ? 'query.bool.filter' : 'query'; | ||
applyRawParameter(this._query, path, super.build()); | ||
@@ -73,0 +73,0 @@ // Add filtered aggregations if we have any |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
155811
2041