elastic-builder
Version:
A JavaScript implementation of the elasticsearch Query DSL
131 lines (105 loc) • 4.34 kB
JavaScript
;
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 _require = require('../../core'),
Query = _require.Query;
/**
* The `FullTextQueryBase` provides support for common options used across
* various full text query implementations.
*
* **NOTE:** Instantiating this directly should not be required.
* However, if you wish to add a custom implementation for whatever reason,
* this class could be extended.
*
* @param {string} queryType
* @param {string=} queryString The query string
*
* @extends Query
*/
var FullTextQueryBase = function (_Query) {
(0, _inherits3.default)(FullTextQueryBase, _Query);
/*
Common options:
analyzer - applicable on all
minimum_should_match - applicable on all except Match Phrase and Match Phrase Prefix
query - applicable on all
*/
// eslint-disable-next-line require-jsdoc
function FullTextQueryBase(queryType, queryString) {
(0, _classCallCheck3.default)(this, FullTextQueryBase);
var _this = (0, _possibleConstructorReturn3.default)(this, (FullTextQueryBase.__proto__ || Object.getPrototypeOf(FullTextQueryBase)).call(this, queryType));
if (!isNil(queryString)) _this._queryOpts.query = queryString;
return _this;
}
/**
* Set the analyzer to control which analyzer will perform the analysis process on the text
*
* @example
* const qry = esb.matchPhraseQuery('message', 'this is a test')
* .analyzer('my_analyzer');
*
* @example
* const qry = esb.multiMatchQuery(['first', 'last', '*.edge'], 'Jon')
* .type('cross_fields')
* .analyzer('standard');
*
* @param {string} analyzer
* @returns {FullTextQueryBase} returns `this` so that calls can be chained.
*/
(0, _createClass3.default)(FullTextQueryBase, [{
key: 'analyzer',
value: function analyzer(_analyzer) {
this._queryOpts.analyzer = _analyzer;
return this;
}
/**
* Sets the value controlling how many "should" clauses in the resulting boolean
* query should match. It can be an absolute value (2), a percentage (30%)
* or a combination of both. For Common Terms Query when specifying different
* `minimum_should_match` for low and high frequency terms, an object with the
* keys `low_freq` and `high_freq` can be used.
*
* @example
* const qry = esb.commonTermsQuery('body', 'nelly the elephant as a cartoon')
* .minimumShouldMatch(2)
* .cutoffFrequency(0.001);
*
* @param {string|number|Object} minimumShouldMatch
* Note: Object notation can only be used with Common Terms Query.
* @returns {FullTextQueryBase} returns `this` so that calls can be chained.
*/
}, {
key: 'minimumShouldMatch',
value: function minimumShouldMatch(_minimumShouldMatch) {
this._queryOpts.minimum_should_match = _minimumShouldMatch;
return this;
}
/**
* Sets the query string.
*
* @example
* const qry = esb.queryStringQuery()
* .query('city.\\*:(this AND that OR thus)')
* .useDisMax(true);
*
* @param {string} queryString
* @returns {FullTextQueryBase} returns `this` so that calls can be chained.
*/
}, {
key: 'query',
value: function query(queryString) {
this._queryOpts.query = queryString;
return this;
}
}]);
return FullTextQueryBase;
}(Query);
module.exports = FullTextQueryBase;