UNPKG

es-builder

Version:

Elasticsearch query builder for Node.js

166 lines (134 loc) 4.57 kB
'use strict'; /** Class representing a function score query.*/ var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var FunctionScoreQuery = function () { /** * Create a function score query */ function FunctionScoreQuery() { _classCallCheck(this, FunctionScoreQuery); this.function_score = {}; } /** * Getter for the completed function score query * @return complete query cloned */ _createClass(FunctionScoreQuery, [{ key: 'query', /** * Set source query * @param {Object} sourceQuery */ value: function query(sourceQuery) { this.function_score.query = sourceQuery; return this; } /** * Set source filter * @param {Object} sourceFilter */ }, { key: 'filter', value: function filter(sourceFilter) { this.function_score.filter = sourceFilter; return this; } /** * Set scoring mode * @param {string} mode */ }, { key: 'scoreMode', value: function scoreMode(mode) { this.function_score.score_mode = mode; return this; } /** * Set score threshold * @param {number} score */ }, { key: 'minScore', value: function minScore(score) { this.function_score.min_score = score; return this; } /** * Set how the new score is calculated * @param {string} mode */ }, { key: 'boostMode', value: function boostMode(mode) { this.function_score.boost_mode = mode; return this; } /** * Set maximum boost value * @param {number} boost */ }, { key: 'maxBoost', value: function maxBoost(boost) { this.function_score.max_boost = boost; return this; } /** * Set boost value for all documents * @param {number} boost */ }, { key: 'boost', value: function boost(_boost) { this.function_score.boost = _boost; return this; } /** * Add score function * NOTE: Not compatible with functions() method * @param {Object} scoreFunction */ }, { key: 'function', value: function _function(scoreFunction) { _extends(this.function_score, scoreFunction); return this; } /** * Add multiple score functions or single score function to score_functions array * NOTE: Not compatible with function() method * @param {Object[]|Object} scoreFunctions */ }, { key: 'functions', value: function functions(scoreFunctions) { var isArray = Array.isArray(scoreFunctions); if (!this.function_score.functions) { this.function_score.functions = isArray ? scoreFunctions : [scoreFunctions]; } else { if (isArray) { var _function_score$funct; (_function_score$funct = this.function_score.functions).push.apply(_function_score$funct, _toConsumableArray(scoreFunctions)); } else { this.function_score.functions.push(scoreFunctions); } } return this; } }, { key: 'built', get: function get() { return JSON.parse(JSON.stringify(this)); } }]); return FunctionScoreQuery; }(); var factoryFunctionScoreQuery = function factoryFunctionScoreQuery() { return new FunctionScoreQuery(); }; // also expose statically the original class factoryFunctionScoreQuery._originalClass = FunctionScoreQuery; module.exports = factoryFunctionScoreQuery;