Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Implement 'Bucket Sort Aggregation' #69

Merged
merged 2 commits into from
Sep 27, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/documentation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ toc:
- BucketScriptAggregation
- BucketSelectorAggregation
- SerialDifferencingAggregation
- BucketSortAggregation
- name: Matrix Aggregations
- MatrixStatsAggregation
- name: Score Functions
Expand Down
115 changes: 115 additions & 0 deletions src/aggregations/pipeline-aggregations/bucket-sort-aggregation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
'use strict';

const PipelineAggregationBase = require('./pipeline-aggregation-base');

const ES_REF_URL =
'https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-bucket-sort-aggregation.html';

/**
* A parent pipeline aggregation which sorts the buckets of its parent
* multi-bucket aggregation. Zero or more sort fields may be specified
* together with the corresponding sort order. Each bucket may be sorted
* based on its _key, _count or its sub-aggregations. In addition, parameters
* from and size may be set in order to truncate the result buckets.
*
* [Elasticsearch reference](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-bucket-sort-aggregation.html)
*
* @example
* const reqBody = esb.requestBodySearch()
* .agg(
* esb.bucketSortAggregation('sort')
* .sort([
* esb.sort('user', 'desc')
* ])
* .from(5)
* .size(10)
* )
* );
*
* @param {string} name The name which will be used to refer to this aggregation.
*
* @extends PipelineAggregationBase
*/
class BucketSortAggregation extends PipelineAggregationBase {
// eslint-disable-next-line require-jsdoc
constructor(name) {
super(name, 'bucket_sort', ES_REF_URL);
}

/**
* Sets the list of fields to sort on. Optional.
*
* [Elasticsearch reference](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-bucket-sort-aggregation.html#id-1.9.5.55.4.6)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it is necessary to repeat the URL for reference and example in every method's docs. Could you please remove them?

*
* @example
* const reqBody = esb.requestBodySearch()
* .agg(
* esb.bucketSortAggregation('sort')
* .sort([
* esb.sort('user', 'desc')
* ])
* .from(5)
* .size(10)
* )
* );
*
* @param {Array<Sort>} sort The list of fields to sort on
* @returns {BucketSortAggregation} returns `this` so that calls can be chained
*/
sort(sort) {
this._aggsDef.sort = sort;
return this;
}

/**
* Sets the value buckets in positions prior to which will be truncated. Optional.
*
* [Elasticsearch reference](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-bucket-sort-aggregation.html#id-1.9.5.55.4.6)
*
* @example
* const reqBody = esb.requestBodySearch()
* .agg(
* esb.bucketSortAggregation('sort')
* .sort([
* esb.sort('user', 'desc')
* ])
* .from(5)
* .size(10)
* )
* );
*
* @param {number} from Buckets in positions prior to the set value will be truncated.
* @returns {BucketSortAggregation} returns `this` so that calls can be chained
*/
from(from) {
this._aggsDef.from = from;
return this;
}

/**
* Sets the number of buckets to return. Optional.
*
* [Elasticsearch reference](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-bucket-sort-aggregation.html#id-1.9.5.55.4.6)
*
* @example
* const reqBody = esb.requestBodySearch()
* .agg(
* esb.bucketSortAggregation('sort')
* .sort([
* esb.sort('user', 'desc')
* ])
* .from(5)
* .size(10)
* )
* );
*
* @param {number} size The number of buckets to return.
* @returns {BucketSortAggregation} returns `this` so that calls can be chained
*/
size(size) {
this._aggsDef.size = size;
return this;
}
}

module.exports = BucketSortAggregation;
1 change: 1 addition & 0 deletions src/aggregations/pipeline-aggregations/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ exports.CumulativeSumAggregation = require('./cumulative-sum-aggregation');
exports.BucketScriptAggregation = require('./bucket-script-aggregation');
exports.BucketSelectorAggregation = require('./bucket-selector-aggregation');
exports.SerialDifferencingAggregation = require('./serial-differencing-aggregation');
exports.BucketSortAggregation = require('./bucket-sort-aggregation');
48 changes: 48 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6516,6 +6516,54 @@ declare namespace esb {
bucketsPath?: string
): BucketSelectorAggregation;

/**
* A parent pipeline aggregation which sorts the buckets of its parent
* multi-bucket aggregation. Zero or more sort fields may be specified
* together with the corresponding sort order. Each bucket may be sorted
* based on its _key, _count or its sub-aggregations. In addition, parameters
* from and size may be set in order to truncate the result buckets.
*
* @param {string} name The name which will be used to refer to this aggregation.
* @extends PipelineAggregationBase
*/
export class BucketSortAggregation extends PipelineAggregationBase {
constructor(name: string);

/**
* Sets the list of fields to sort on.
*
* @param {Array<Sort>} sort The list of fields to sort on
*/
sort(sort: Array<Sort>): this;

/**
* Sets the value buckets in positions prior to which will be truncated.
*
* @param {number} from Buckets in positions prior to the set value will be truncated.
*/
from(from: number): this;

/**
* Sets the number of buckets to return.
*
* @param {number} size The number of buckets to return.
*/
size(size: number): this;
}

/**
* A parent pipeline aggregation which sorts the buckets of its parent
* multi-bucket aggregation. Zero or more sort fields may be specified
* together with the corresponding sort order. Each bucket may be sorted
* based on its _key, _count or its sub-aggregations. In addition, parameters
* from and size may be set in order to truncate the result buckets.
*
* @param {string} name The name which will be used to refer to this aggregation.
*/
export function bucketSortAggregation(
name: string
): BucketSortAggregation;

/**
* Serial differencing is a technique where values in a time series are
* subtracted from itself at different time lags or periods.
Expand Down
6 changes: 5 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ const {
CumulativeSumAggregation,
BucketScriptAggregation,
BucketSelectorAggregation,
SerialDifferencingAggregation
SerialDifferencingAggregation,
BucketSortAggregation
},
matrixAggregations: { MatrixStatsAggregation }
} = require('./aggregations');
Expand Down Expand Up @@ -447,6 +448,9 @@ exports.maxBucketAggregation = constructorWrapper(MaxBucketAggregation);
exports.MinBucketAggregation = MinBucketAggregation;
exports.minBucketAggregation = constructorWrapper(MinBucketAggregation);

exports.BucketSortAggregation = BucketSortAggregation;
exports.bucketSortAggregation = constructorWrapper(BucketSortAggregation);

exports.SumBucketAggregation = SumBucketAggregation;
exports.sumBucketAggregation = constructorWrapper(SumBucketAggregation);

Expand Down
40 changes: 40 additions & 0 deletions test/aggregations-test/bucket-sort-agg.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import test from 'ava';
import { BucketSortAggregation, Sort } from '../../src';
import { setsAggType } from '../_macros';

const getInstance = () => new BucketSortAggregation('my_agg');

test(setsAggType, BucketSortAggregation, 'bucket_sort');

test('can be instantiated', t => {
const value = getInstance().toJSON();
const expected = {
my_agg: {
bucket_sort: {}
}
};
t.deepEqual(value, expected);
});

test('sort from and size are set', t => {
const value = getInstance()
.sort([new Sort('myField', 'desc')])
.from(5)
.size(10)
.toJSON();

const expected = {
my_agg: {
bucket_sort: {
sort: [
{
myField: 'desc'
}
],
from: 5,
size: 10
}
}
};
t.deepEqual(value, expected);
});
3 changes: 3 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,9 @@ test('aggregations are exported', t => {
t.truthy(esb.BucketSelectorAggregation);
t.truthy(esb.bucketSelectorAggregation);

t.truthy(esb.BucketSortAggregation);
t.truthy(esb.bucketSortAggregation);

t.truthy(esb.SerialDifferencingAggregation);
t.truthy(esb.serialDifferencingAggregation);

Expand Down