UNPKG

elastic.js

Version:

Javascript API for ElasticSearch DSL

195 lines (156 loc) 6.33 kB
/** @class <p>A <code>boolQuery</code> allows you to build <em>Boolean</em> query constructs from individual term or phrase queries. For example you might want to search for documents containing the terms <code>javascript</code> and <code>python</code>.</p> @name ejs.BoolQuery @ejs query @borrows ejs.QueryMixin.boost as boost @borrows ejs.QueryMixin._type as _type @borrows ejs.QueryMixin.toJSON as toJSON @desc A Query that matches documents matching boolean combinations of other queries, e.g. <code>termQuerys, phraseQuerys</code> or other <code>boolQuerys</code>. */ ejs.BoolQuery = function () { var _common = ejs.QueryMixin('bool'), query = _common.toJSON(); return extend(_common, { /** Adds query to boolean container. Given query "must" appear in matching documents. @member ejs.BoolQuery @param {Object} oQuery A valid <code>Query</code> object @returns {Object} returns <code>this</code> so that calls can be chained. */ must: function (oQuery) { var i, len; if (query.bool.must == null) { query.bool.must = []; } if (oQuery == null) { return query.bool.must; } if (isQuery(oQuery)) { query.bool.must.push(oQuery.toJSON()); } else if (isArray(oQuery)) { query.bool.must = []; for (i = 0, len = oQuery.length; i < len; i++) { if (!isQuery(oQuery[i])) { throw new TypeError('Argument must be an array of Queries'); } query.bool.must.push(oQuery[i].toJSON()); } } else { throw new TypeError('Argument must be a Query or array of Queries'); } return this; }, /** Adds query to boolean container. Given query "must not" appear in matching documents. @member ejs.BoolQuery @param {Object} oQuery A valid query object @returns {Object} returns <code>this</code> so that calls can be chained. */ mustNot: function (oQuery) { var i, len; if (query.bool.must_not == null) { query.bool.must_not = []; } if (oQuery == null) { return query.bool.must_not; } if (isQuery(oQuery)) { query.bool.must_not.push(oQuery.toJSON()); } else if (isArray(oQuery)) { query.bool.must_not = []; for (i = 0, len = oQuery.length; i < len; i++) { if (!isQuery(oQuery[i])) { throw new TypeError('Argument must be an array of Queries'); } query.bool.must_not.push(oQuery[i].toJSON()); } } else { throw new TypeError('Argument must be a Query or array of Queries'); } return this; }, /** Adds query to boolean container. Given query "should" appear in matching documents. @member ejs.BoolQuery @param {Object} oQuery A valid query object @returns {Object} returns <code>this</code> so that calls can be chained. */ should: function (oQuery) { var i, len; if (query.bool.should == null) { query.bool.should = []; } if (oQuery == null) { return query.bool.should; } if (isQuery(oQuery)) { query.bool.should.push(oQuery.toJSON()); } else if (isArray(oQuery)) { query.bool.should = []; for (i = 0, len = oQuery.length; i < len; i++) { if (!isQuery(oQuery[i])) { throw new TypeError('Argument must be an array of Queries'); } query.bool.should.push(oQuery[i].toJSON()); } } else { throw new TypeError('Argument must be a Query or array of Queries'); } return this; }, /** Sets if the <code>Query</code> should be enhanced with a <code>MatchAllQuery</code> in order to act as a pure exclude when only negative (mustNot) clauses exist. Default: true. @member ejs.BoolQuery @param {String} trueFalse A <code>true/false</code value. @returns {Object} returns <code>this</code> so that calls can be chained. */ adjustPureNegative: function (trueFalse) { if (trueFalse == null) { return query.bool.adjust_pure_negative; } query.bool.adjust_pure_negative = trueFalse; return this; }, /** Enables or disables similarity coordinate scoring of documents matching the <code>Query</code>. Default: false. @member ejs.BoolQuery @param {String} trueFalse A <code>true/false</code value. @returns {Object} returns <code>this</code> so that calls can be chained. */ disableCoord: function (trueFalse) { if (trueFalse == null) { return query.bool.disable_coord; } query.bool.disable_coord = trueFalse; return this; }, /** <p>Sets the number of optional clauses that must match.</p> <p>By default no optional clauses are necessary for a match (unless there are no required clauses). If this method is used, then the specified number of clauses is required.</p> <p>Use of this method is totally independent of specifying that any specific clauses are required (or prohibited). This number will only be compared against the number of matching optional clauses.</p> @member ejs.BoolQuery @param {Integer} minMatch A positive <code>integer</code> value. @returns {Object} returns <code>this</code> so that calls can be chained. */ minimumNumberShouldMatch: function (minMatch) { if (minMatch == null) { return query.bool.minimum_number_should_match; } query.bool.minimum_number_should_match = minMatch; return this; } }); };