UNPKG

@jmaitrehenry/elastic-builder

Version:

A JavaScript implementation of the elasticsearch Query DSL

170 lines (139 loc) 6.02 kB
'use strict'; var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); var _createClass2 = require('babel-runtime/helpers/createClass'); var _createClass3 = _interopRequireDefault(_createClass2); var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn'); var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2); var _inherits2 = require('babel-runtime/helpers/inherits'); var _inherits3 = _interopRequireDefault(_inherits2); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } var isNil = require('lodash.isnil'); var BucketAggregationBase = require('./bucket-aggregation-base'); var ES_REF_URL = 'https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-rare-terms-aggregation.html'; /** * A multi-bucket value source based aggregation which finds * "rare" terms — terms that are at the long-tail of the * distribution and are not frequent. Conceptually, this is like * a terms aggregation that is sorted by `_count` ascending. * As noted in the terms aggregation docs, actually ordering * a `terms` agg by count ascending has unbounded error. * Instead, you should use the `rare_terms` aggregation * * [Elasticsearch reference](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-rare-terms-aggregation.html) * * NOTE: Only available in Elasticsearch 7.3.0+. * * @example * const agg = esb.rareTermsAggregation('genres', 'genre'); * * @param {string} name The name which will be used to refer to this aggregation. * @param {string} field The field we wish to find rare terms in * * @extends BucketAggregationBase */ var RareTermsAggregation = function (_BucketAggregationBas) { (0, _inherits3.default)(RareTermsAggregation, _BucketAggregationBas); // eslint-disable-next-line require-jsdoc function RareTermsAggregation(name, field) { (0, _classCallCheck3.default)(this, RareTermsAggregation); return (0, _possibleConstructorReturn3.default)(this, (RareTermsAggregation.__proto__ || Object.getPrototypeOf(RareTermsAggregation)).call(this, name, 'rare_terms', field)); } /** * Sets the maximum number of documents a term should appear in. * * @example * const agg = esb.rareTermsAggregation('genres', 'genre').maxDocCount(2); * * @param {number} maxDocCnt Integer value for maximum number of documents a term should appear in. * Max doc count can be between 1 and 100. * @returns {RareTermsAggregation} returns `this` so that calls can be chained */ (0, _createClass3.default)(RareTermsAggregation, [{ key: 'maxDocCount', value: function maxDocCount(maxDocCnt) { if (isNil(maxDocCnt) || maxDocCnt < 1 || maxDocCnt > 100) { throw new Error('`maxDocCount` can only be value from 1 to 100.'); } this._aggsDef.max_doc_count = maxDocCnt; return this; } /** * Sets the precision of the internal CuckooFilters. Smaller precision * leads to better approximation, but higher memory usage. * Cannot be smaller than 0.00001 * * @example * const agg = esb.rareTermsAggregation('genres', 'genre').precision(0.001); * * @param {number} precision Float value for precision of the internal CuckooFilters. Default is 0.01 * @returns {RareTermsAggregation} returns `this` so that calls can be chained */ }, { key: 'precision', value: function precision(_precision) { if (_precision < 0.00001) { throw new Error('`precision` must be greater than 0.00001.'); } this._aggsDef.precision = _precision; return this; } /** * Sets terms that should be included in the aggregation * * @example * const agg = esb.rareTermsAggregation('genres', 'genre').include('swi*'); * * @param {string} include Regular expression that will determine what values * are "allowed" to be aggregated * @returns {RareTermsAggregation} returns `this` so that calls can be chained */ }, { key: 'include', value: function include(_include) { this._aggsDef.include = _include; return this; } /** * Sets terms that should be excluded from the aggregation * * @example * const agg = esb.rareTermsAggregation('genres', 'genre').exclude('electro*'); * * @param {string} exclude Regular expression that will determine what values * should not be aggregated * @returns {RareTermsAggregation} returns `this` so that calls can be chained */ }, { key: 'exclude', value: function exclude(_exclude) { this._aggsDef.exclude = _exclude; return this; } /** * Sets the missing parameter which defines how documents * that are missing a value should be treated. * * @param {string} value * @returns {RareTermsAggregation} returns `this` so that calls can be chained */ }, { key: 'missing', value: function missing(value) { this._aggsDef.missing = value; return this; } /** * @override * @throws {Error} This method cannot be called on RareTermsAggregation */ }, { key: 'script', value: function script() { console.log('Please refer ' + ES_REF_URL); throw new Error('script is not supported in RareTermsAggregation'); } }]); return RareTermsAggregation; }(BucketAggregationBase); module.exports = RareTermsAggregation;