/
boosting-query.js
81 lines (71 loc) · 2.33 KB
/
boosting-query.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
'use strict';
const isNil = require('lodash.isnil');
const {
Query,
util: { checkType }
} = require('../../core');
/**
* The boosting query can be used to effectively demote results that match
* a given query. Unlike the "NOT" clause in bool query, this still selects
* documents that contain undesirable terms, but reduces their overall
* score.
*
* [Elasticsearch reference](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-boosting-query.html)
*
* @example
* const qry = esb.boostingQuery(
* esb.termQuery('field1', 'value1'), // positiveQry
* esb.termQuery('field2', 'value2'), // negativeQry
* 0.2 // negativeBoost
* );
*
* @param {Query=} positiveQry A valid `Query` object.
* @param {Query=} negativeQry A valid `Query` object.
* @param {number=} negativeBoost A positive `double` value where `0 < n < 1`.
*
* @extends Query
*/
class BoostingQuery extends Query {
// eslint-disable-next-line require-jsdoc
constructor(positiveQry, negativeQry, negativeBoost) {
super('boosting');
if (!isNil(positiveQry)) this.positive(positiveQry);
if (!isNil(negativeQry)) this.negative(negativeQry);
if (!isNil(negativeBoost))
this._queryOpts.negative_boost = negativeBoost;
}
/**
* Sets the "master" query that determines which results are returned.
*
* @param {Query} query A valid `Query` object.
* @returns {BoostingQuery} returns `this` so that calls can be chained.
*/
positive(query) {
checkType(query, Query);
this._queryOpts.positive = query;
return this;
}
/**
* Sets the query used to match documents in the `positive`
* query that will be negatively boosted.
*
* @param {Query} query A valid `Query` object.
* @returns {BoostingQuery} returns `this` so that calls can be chained.
*/
negative(query) {
checkType(query, Query);
this._queryOpts.negative = query;
return this;
}
/**
* Sets the negative boost value.
*
* @param {number} factor A positive `double` value where `0 < n < 1`.
* @returns {BoostingQuery} returns `this` so that calls can be chained.
*/
negativeBoost(factor) {
this._queryOpts.negative_boost = factor;
return this;
}
}
module.exports = BoostingQuery;