/
nested-query.js
53 lines (46 loc) · 1.48 KB
/
nested-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
'use strict';
const isNil = require('lodash.isnil');
const JoiningQueryBase = require('./joining-query-base');
const ES_REF_URL =
'https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-nested-query.html';
/**
* Nested query allows to query nested objects. The query is executed against
* the nested objects / docs as if they were indexed as separate docs
* (they are, internally) and resulting in the root parent doc (or parent nested mapping).
*
* [Elasticsearch reference](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-nested-query.html)
*
* @example
* const qry = esb.nestedQuery()
* .path('obj1')
* .scoreMode('avg')
* .query(
* esb.boolQuery().must([
* esb.matchQuery('obj1.name', 'blue'),
* esb.rangeQuery('obj1.count').gt(5)
* ])
* );
*
* @param {Query=} qry A valid `Query` object
* @param {string=} path The nested object path.
*
* @extends JoiningQueryBase
*/
class NestedQuery extends JoiningQueryBase {
// eslint-disable-next-line require-jsdoc
constructor(qry, path) {
super('nested', ES_REF_URL, qry);
if (!isNil(path)) this._queryOpts.path = path;
}
/**
* Sets the root context for the nested query.
*
* @param {string} path
* @returns {NestedQuery} returns `this` so that calls can be chained.
*/
path(path) {
this._queryOpts.path = path;
return this;
}
}
module.exports = NestedQuery;