@asymmetrik/elastic-querybuilder
Advanced tools
Comparing version 0.9.3 to 0.9.4
@@ -103,5 +103,27 @@ const QueryBuilder = require('../src/index'); | ||
boost: 1.2, | ||
must: [ | ||
{ match: { name: 'Kenny' }}, | ||
{ match: { alias: 'Mysterion' }} | ||
] | ||
} | ||
} | ||
}); | ||
}); | ||
test('should place should filters inside a filter query', () => { | ||
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, | ||
filter: { | ||
bool: { | ||
must: [ | ||
should: [ | ||
{ match: { name: 'Kenny' }}, | ||
@@ -108,0 +130,0 @@ { match: { alias: 'Mysterion' }} |
{ | ||
"name": "@asymmetrik/elastic-querybuilder", | ||
"version": "0.9.3", | ||
"version": "0.9.4", | ||
"description": "A query builder for Elasticsearch.", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -161,8 +161,7 @@ const { | ||
/** | ||
* @description Do we have a boolean query that should be filtered | ||
* @description Do we have a should query that should be filtered | ||
* @return {boolean} | ||
*/ | ||
isBoolean () { | ||
const query = prepareBoolQuery(this._queries); | ||
return !!(query.bool && Object.getOwnPropertyNames(query.bool).length); | ||
hasShould () { | ||
return this._queries.some((query) => query.type === BOOL.SHOULD); | ||
} | ||
@@ -169,0 +168,0 @@ |
@@ -54,3 +54,3 @@ const BaseBuilder = require('./BaseBuilder'); | ||
raw (path, value) { | ||
invariant(path && value && arguments.length === 2, ERRORS.RAW); | ||
invariant(path && value !== undefined && arguments.length === 2, ERRORS.RAW); | ||
this._raw.push({ path, value }); | ||
@@ -68,3 +68,6 @@ return this; | ||
build (options = {}) { | ||
const path = this.isBoolean() ? 'query.bool.filter' : 'query'; | ||
// If should is combined with any other boolean query, it will only affect the score | ||
// and the query will only match the other bool options. See the 'should' section | ||
// here: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html | ||
const path = this.hasShould() ? 'query.bool.filter' : 'query'; | ||
applyRawParameter(this._query, path, super.build()); | ||
@@ -91,3 +94,3 @@ // Add filtered aggregations if we have any | ||
this._raw.forEach((param) => applyRawParameter(this._query, param.path, param.value)); | ||
return this._query; | ||
return Object.assign({}, this._query); | ||
} | ||
@@ -121,3 +124,3 @@ | ||
this._raw.forEach((param) => applyRawParameter(this._query, param.path, param.value)); | ||
return this._query; | ||
return Object.assign({}, this._query); | ||
} | ||
@@ -157,3 +160,3 @@ | ||
this._raw.forEach((param) => applyRawParameter(this._query, param.path, param.value)); | ||
return this._query; | ||
return Object.assign({}, this._query); | ||
} | ||
@@ -187,3 +190,3 @@ | ||
this._raw.forEach((param) => applyRawParameter(this._query, param.path, param.value)); | ||
return this._query; | ||
return Object.assign({}, this._query); | ||
} | ||
@@ -190,0 +193,0 @@ |
153467
1958