/
children-aggregation.js
74 lines (67 loc) · 2.28 KB
/
children-aggregation.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
'use strict';
const BucketAggregationBase = require('./bucket-aggregation-base');
const ES_REF_URL =
'https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-children-aggregation.html';
/**
* A special single bucket aggregation that enables aggregating
* from buckets on parent document types to buckets on child documents.
*
* This aggregation relies on the `_parent` field in the mapping.
*
* [Elasticsearch reference](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-children-aggregation.html)
*
* @example
* const reqBody = esb.requestBodySearch()
* .agg(
* esb.termsAggregation('top-tags', 'tags.keyword')
* .size(10)
* .agg(
* esb.childrenAggregation('to-answers')
* .type('answer')
* .agg(
* esb.termsAggregation(
* 'top-names',
* 'owner.display_name.keyword'
* ).size(10)
* )
* )
* )
* .size(0);
*
* @param {string} name The name which will be used to refer to this aggregation.
*
* @extends BucketAggregationBase
*/
class ChildrenAggregation extends BucketAggregationBase {
// eslint-disable-next-line require-jsdoc
constructor(name) {
super(name, 'children');
}
/**
* @override
* @throws {Error} This method cannot be called on ChildrenAggregation
*/
field() {
console.log(`Please refer ${ES_REF_URL}`);
throw new Error('field is not supported in ChildrenAggregation');
}
/**
* @override
* @throws {Error} This method cannot be called on ChildrenAggregation
*/
script() {
console.log(`Please refer ${ES_REF_URL}`);
throw new Error('script is not supported in ChildrenAggregation');
}
/**
* Sets the child type/mapping for aggregation.
*
* @param {string} type The child type that the buckets in the parent space should be mapped to.
* @returns {ChildrenAggregation} returns `this` so that calls can be chained
*/
type(type) {
this._aggsDef.type = type;
return this;
}
}
module.exports = ChildrenAggregation;