-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Parent aggregation was added in Elasticsearch v6.6
- Loading branch information
Showing
7 changed files
with
160 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
src/aggregations/bucket-aggregations/parent-aggregation.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
'use strict'; | ||
|
||
const isNil = require('lodash.isnil'); | ||
|
||
const BucketAggregationBase = require('./bucket-aggregation-base'); | ||
|
||
const ES_REF_URL = | ||
'https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-parent-aggregation.html'; | ||
|
||
/** | ||
* A special single bucket aggregation that enables aggregating | ||
* from buckets on child document types to buckets on parent 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-parent-aggregation.html) | ||
* | ||
* @example | ||
* const reqBody = esb.requestBodySearch() | ||
* .agg( | ||
* esb.termsAggregation('top-names', 'owner.display_name.keyword') | ||
* .size(10) | ||
* .agg( | ||
* esb.parentAggregation('to-questions') | ||
* .type('answer') | ||
* .agg( | ||
* esb.termsAggregation( | ||
* 'top-tags', | ||
* 'tags.keyword' | ||
* ).size(10) | ||
* ) | ||
* ) | ||
* ) | ||
* .size(0); | ||
* | ||
* @param {string} name The name which will be used to refer to this aggregation. | ||
* @param {string=} type The type of the child document. | ||
* | ||
* @extends BucketAggregationBase | ||
*/ | ||
class ParentAggregation extends BucketAggregationBase { | ||
// eslint-disable-next-line require-jsdoc | ||
constructor(name, type) { | ||
super(name, 'parent'); | ||
|
||
if (!isNil(type)) this.type(type); | ||
} | ||
|
||
/** | ||
* @override | ||
* @throws {Error} This method cannot be called on ParentAggregation | ||
*/ | ||
field() { | ||
console.log(`Please refer ${ES_REF_URL}`); | ||
throw new Error('field is not supported in ParentAggregation'); | ||
} | ||
|
||
/** | ||
* @override | ||
* @throws {Error} This method cannot be called on ParentAggregation | ||
*/ | ||
script() { | ||
console.log(`Please refer ${ES_REF_URL}`); | ||
throw new Error('script is not supported in ParentAggregation'); | ||
} | ||
|
||
/** | ||
* 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 {ParentAggregation} returns `this` so that calls can be chained | ||
*/ | ||
type(type) { | ||
this._aggsDef.type = type; | ||
return this; | ||
} | ||
} | ||
|
||
module.exports = ParentAggregation; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import test from 'ava'; | ||
import { ParentAggregation } from '../../src'; | ||
import { illegalCall, setsAggType } from '../_macros'; | ||
|
||
test(setsAggType, ParentAggregation, 'parent'); | ||
test(illegalCall, ParentAggregation, 'field', 'my_agg'); | ||
test(illegalCall, ParentAggregation, 'script', 'my_agg'); | ||
|
||
test('constructor sets type', t => { | ||
const value = new ParentAggregation('to_questions', 'answer').toJSON(); | ||
const expected = { | ||
to_questions: { | ||
parent: { | ||
type: 'answer' | ||
} | ||
} | ||
}; | ||
t.deepEqual(value, expected); | ||
}); | ||
|
||
test('type is set', t => { | ||
const value = new ParentAggregation('to_questions').type('answer').toJSON(); | ||
const expected = { | ||
to_questions: { | ||
parent: { | ||
type: 'answer' | ||
} | ||
} | ||
}; | ||
t.deepEqual(value, expected); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters