-
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.
feat: Add VariableWidthHistogramAggregation (#172)
variable_width_histogram aggregation was added to Elasticsearch in v7.9.0.
- Loading branch information
1 parent
e3e7710
commit 713bdd2
Showing
7 changed files
with
139 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,7 +64,10 @@ | |
"webpack-cli": "^3.0.8" | ||
}, | ||
"release": { | ||
"branches": ["master", "next"] | ||
"branches": [ | ||
"master", | ||
"next" | ||
] | ||
}, | ||
"lint-staged": { | ||
"src/**/*.js": [ | ||
|
@@ -99,6 +102,8 @@ | |
"author": "Suhas Karanth <[email protected]>", | ||
"contributors": [ | ||
"austin ce <[email protected]>", | ||
"ochan12 <[email protected]>" | ||
"ochan12 <[email protected]>", | ||
"kennylindahl <[email protected]>", | ||
"foxstarius <[email protected]>" | ||
] | ||
} |
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
45 changes: 45 additions & 0 deletions
45
src/aggregations/bucket-aggregations/variable-width-histogram-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,45 @@ | ||
'use strict'; | ||
|
||
const isNil = require('lodash.isnil'); | ||
|
||
const BucketAggregationBase = require('./bucket-aggregation-base'); | ||
|
||
/** | ||
* This is a multi-bucket aggregation similar to Histogram. | ||
* However, the width of each bucket is not specified. | ||
* Rather, a target number of buckets is provided and bucket intervals are dynamically determined based on the document distribution. | ||
* This is done using a simple one-pass document clustering algorithm that aims to obtain low distances between bucket centroids. | ||
* Unlike other multi-bucket aggregations, the intervals will not necessarily have a uniform width. | ||
* | ||
* [Elasticsearch reference](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-variablewidthhistogram-aggregation.html) | ||
* | ||
* NOTE: Only available in Elasticsearch v7.9.0+ | ||
* @example | ||
* const agg = esb.variableWidthHistogramAggregation('price', 'lowestPrice', 10) | ||
* | ||
* @param {string} name The name which will be used to refer to this aggregation. | ||
* @param {string} [field] The field to aggregate on | ||
* @param {number} [buckets] Bucket count to generate histogram over. | ||
* | ||
* @extends BucketAggregationBase | ||
*/ | ||
class VariableWidthHistogramAggregation extends BucketAggregationBase { | ||
// eslint-disable-next-line require-jsdoc | ||
constructor(name, field, buckets) { | ||
super(name, 'variable_width_histogram', field); | ||
if (!isNil(buckets)) this._aggsDef.buckets = buckets; | ||
} | ||
|
||
/** | ||
* Sets the histogram bucket count. Buckets are generated based on this value. | ||
* | ||
* @param {number} buckets Bucket count to generate histogram over. | ||
* @returns {VariableWidthHistogramAggregation} returns `this` so that calls can be chained | ||
*/ | ||
buckets(buckets) { | ||
this._aggsDef.buckets = buckets; | ||
return this; | ||
} | ||
} | ||
|
||
module.exports = VariableWidthHistogramAggregation; |
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
42 changes: 42 additions & 0 deletions
42
test/aggregations-test/variable-width-histogram-aggregation.test.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,42 @@ | ||
import test from 'ava'; | ||
import { VariableWidthHistogramAggregation } from '../../src'; | ||
import { setsAggType } from '../_macros'; | ||
|
||
test( | ||
setsAggType, | ||
VariableWidthHistogramAggregation, | ||
'variable_width_histogram' | ||
); | ||
|
||
test('constructor sets arguments', t => { | ||
const value = new VariableWidthHistogramAggregation( | ||
'price', | ||
'lowestPrice', | ||
10 | ||
).toJSON(), | ||
expected = { | ||
price: { | ||
variable_width_histogram: { field: 'lowestPrice', buckets: 10 } | ||
} | ||
}; | ||
t.deepEqual(value, expected); | ||
}); | ||
|
||
test('buckets is set', t => { | ||
const value = new VariableWidthHistogramAggregation( | ||
'price', | ||
'lowestPrice', | ||
10 | ||
) | ||
.buckets(20) | ||
.toJSON(); | ||
const expected = { | ||
price: { | ||
variable_width_histogram: { | ||
field: 'lowestPrice', | ||
buckets: 20 | ||
} | ||
} | ||
}; | ||
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