elastic.js
Version:
Javascript API for ElasticSearch DSL
132 lines (100 loc) • 4.04 kB
JavaScript
/**
@class
<p>Matches documents that have fields containing terms with a specified
prefix (not analyzed). The prefix query maps to Lucene PrefixQuery.</p>
@name ejs.PrefixQuery
@ejs query
@borrows ejs.QueryMixin._type as _type
@borrows ejs.QueryMixin.toJSON as toJSON
@desc
Matches documents containing the specified un-analyzed prefix.
@param {String} field A valid field name.
@param {String} value A string prefix.
*/
ejs.PrefixQuery = function (field, value) {
var
_common = ejs.QueryMixin('prefix'),
query = _common.toJSON();
query.prefix[field] = {
value: value
};
return extend(_common, {
/**
The field to run the query against.
@member ejs.PrefixQuery
@param {String} f A single field name.
@returns {Object} returns <code>this</code> so that calls can be chained.
*/
field: function (f) {
var oldValue = query.prefix[field];
if (f == null) {
return field;
}
delete query.prefix[field];
field = f;
query.prefix[f] = oldValue;
return this;
},
/**
The prefix value.
@member ejs.PrefixQuery
@param {String} p A string prefix
@returns {Object} returns <code>this</code> so that calls can be chained.
*/
value: function (p) {
if (p == null) {
return query.prefix[field].value;
}
query.prefix[field].value = p;
return this;
},
/**
Sets rewrite method. Valid values are:
constant_score_auto - tries to pick the best constant-score rewrite
method based on term and document counts from the query
scoring_boolean - translates each term into boolean should and
keeps the scores as computed by the query
constant_score_boolean - same as scoring_boolean, expect no scores
are computed.
constant_score_filter - first creates a private Filter, by visiting
each term in sequence and marking all docs for that term
top_terms_boost_N - first translates each term into boolean should
and scores are only computed as the boost using the top N
scoring terms. Replace N with an integer value.
top_terms_N - first translates each term into boolean should
and keeps the scores as computed by the query. Only the top N
scoring terms are used. Replace N with an integer value.
Default is constant_score_auto.
This is an advanced option, use with care.
@member ejs.PrefixQuery
@param {String} m The rewrite method as a string.
@returns {Object} returns <code>this</code> so that calls can be chained.
*/
rewrite: function (m) {
if (m == null) {
return query.prefix[field].rewrite;
}
m = m.toLowerCase();
if (m === 'constant_score_auto' || m === 'scoring_boolean' ||
m === 'constant_score_boolean' || m === 'constant_score_filter' ||
m.indexOf('top_terms_boost_') === 0 ||
m.indexOf('top_terms_') === 0) {
query.prefix[field].rewrite = m;
}
return this;
},
/**
Sets the boost value of the <code>Query</code>.
@member ejs.PrefixQuery
@param {Double} boost A positive <code>double</code> value.
@returns {Object} returns <code>this</code> so that calls can be chained.
*/
boost: function (boost) {
if (boost == null) {
return query.prefix[field].boost;
}
query.prefix[field].boost = boost;
return this;
}
});
};