From 83dda1373b715cb26e19cbf8a9c9f77a6b4777ad Mon Sep 17 00:00:00 2001 From: Suhas Karanth Date: Wed, 14 Jun 2017 11:03:33 +0530 Subject: [PATCH] feat: Add class AdjacencyMatrixAggregation --- .../adjacency-matrix-aggregation.js | 106 ++++++++++++++++++ src/aggregations/bucket-aggregations/index.js | 1 + src/index.js | 4 + 3 files changed, 111 insertions(+) create mode 100644 src/aggregations/bucket-aggregations/adjacency-matrix-aggregation.js diff --git a/src/aggregations/bucket-aggregations/adjacency-matrix-aggregation.js b/src/aggregations/bucket-aggregations/adjacency-matrix-aggregation.js new file mode 100644 index 00000000..2e7f6b36 --- /dev/null +++ b/src/aggregations/bucket-aggregations/adjacency-matrix-aggregation.js @@ -0,0 +1,106 @@ +'use strict'; + +const has = require('lodash.has'); + +const { Query, util: { checkType } } = require('../../core'); + +const BucketAggregationBase = require('./bucket-aggregation-base'); + +const ES_REF_URL = + 'https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-adjacency-matrix-aggregation.html'; + +/** + * A bucket aggregation returning a form of adjacency matrix. + * The request provides a collection of named filter expressions, + * similar to the `filters` aggregation request. Each bucket in the response + * represents a non-empty cell in the matrix of intersecting filters. + * + * [Elasticsearch reference](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-adjacency-matrix-aggregation.html) + * + * @example + * const agg = bob.adjacencyMatrixAggregation('interactions').filters({ + * grpA: bob.termsQuery('accounts', ['hillary', 'sidney']), + * grpB: bob.termsQuery('accounts', ['donald', 'mitt']), + * grpC: bob.termsQuery('accounts', ['vladimir', 'nigel']) + * }); + * + * @param {string} name The name which will be used to refer to this aggregation. + * + * @extends BucketAggregationBase + */ +class AdjacencyMatrixAggregation extends BucketAggregationBase { + // eslint-disable-next-line require-jsdoc + constructor(name) { + super(name, 'adjacency_matrix'); + } + + /** + * @override + * @throws {Error} This method cannot be called on AdjacencyMatrixAggregation + */ + field() { + console.log(`Please refer ${ES_REF_URL}`); + throw new Error('field is not supported in AdjacencyMatrixAggregation'); + } + + /** + * @override + * @throws {Error} This method cannot be called on AdjacencyMatrixAggregation + */ + script() { + console.log(`Please refer ${ES_REF_URL}`); + throw new Error('script is not supported in AdjacencyMatrixAggregation'); + } + + /** + * Sets a named filter query. + * + * @param {string} filterName Name for the filter. + * @param {Query} filterQuery Query to filter on. Example - term query. + * @returns {AdjacencyMatrixAggregation} returns `this` so that calls can be chained + * @throws {TypeError} If `filterQuery` is not an instance of `Query` + */ + filter(filterName, filterQuery) { + checkType(filterQuery, Query); + + if (!has(this._aggsDef, 'filters')) this._aggsDef.filters = {}; + + this._aggsDef.filters[filterName] = filterQuery; + return this; + } + + /** + * Assigns filters to already added filters. + * Does not mix with anonymous filters. + * If anonymous filters are present, they will be overwritten. + * + * @param {Object} filterQueries Object with multiple key value pairs + * where filter name is the key and filter query is the value. + * @returns {AdjacencyMatrixAggregation} returns `this` so that calls can be chained + * @throws {TypeError} If `filterQueries` is not an instance of object + */ + filters(filterQueries) { + checkType(filterQueries, Object); + + if (!has(this._aggsDef, 'filters')) this._aggsDef.filters = {}; + + Object.assign(this._aggsDef.filters, filterQueries); + return this; + } + + /** + * Sets the `separator` parameter to use a separator string other than + * the default of the ampersand. + * + * @param {string} sep the string used to separate keys in intersections buckets + * e.g. & character for keyed filters A and B would return an + * intersection bucket named A&B + * @returns {AdjacencyMatrixAggregation} returns `this` so that calls can be chained + */ + separator(sep) { + this._aggsDef.separator = sep; + return this; + } +} + +module.exports = AdjacencyMatrixAggregation; diff --git a/src/aggregations/bucket-aggregations/index.js b/src/aggregations/bucket-aggregations/index.js index 5b2c0bdd..9ed194b4 100644 --- a/src/aggregations/bucket-aggregations/index.js +++ b/src/aggregations/bucket-aggregations/index.js @@ -5,6 +5,7 @@ exports.HistogramAggregationBase = require('./histogram-aggregation-base'); exports.RangeAggregationBase = require('./range-aggregation-base'); exports.TermsAggregationBase = require('./terms-aggregation-base'); +exports.AdjacencyMatrixAggregation = require('./adjacency-matrix-aggregation'); exports.ChildrenAggregation = require('./children-aggregation'); exports.DateHistogramAggregation = require('./date-histogram-aggregation'); exports.DateRangeAggregation = require('./date-range-aggregation'); diff --git a/src/index.js b/src/index.js index e04b69e6..912d401d 100644 --- a/src/index.js +++ b/src/index.js @@ -87,6 +87,7 @@ const { ValueCountAggregation }, bucketAggregations: { + AdjacencyMatrixAggregation, ChildrenAggregation, DateHistogramAggregation, DateRangeAggregation, @@ -332,6 +333,9 @@ exports.valueCountAggregation = constructorWrapper(ValueCountAggregation); /* ============ ============ ============ */ /* ========= Bucket Aggregations ======== */ /* ============ ============ ============ */ +exports.AdjacencyMatrixAggregation = AdjacencyMatrixAggregation; +exports.adjacencyMatrixAggregation = constructorWrapper(AdjacencyMatrixAggregation); + exports.ChildrenAggregation = ChildrenAggregation; exports.childrenAggregation = constructorWrapper(ChildrenAggregation);