2.30.0
https://github.com/sudo-suhas/elastic-builder
elastic-builder
is a library for easily building elasticsearch request body for search.
It implements the builder syntax for building complex queries combining queries and aggregations.
What's Included:
The complete library documentation is present here.
There are two ways to use the classes for constructing queries:
// Import the library
const esb = require('elastic-builder'); // the builder
// Use `new` keyword for constructor instances of class
const requestBody = new esb.RequestBodySearch()
.query(new esb.MatchQuery('message', 'this is a test'));
// Or use helper methods which construct the object without need for the `new` keyword
const requestBody = esb.requestBodySearch()
.query(esb.matchQuery('message', 'this is a test'));
// Build the request body
requestBody.toJSON()
{
"query": {
"match": {
"message": "this is a test"
}
}
}
Demo - https://elastic-builder.js.org/
ProTip: The source is transpiled using babel for compatibility with older versions of node and used by default.
But this is not required in node env 6 and above. You can directly use the src
files:
const esb = require('elastic-builder/src');
This module is heavily influenced by elastic.js(not maintained anymore).
https://github.com/sudo-suhas/elastic-builder
elastic-builder
is a library for easily building elasticsearch request body for search.
It implements the builder syntax for building complex queries combining queries and aggregations.
What's Included:
The complete library documentation is present here.
There are two ways to use the classes for constructing queries:
// Import the library
const esb = require('elastic-builder'); // the builder
// Use `new` keyword for constructor instances of class
const requestBody = new esb.RequestBodySearch()
.query(new esb.MatchQuery('message', 'this is a test'));
// Or use helper methods which construct the object without need for the `new` keyword
const requestBody = esb.requestBodySearch()
.query(esb.matchQuery('message', 'this is a test'));
// Build the request body
requestBody.toJSON()
{
"query": {
"match": {
"message": "this is a test"
}
}
}
Demo - https://elastic-builder.js.org/
ProTip: The source is transpiled using babel for compatibility with older versions of node and used by default.
But this is not required in node env 6 and above. You can directly use the src
files:
const esb = require('elastic-builder/src');
This module is heavily influenced by elastic.js(not maintained anymore).
The RequestBodySearch
object provides methods generating an elasticsearch
search request body. The search request can be executed with a search DSL,
which includes the Query DSL, within its body.
const reqBody = esb.requestBodySearch()
.query(esb.termQuery('user', 'kimchy'))
.from(0)
.size(10);
reqBody.toJSON();
{
"query": { "term": { "user": "kimchy" } },
"from": 0,
"size": 10
}
// Query and aggregation
const reqBody = esb.requestBodySearch()
.query(esb.matchQuery('business_type', 'shop'))
.agg(
esb.geoBoundsAggregation('viewport', 'location').wrapLongitude(true)
);
// Query, aggregation with nested
const reqBody = esb.requestBodySearch()
.query(esb.matchQuery('crime', 'burglary'))
.agg(
esb.termsAggregation('towns', 'town').agg(
esb.geoCentroidAggregation('centroid', 'location')
)
);
Define query on the search request body using the Query DSL.
(Query)
RequestBodySearch
:
returns
this
so that calls can be chained.
Sets knn on the search request body.
((Knn | Array<Knn>))
RequestBodySearch
:
returns
this
so that calls can be chained.
Sets aggregation on the request body.
Alias for method aggregation
RequestBodySearch
:
returns
this
so that calls can be chained.
agg
is not an instance of
Aggregation
Sets aggregation on the request body.
RequestBodySearch
:
returns
this
so that calls can be chained.
agg
is not an instance of
Aggregation
Sets multiple nested aggregation items.
Alias for method aggregations
Aggregation
:
returns
this
so that calls can be chained.
Sets multiple nested aggregation items. This method accepts an array to set multiple nested aggregations in one call.
Aggregation
:
returns
this
so that calls can be chained.
Sets suggester on the request body.
RequestBodySearch
:
returns
this
so that calls can be chained.
suggest
is not an instance of
Suggester
const req = esb.requestBodySearch()
.query(esb.matchQuery('message', 'trying out elasticsearch'))
.suggest(
esb.termSuggester(
'my-suggestion',
'message',
'tring out Elasticsearch'
)
);
Sets the global suggest text to avoid repetition for multiple suggestions.
(string)
Global suggest text
RequestBodySearch
:
returns
this
so that calls can be chained.
const req = esb.requestBodySearch()
.suggestText('tring out elasticsearch')
.suggest(esb.termSuggester('my-suggest-1', 'message'))
.suggest(esb.termSuggester('my-suggest-2', 'user'));
Sets a search timeout, bounding the search request to be executed within the specified time value and bail with the hits accumulated up to that point when expired.
RequestBodySearch
:
returns
this
so that calls can be chained.
To retrieve hits from a certain offset.
(number)
Defaults to 0.
RequestBodySearch
:
returns
this
so that calls can be chained.
The number of hits to return. If you do not care about getting some hits back but only about the number of matches and/or aggregations, setting the value to 0 will help performance.
(number)
Defaults to 10.
RequestBodySearch
:
returns
this
so that calls can be chained.
The maximum number of documents to collect for each shard, upon reaching which
the query execution will terminate early. If set, the response will have a
boolean field terminated_early
to indicate whether the query execution has
actually terminated early.
(number)
Maximum number of documents to collect for each shard.
Defaults to no limit.
RequestBodySearch
:
returns
this
so that calls can be chained.
Allows to add sort on specific field. The sort can be reversed as well.
The sort is defined on a per field level, with special field name for _score
to
sort by score, and _doc
to sort by index order.
(Sort)
RequestBodySearch
:
returns
this
so that calls can be chained.
sort
is not an instance of
Sort
.
const reqBody = esb.requestBodySearch()
.query(esb.termQuery('user', 'kimchy'))
.sort(esb.sort('post_date', 'asc'))
.sort(esb.sort('user'))
.sorts([
esb.sort('name', 'desc'),
esb.sort('age', 'desc'),
esb.sort('_score')
]);
Allows to add multiple sort on specific fields. Each sort can be reversed as well. The sort is defined on a per field level, with special field name for _score to sort by score, and _doc to sort by index order.
RequestBodySearch
:
returns
this
so that calls can be chained.
sorts
is not an instance of
Sort
.
const reqBody = esb.requestBodySearch()
.query(esb.termQuery('user', 'kimchy'))
.sort(esb.sort('post_date', 'asc'))
.sort(esb.sort('user'))
.sorts([
esb.sort('name', 'desc'),
esb.sort('age', 'desc'),
esb.sort('_score')
]);
When sorting on a field, scores are not computed. By setting track_scores
to true,
scores will still be computed and tracked.
(boolean)
RequestBodySearch
:
returns
this
so that calls can be chained
const reqBody = esb.requestBodySearch()
.trackScores(true)
.sorts([
esb.sort('post_date', 'desc'),
esb.sort('name', 'desc'),
esb.sort('age', 'desc')
])
.query(esb.termQuery('user', 'kimchy'));
The track_total_hits
parameter allows you to control how the total number of hits
should be tracked. Passing false
can increase performance in some situations.
(Added in elasticsearch@7)
Pass true
, false
, or the upper limit (default: 10000
) of hits you want tracked.
RequestBodySearch
:
returns
this
so that calls can be chained
Allows to control how the _source
field is returned with every hit.
You can turn off _source
retrieval by passing false
.
It also accepts one(string) or more wildcard(array) patterns to control
what parts of the _source
should be returned
An object can also be used to specify the wildcard patterns for includes
and excludes
.
RequestBodySearch
:
returns
this
so that calls can be chained
// To disable `_source` retrieval set to `false`:
const reqBody = esb.requestBodySearch()
.query(esb.termQuery('user', 'kimchy'))
.source(false);
// The `_source` also accepts one or more wildcard patterns to control what
// parts of the `_source` should be returned:
const reqBody = esb.requestBodySearch()
.query(esb.termQuery('user', 'kimchy'))
.source('obj.*');
// OR
const reqBody = esb.requestBodySearch()
.query(esb.termQuery('user', 'kimchy'))
.source([ 'obj1.*', 'obj2.*' ]);
// For complete control, you can specify both `includes` and `excludes` patterns:
const reqBody = esb.requestBodySearch()
.query(esb.termQuery('user', 'kimchy'))
.source({
'includes': [ 'obj1.*', 'obj2.*' ],
'excludes': [ '*.description' ]
});
The stored_fields
parameter is about fields that are explicitly marked as stored in the mapping.
Selectively load specific stored fields for each document represented by a search hit
using array of stored fields.
An empty array will cause only the _id
and _type
for each hit to be returned.
To disable the stored fields (and metadata fields) entirely use: _none_
RequestBodySearch
:
returns
this
so that calls can be chained
// Selectively load specific stored fields for each document
// represented by a search hit
const reqBody = esb.requestBodySearch()
.query(esb.termQuery('user', 'kimchy'))
.storedFields(['user', 'postDate']);
// Return only the `_id` and `_type` to be returned:
const reqBody = esb.requestBodySearch()
.query(esb.termQuery('user', 'kimchy'))
.storedFields([]);
// Disable the stored fields (and metadata fields) entirely
const reqBody = esb.requestBodySearch()
.query(esb.termQuery('user', 'kimchy'))
.storedFields('_none_');
Computes a document property dynamically based on the supplied runtimeField
.
Added in Elasticsearch v7.11.0 Release note
(string)
Name for the computed runtime mapping field.
(RuntimeField)
Instance of RuntimeField
RequestBodySearch
:
returns
this
so that calls can be chained
const reqBody = esb.requestBodySearch()
.query(esb.matchAllQuery())
.runtimeMapping(
'sessionId-name',
esb.runtimeField(
'keyword',
`emit(doc['session_id'].value + '::' + doc['name'].value)`
)
)
// runtime fields can also be used in query aggregation
const reqBody = esb.requestBodySearch()
.query(esb.matchAllQuery())
.runtimeMapping(
'sessionId-eventName',
esb.runtimeField(
'keyword',
`emit(doc['session_id'].value + '::' + doc['eventName'].value)`,
)
)
.agg(esb.cardinalityAggregation('uniqueCount', `sessionId-eventName`)),;
Computes one or more document properties dynamically based on supplied RuntimeField
s.
Added in Elasticsearch v7.11.0 Release note
(Object)
Object with
runtimeFieldName
as key and instance of
RuntimeField
as the value.
RequestBodySearch
:
returns
this
so that calls can be chained
const fieldA = esb.runtimeField(
'keyword',
`emit(doc['session_id'].value + '::' + doc['name'].value)`
);
const reqBody = esb.requestBodySearch()
.query(esb.matchAllQuery())
.runtimeMappings({
'sessionId-name': fieldA,
})
Computes a document property dynamically based on the supplied Script
.
RequestBodySearch
:
returns
this
so that calls can be chained
const reqBody = esb.requestBodySearch()
.query(esb.matchAllQuery())
.scriptField(
'test1',
esb.script('inline', "doc['my_field_name'].value * 2").lang('painless')
)
.scriptField(
'test2',
esb.script('inline', "doc['my_field_name'].value * factor")
.lang('painless')
.params({ factor: 2.0 })
);
// Script fields can also access the actual `_source` document and extract
// specific elements to be returned from it by using `params['_source']`.
const reqBody = esb.requestBodySearch()
.query(esb.matchAllQuery())
.scriptField('test1', "params['_source']['message']");
Sets given dynamic document properties to be computed using supplied Script
s.
Object should have scriptFieldName
as key and script
as the value.
TopHitsAggregation
:
returns
this
so that calls can be chained
const reqBody = esb.requestBodySearch()
.query(esb.matchAllQuery())
.scriptFields({
test1: esb
.script('inline', "doc['my_field_name'].value * 2")
.lang('painless'),
test2: esb
.script('inline', "doc['my_field_name'].value * factor")
.lang('painless')
.params({ factor: 2.0 })
});
// Script fields can also access the actual `_source` document and extract
// specific elements to be returned from it by using `params['_source']`.
const reqBody = esb.requestBodySearch()
.query(esb.matchAllQuery())
.scriptFields({ test1: "params['_source']['message']" });
Allows to return the doc value representation of a field for each hit. Doc value fields can work on fields that are not stored.
RequestBodySearch
:
returns
this
so that calls can be chained
const reqBody = esb.requestBodySearch()
.query(esb.matchAllQuery())
.docvalueFields(['test1', 'test2']);
The post_filter
is applied to the search hits at the very end of a search request,
after aggregations have already been calculated.
(Query)
The filter to be applied after aggregation.
RequestBodySearch
:
returns
this
so that calls can be chained
const reqBody = esb.requestBodySearch()
.query(esb.boolQuery().filter(esb.termQuery('brand', 'gucci')))
.agg(esb.termsAggregation('colors', 'color'))
.agg(
esb.filterAggregation(
'color_red',
esb.termQuery('color', 'red')
).agg(esb.termsAggregation('models', 'model'))
)
.postFilter(esb.termQuery('color', 'red'));
Allows to highlight search results on one or more fields. The implementation
uses either the lucene plain
highlighter, the fast vector highlighter (fvh
)
or postings
highlighter.
Note: The postings
highlighter has been removed in elasticsearch 6.0.
The unified
highlighter outputs the same highlighting when
index_options
is set to offsets
.
(Highlight)
RequestBodySearch
:
returns
this
so that calls can be chained
const reqBody = esb.requestBodySearch()
.query(esb.matchAllQuery())
.highlight(esb.highlight('content'));
const reqBody = esb.requestBodySearch()
.query(
esb.percolateQuery('query', 'doctype').document({
message: 'The quick brown fox jumps over the lazy dog'
})
)
.highlight(esb.highlight('message'));
Rescoring can help to improve precision by reordering just the top (eg 100 - 500)
documents returned by the query
and post_filter
phases, using a secondary
(usually more costly) algorithm, instead of applying the costly algorithm to
all documents in the index.
(Rescore)
RequestBodySearch
:
returns
this
so that calls can be chained
query
is not an instance of
Rescore
const reqBody = esb.requestBodySearch()
.query(esb.matchQuery('message', 'the quick brown').operator('or'))
.rescore(
esb.rescore(
50,
esb.matchPhraseQuery('message', 'the quick brown').slop(2)
)
.queryWeight(0.7)
.rescoreQueryWeight(1.2)
);
const reqBody = esb.requestBodySearch()
.query(esb.matchQuery('message', 'the quick brown').operator('or'))
.rescore(
esb.rescore(
100,
esb.matchPhraseQuery('message', 'the quick brown').slop(2)
)
.queryWeight(0.7)
.rescoreQueryWeight(1.2)
)
.rescore(
esb.rescore(
10,
esb.functionScoreQuery().function(
esb.scriptScoreFunction(
esb.script('inline', 'Math.log10(doc.likes.value + 2)')
)
)
).scoreMode('multiply')
);
Enables explanation for each hit on how its score was computed.
(boolean)
RequestBodySearch
:
returns
this
so that calls can be chained
const reqBody = esb.requestBodySearch()
.query(esb.termQuery('user', 'kimchy'))
.explain(true);
Returns a version for each search hit.
(boolean)
RequestBodySearch
:
returns
this
so that calls can be chained.
const reqBody = esb.requestBodySearch()
.query(esb.termQuery('user', 'kimchy'))
.version(true);
Allows to configure different boost level per index when searching across more than one indices. This is very handy when hits coming from one index matter more than hits coming from another index.
Alias for method indicesBoost
.
RequestBodySearch
:
returns
this
so that calls can be chained.
const reqBody = esb.requestBodySearch()
.indexBoost('alias1', 1.4)
.indexBoost('index*', 1.3);
Allows to configure different boost level per index when searching across more than one indices. This is very handy when hits coming from one index matter more than hits coming from another index.
RequestBodySearch
:
returns
this
so that calls can be chained.
const reqBody = esb.requestBodySearch()
.indicesBoost('alias1', 1.4)
.indicesBoost('index*', 1.3);
Exclude documents which have a _score
less than the minimum specified in min_score
.
(number)
RequestBodySearch
:
returns
this
so that calls can be chained.
const reqBody = esb.requestBodySearch()
.query(esb.termQuery('user', 'kimchy'))
.minScore(0.5);
Allows to collapse search results based on field values. The collapsing is done by selecting only the top sorted document per collapse key.
The field used for collapsing must be a single valued keyword
or numeric
field with doc_values
activated
(string)
(InnerHits?)
Allows to expand each collapsed top hits.
(number?)
The number of concurrent
requests allowed to retrieve the inner_hits' per group
RequestBodySearch
:
returns
this
so that calls can be chained.
innerHits
is not an instance of
InnerHits
const reqBody = esb.requestBodySearch()
.query(esb.matchQuery('message', 'elasticsearch'))
.collapse('user')
.sort(esb.sort('likes'))
.from(10);
// Expand each collapsed top hits with the `inner_hits` option:
const reqBody = esb.requestBodySearch()
.query(esb.matchQuery('message', 'elasticsearch'))
.collapse(
'user',
esb.innerHits('last_tweets')
.size(5)
.sort(esb.sort('date', 'asc')),
4
)
.sort(esb.sort('likes'))
.from(10);
Allows to use the results from the previous page to help the retrieval
of the next page. The search_after
parameter provides a live cursor.
The parameter from
must be set to 0
(or -1
) when search_after
is used.
RequestBodySearch
:
returns
this
so that calls can be chained.
const reqBody = esb.requestBodySearch()
.size(10)
.query(esb.matchQuery('message', 'elasticsearch'))
.searchAfter(1463538857, 'tweet#654323')
.sorts([esb.sort('date', 'asc'), esb.sort('_uid', 'desc')]);
These classes allow creation and manipulation of objects which map to elasticsearch DSL for queries.
These classes allow creation and manipulation of objects which map to elasticsearch DSL for queries.
Base class implementation for all query types.
NOTE: Instantiating this directly should not be required. However, if you wish to add a custom implementation for whatever reason, this class should be extended and used, as validation against the class type is present in various places.
(string)
Sets the query name.
(string)
Query
:
returns
this
so that calls can be chained.
const boolQry = esb.boolQuery()
.should([
esb.matchQuery('name.first', 'shay').name('first'),
esb.matchQuery('name.last', 'banon').name('last')
])
.filter(esb.termsQuery('name.last', ['banon', 'kimchy']).name('test'));
The most simple query, which matches all documents, giving them all a _score
of 1.0
.
Extends Query
const qry = esb.matchAllQuery().boost(1.2);
The inverse of the match_all
query, which matches no documents.
Extends Query
const qry = esb.matchNoneQuery();
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.
Extends Query
Set the analyzer to control which analyzer will perform the analysis process on the text
(string)
FullTextQueryBase
:
returns
this
so that calls can be chained.
const qry = esb.matchPhraseQuery('message', 'this is a test')
.analyzer('my_analyzer');
const qry = esb.multiMatchQuery(['first', 'last', '*.edge'], 'Jon')
.type('cross_fields')
.analyzer('standard');
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.
FullTextQueryBase
:
returns
this
so that calls can be chained.
const qry = esb.commonTermsQuery('body', 'nelly the elephant as a cartoon')
.minimumShouldMatch(2)
.cutoffFrequency(0.001);
Sets the query string.
(string)
FullTextQueryBase
:
returns
this
so that calls can be chained.
const qry = esb.queryStringQuery()
.query('city.\\*:(this AND that OR thus)')
.useDisMax(true);
The MonoFieldQueryBase
provides support for common options used across
various full text query implementations with single search field.
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.
Extends FullTextQueryBase
(string)
(string?)
The document field to query against
(string?)
The query string
Sets the field to search on.
(string)
MonoFieldQueryBase
:
returns
this
so that calls can be chained.
match
query accepts text/numerics/dates, analyzes them, and constructs a query.
Extends MonoFieldQueryBase
const matchQry = esb.matchQuery('message', 'to be or not to be');
// Providing additional parameters:
const qry = esb.matchQuery('message', 'this is a test').operator('and');
The operator to be used in the boolean query which is constructed
by analyzing the text provided. The operator
flag can be set to or
or
and
to control the boolean clauses (defaults to or
).
MatchQuery
:
returns
this
so that calls can be chained.
Sets the lenient
parameter which allows to ignore exceptions caused
by data-type mismatches such as trying to query a numeric field with a
text query string when set to true
.
MatchQuery
:
returns
this
so that calls can be chained.
Sets the fuzziness
parameter which is interpreted as a Levenshtein Edit Distance —
the number of one character changes that need to be made to one string to make it
the same as another string.
MatchQuery
:
returns
this
so that calls can be chained.
Sets the prefix length for a fuzzy prefix MatchQuery
(number)
MatchQuery
:
returns
this
so that calls can be chained.
Sets the max expansions for a fuzzy prefix MatchQuery
(number)
MatchQuery
:
returns
this
so that calls can be chained.
Sets the rewrite method. Valid values are:
constant_score
- tries to pick the best constant-score rewrite
method based on term and document counts from the query.
Synonyms - constant_score_auto
, constant_score_filter
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
.
Note: The deprecated multi term rewrite parameters constant_score_auto
,
constant_score_filter
(synonyms for constant_score
) have been removed
in elasticsearch 6.0.
This is an advanced option, use with care.
(string)
The rewrite method as a string.
MatchQuery
:
returns
this
so that calls can be chained.
rewrite
method is not valid.
Sets the fuzzy rewrite method. Valid values are:
constant_score
- tries to pick the best constant-score rewrite
method based on term and document counts from the query.
Synonyms - constant_score_auto
, constant_score_filter
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
.
This is an advanced option, use with care.
Note: The deprecated multi term rewrite parameters constant_score_auto
,
constant_score_filter
(synonyms for constant_score
) have been removed
in elasticsearch 6.0.
(string)
The rewrite method as a string.
MatchQuery
:
returns
this
so that calls can be chained.
fuzzy_rewrite
method is not valid.
Fuzzy transpositions (ab
→ ba
) are allowed by default but can be disabled
by setting fuzzy_transpositions
to false.
(boolean)
MatchQuery
:
returns
this
so that calls can be chained.
If the analyzer used removes all tokens in a query like a stop
filter does,
the default behavior is to match no documents at all. In order to change that
the zero_terms_query
option can be used, which accepts none
(default) and all
which corresponds to a match_all
query.
MatchQuery
:
returns
this
so that calls can be chained.
const qry = esb.matchQuery('message', 'to be or not to be')
.operator('and')
.zeroTermsQuery('all');
Allows specifying an absolute or relative document frequency where high frequency
terms are moved into an optional subquery and are only scored if one of the
low frequency (below the cutoff) terms in the case of an or
operator or
all of the low frequency terms in the case of an and
operator match.
(number)
It can either be relative to the total number of documents
if in the range
[0..1)
or absolute if greater or equal to
1.0
.
MatchQuery
:
returns
this
so that calls can be chained.
const qry = esb.matchQuery('message', 'to be or not to be')
.cutoffFrequency(0.001);
The MatchPhraseQueryBase
provides support for common options used across
various bucket match phrase 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.
Extends MonoFieldQueryBase
(string)
(string)
(string?)
The document field to query against
(string?)
The query string
MatchPhraseQueryBase
Configures the slop
(default is 0) for matching terms in any order.
Transposed terms have a slop of 2.
(number)
A positive integer value, defaults is 0.
MatchPhraseQueryBase
:
returns
this
so that calls can be chained.
The match_phrase
query analyzes the text and creates a phrase
query out of
the analyzed text.
Extends MatchPhraseQueryBase
const qry = esb.matchPhraseQuery('message', 'to be or not to be');
Extends MatchPhraseQueryBase
const qry = esb.matchPhrasePrefixQuery('message', 'quick brown f');
Control to how many prefixes the last term will be expanded.
(number)
Defaults to 50.
MatchPhrasePrefixQuery
:
returns
this
so that calls can be chained.
const qry = esb.matchPhrasePrefixQuery('message', 'quick brown f')
.maxExpansions(10);
A MultiMatchQuery
query builds further on top of the
MultiMatchQuery
by allowing multiple fields to be specified.
The idea here is to allow to more easily build a concise match type query
over multiple fields instead of using a relatively more expressive query
by using multiple match queries within a bool query.
Extends FullTextQueryBase
const qry = esb.multiMatchQuery(['subject', 'message'], 'this is a test');
Appends given field to the list of fields to search against.
Fields can be specified with wildcards.
Individual fields can be boosted with the caret (^) notation.
Example - "subject^3"
(string)
One of the fields to be queried
MultiMatchQuery
:
returns
this
so that calls can be chained.
Appends given fields to the list of fields to search against. Fields can be specified with wildcards. Individual fields can be boosted with the caret (^) notation.
MultiMatchQuery
:
returns
this
so that calls can be chained.
// Boost individual fields with caret `^` notation
const qry = esb.multiMatchQuery(['subject^3', 'message'], 'this is a test');
// Specify fields with wildcards
const qry = esb.multiMatchQuery(['title', '*_name'], 'Will Smith');
Sets the type of multi match query. Valid values are:
best_fields
- (default) Finds documents which match any field,
but uses the _score
from the best field.
most_fields
- Finds documents which match any field and combines
the _score
from each field.
cross_fields
- Treats fields with the same analyzer
as though
they were one big field. Looks for each word in any field
phrase
- Runs a match_phrase
query on each field and combines
the _score
from each field.
phrase_prefix
- Runs a match_phrase_prefix
query on each field
and combines the _score
from each field.
bool_prefix
- (added in v7.2) Creates a match_bool_prefix query on each field and
combines the _score from each field.
(string)
Can be one of
best_fields
,
most_fields
,
cross_fields
,
phrase
,
phrase_prefix
and
bool_prefix
. Default is
best_fields
.
MultiMatchQuery
:
returns
this
so that calls can be chained.
// Find the single best matching field
const qry = esb.multiMatchQuery(['subject', 'message'], 'brown fox')
.type('best_fields')
.tieBreaker(0.3);
// Query multiple fields analyzed differently for the same text
const qry = esb.multiMatchQuery(
['title', 'title.original', 'title.shingles'],
'quick brown fox'
).type('most_fields');
// Run a `match_phrase_prefix` query on multiple fields
const qry = esb.multiMatchQuery(
['subject', 'message'],
'quick brown f'
).type('phrase_prefix');
// All terms must be present in at least one field for document to match
const qry = esb.multiMatchQuery(['first_name', 'last_name'], 'Will Smith')
.type('cross_fields')
.operator('and');
The tie breaker value. The tie breaker capability allows results
that include the same term in multiple fields to be judged better than
results that include this term in only the best of those multiple
fields, without confusing this with the better case of two different
terms in the multiple fields. Default: 0.0
.
(number)
MultiMatchQuery
:
returns
this
so that calls can be chained.
The operator to be used in the boolean query which is constructed
by analyzing the text provided. The operator
flag can be set to or
or
and
to control the boolean clauses (defaults to or
).
MultiMatchQuery
:
returns
this
so that calls can be chained.
Sets the lenient
parameter which allows to ignore exceptions caused
by data-type mismatches such as trying to query a numeric field with a
text query string when set to true
.
MultiMatchQuery
:
returns
this
so that calls can be chained.
Configures the slop
(default is 0) for matching terms in any order.
Transposed terms have a slop of 2.
(number)
A positive integer value, defaults is 0.
MultiMatchQuery
:
returns
this
so that calls can be chained.
Sets the fuzziness
parameter which is interpreted as a Levenshtein Edit Distance —
the number of one character changes that need to be made to one string to make it
the same as another string.
The fuzziness
parameter cannot be used with the phrase
, phrase_prefix
or cross_fields
type.
MultiMatchQuery
:
returns
this
so that calls can be chained.
Sets the prefix length for a fuzzy prefix MultiMatchQuery
(number)
MultiMatchQuery
:
returns
this
so that calls can be chained.
Sets the max expansions for a fuzzy prefix MultiMatchQuery
(number)
MultiMatchQuery
:
returns
this
so that calls can be chained.
Sets the rewrite method. Valid values are:
constant_score
- tries to pick the best constant-score rewrite
method based on term and document counts from the query.
Synonyms - constant_score_auto
, constant_score_filter
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
.
This is an advanced option, use with care.
Note: The deprecated multi term rewrite parameters constant_score_auto
,
constant_score_filter
(synonyms for constant_score
) have been removed
in elasticsearch 6.0.
(string)
The rewrite method as a string.
MultiMatchQuery
:
returns
this
so that calls can be chained.
rewrite
method is not valid.
Sets the fuzzy rewrite method. Valid values are:
constant_score
- tries to pick the best constant-score rewrite
method based on term and document counts from the query.
Synonyms - constant_score_auto
, constant_score_filter
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
.
This is an advanced option, use with care.
Note: The deprecated multi term rewrite parameters constant_score_auto
,
constant_score_filter
(synonyms for constant_score
) have been removed
in elasticsearch 6.0.
(string)
The rewrite method as a string.
MultiMatchQuery
:
returns
this
so that calls can be chained.
fuzzy_rewrite
method is not valid.
If the analyzer used removes all tokens in a query like a stop
filter does,
the default behavior is to match no documents at all. In order to change that
the zero_terms_query
option can be used, which accepts none
(default) and all
which corresponds to a match_all
query.
MultiMatchQuery
:
returns
this
so that calls can be chained.
Allows specifying an absolute or relative document frequency where high frequency
terms are moved into an optional subquery and are only scored if one of the
low frequency (below the cutoff) terms in the case of an or
operator or
all of the low frequency terms in the case of an and
operator match.
(number)
It can either be relative to the total number of documents
if in the range
[0..1)
or absolute if greater or equal to
1.0
.
MultiMatchQuery
:
returns
this
so that calls can be chained.
The common
terms query is a modern alternative to stopwords which
improves the precision and recall of search results (by taking
stopwords into account), without sacrificing performance.
Extends MonoFieldQueryBase
const qry = esb.commonTermsQuery('body','this is bonsai cool')
.cutoffFrequency(0.001);
Allows specifying an absolute or relative document frequency where high frequency
terms are moved into an optional subquery and are only scored if one of the
low frequency (below the cutoff) terms in the case of an or
operator or
all of the low frequency terms in the case of an and
operator match.
(number)
It can either be relative to the total number of documents
if in the range
[0..1)
or absolute if greater or equal to
1.0
.
CommonTermsQuery
:
returns
this
so that calls can be chained.
The operator to be used on low frequency terms in the boolean query
which is constructed by analyzing the text provided. The operator
flag
can be set to or
or and
to control the boolean clauses (defaults to or
).
CommonTermsQuery
:
returns
this
so that calls can be chained.
const qry = esb.commonTermsQuery('body', 'nelly the elephant as a cartoon')
.lowFreqOperator('and')
.cutoffFrequency(0.001);
The operator to be used on high frequency terms in the boolean query
which is constructed by analyzing the text provided. The operator
flag
can be set to or
or and
to control the boolean clauses (defaults to or
).
CommonTermsQuery
:
returns
this
so that calls can be chained.
Sets the value controlling how many "should" clauses in the resulting boolean query should match for low frequency terms. It can be an absolute value (2), a percentage (30%) or a combination of both.
CommonTermsQuery
:
returns
this
so that calls can be chained.
const qry = esb.commonTermsQuery('body', 'nelly the elephant as a cartoon')
.lowFreq(2)
.highFreq(3)
.cutoffFrequency(0.001);
Sets the value controlling how many "should" clauses in the resulting boolean query should match for high frequency terms. It can be an absolute value (2), a percentage (30%) or a combination of both.
CommonTermsQuery
:
returns
this
so that calls can be chained.
const qry = esb.commonTermsQuery('body', 'nelly the elephant as a cartoon')
.lowFreq(2)
.highFreq(3)
.cutoffFrequency(0.001);
Enables or disables similarity coordinate scoring of documents
commoning the CommonTermsQuery
. Default: false
.
NOTE: This has been removed in elasticsearch 6.0. If provided, it will be ignored and a deprecation warning will be issued.
(boolean)
CommonTermsQuery
:
returns
this
so that calls can be chained.
The QueryStringQueryBase
provides support for common options used across
full text query implementations QueryStringQuery
and SimpleQueryStringQuery
.
A query that uses a query parser in order to parse its content.
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.
Extends FullTextQueryBase
Appends given field to the list of fields to search against. Fields can be specified with wildcards.
Individual fields can be boosted with the caret (^) notation.
Example - "subject^3"
(string)
One of the fields to be queried
QueryStringQueryBase
:
returns
this
so that calls can be chained.
const qry = esb.queryStringQuery('this AND that OR thus')
.field('city.*')
.useDisMax(true);
const qry = esb.simpleQueryStringQuery('foo bar -baz').field('content');
Appends given fields to the list of fields to search against. Fields can be specified with wildcards.
Individual fields can be boosted with the caret (^) notation.
Example - [ "subject^3", "message" ]
QueryStringQueryBase
:
returns
this
so that calls can be chained.
const qry = esb.queryStringQuery('this AND that')
.fields(['content', 'name'])
const qry = esb.simpleQueryStringQuery('foo bar baz')
.fields(['content', 'name.*^5']);
The default operator used if no explicit operator is specified.
For example, with a default operator of OR
, the query capital of Hungary
is translated to capital OR of OR Hungary
, and with default operator of AND,
the same query is translated to capital AND of AND Hungary
.
The default value is OR.
QueryStringQueryBase
:
returns
this
so that calls can be chained.
By default, wildcards terms in a query string are not analyzed.
By setting this value to true
, a best effort will be made to analyze those as well.
(boolean)
QueryStringQueryBase
:
returns
this
so that calls can be chained.
Sets the lenient
parameter which allows to ignore exceptions caused
by data-type mismatches such as trying to query a numeric field with a
text query string when set to true
.
QueryStringQueryBase
:
returns
this
so that calls can be chained.
A suffix to append to fields for quoted parts of the query string. This allows to use a field that has a different analysis chain for exact matching.
(string)
QueryStringQueryBase
:
returns
this
so that calls can be chained.
Perform the query on all fields detected in the mapping that can be queried.
Will be used by default when the _all
field is disabled and
no default_field
is specified (either in the index settings or
in the request body) and no fields
are specified.
(boolean)
QueryStringQueryBase
:
returns
this
so that calls can be chained.
A query that uses a query parser in order to parse its content.
Extends QueryStringQueryBase
(string?)
The actual query to be parsed.
const qry = esb.queryStringQuery('this AND that OR thus')
.defaultField('content');
The default field for query terms if no prefix field is specified.
Defaults to the index.query.default_field
index settings, which
in turn defaults to _all
.
(string)
QueryStringQuery
:
returns
this
so that calls can be chained.
When set, *
or ?
are allowed as the first character. Defaults to true
.
(boolean)
QueryStringQuery
:
returns
this
so that calls can be chained.
Set to true to enable position increments in result queries. Defaults to true.
(boolean)
QueryStringQuery
:
returns
this
so that calls can be chained.
Controls the number of terms fuzzy queries will expand to. Defaults to 50
.
(number)
QueryStringQuery
:
returns
this
so that calls can be chained.
Sets the fuzziness
parameter which is interpreted as a Levenshtein Edit Distance —
the number of one character changes that need to be made to one string to make it
the same as another string. Defaults to AUTO
.
QueryStringQuery
:
returns
this
so that calls can be chained.
Set the prefix length for fuzzy queries. Default is 0
.
(number)
QueryStringQuery
:
returns
this
so that calls can be chained.
Sets the rewrite method. Valid values are:
constant_score
- tries to pick the best constant-score rewrite
method based on term and document counts from the query.
Synonyms - constant_score_auto
, constant_score_filter
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
.
This is an advanced option, use with care.
Note: The deprecated multi term rewrite parameters constant_score_auto
,
constant_score_filter
(synonyms for constant_score
) have been removed
in elasticsearch 6.0.
(string)
The rewrite method as a string.
QueryStringQuery
:
returns
this
so that calls can be chained.
rewrite
method is not valid.
Sets the fuzzy rewrite method. Valid values are:
constant_score
- tries to pick the best constant-score rewrite
method based on term and document counts from the query.
Synonyms - constant_score_auto
, constant_score_filter
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
.
This is an advanced option, use with care.
Note: The deprecated multi term rewrite parameters constant_score_auto
,
constant_score_filter
(synonyms for constant_score
) have been removed
in elasticsearch 6.0.
(string)
The rewrite method as a string.
QueryStringQuery
:
returns
this
so that calls can be chained.
fuzzy_rewrite
method is not valid.
Sets the default slop for phrases. If zero, then exact phrase matches are required. Default value is 0.
(number)
A positive integer value, defaults is 0.
QueryStringQuery
:
returns
this
so that calls can be chained.
Auto generate phrase queries. Defaults to false
.
Note: This parameter has been removed in elasticsearch 6.0. If provided, it will be ignored and issue a deprecation warning.
(boolean)
QueryStringQuery
:
returns
this
so that calls can be chained.
Limit on how many automaton states regexp queries are allowed to create. This protects against too-difficult (e.g. exponentially hard) regexps. Defaults to 10000.
(number)
QueryStringQuery
:
returns
this
so that calls can be chained.
Time Zone to be applied to any range query related to dates.
(string)
QueryStringQuery
:
returns
this
so that calls can be chained.
Whether query text should be split on whitespace prior to analysis.
Instead the queryparser would parse around only real operators.
Default is false
. It is not allowed to set this option to false
if auto_generate_phrase_queries
is already set to true
.
Note: This parameter has been removed in elasticsearch 6.0. If provided,
it will be ignored and issue a deprecation warning. The query_string
query now splits on operator only.
(string)
QueryStringQuery
:
returns
this
so that calls can be chained.
Should the queries be combined using dis_max
(set it to true
),
or a bool query (set it to false
). Defaults to true
.
Note: This parameter has been removed in elasticsearch 6.0. If provided,
it will be ignored and issue a deprecation warning. The tie_breaker
parameter must be used instead.
(boolean)
QueryStringQuery
:
returns
this
so that calls can be chained.
const qry = esb.queryStringQuery('this AND that OR thus')
.fields(['content', 'name^5'])
.useDisMax(true);
When using dis_max
, the disjunction max tie breaker. Defaults to 0
.
(number)
QueryStringQuery
:
returns
this
so that calls can be chained.
Sets the quote analyzer name used to analyze the query
when in quoted text.
(string)
A valid analyzer name.
QueryStringQuery
:
returns
this
so that calls can be chained.
If they query string should be escaped or not.
(boolean)
QueryStringQuery
:
returns
this
so that calls can be chained.
A query that uses the SimpleQueryParser
to parse its context.
Unlike the regular query_string
query, the simple_query_string
query
will never throw an exception, and discards invalid parts of the query.
Extends QueryStringQueryBase
(string?)
The query string
const qry = esb.simpleQueryStringQuery(
'"fried eggs" +(eggplant | potato) -frittata'
)
.analyzer('snowball')
.fields(['body^5', '_all'])
.defaultOperator('and');
simple_query_string
support multiple flags to specify which parsing features
should be enabled. It is specified as a |
-delimited string.
(string)
|
delimited string. The available flags are:
ALL
,
NONE
,
AND
,
OR
,
NOT
,
PREFIX
,
PHRASE
,
PRECEDENCE
,
ESCAPE
,
WHITESPACE
,
FUZZY
,
NEAR
, and
SLOP
.
SimpleQueryStringQuery
:
returns
this
so that calls can be chained.
const qry = esb.simpleQueryStringQuery('foo | bar + baz*')
.flags('OR|AND|PREFIX');
The ValueTermQueryBase
provides support for common options used across
various term level query implementations.
Extends Query
(string)
(string?)
The document field to query against
(string?)
The query string
Sets the field to search on.
(string)
ValueTermQueryBase
:
returns
this
so that calls can be chained.
Sets the query string.
ValueTermQueryBase
:
returns
this
so that calls can be chained.
Allows ASCII case insensitive matching of the value with the indexed field values when set to true.
NOTE: Only available in Elasticsearch v7.10.0+
(boolean
= true
)
ValueTermQueryBase
:
returns
this
so that calls can be chained.
The term
query finds documents that contain the exact term specified
in the inverted index.
Extends ValueTermQueryBase
const termQry = esb.termQuery('user', 'Kimchy');
Filters documents that have fields that match any of the provided terms (not analyzed).
Extends Query
const qry = esb.constantScoreQuery(
esb.termsQuery('user', ['kimchy', 'elasticsearch'])
);
const qry = esb.termsQuery('user')
.index('users')
.type('user')
.id(2)
.path('followers');
Sets the field to search on.
(string)
TermsQuery
:
returns
this
so that calls can be chained.
Append given value to list of values to run Terms Query with.
TermsQuery
:
returns
this
so that calls can be chained
Specifies the values to run query for.
TermsQuery
:
returns
this
so that calls can be chained
values
is not an instance of Array
Convenience method for setting term lookup options.
Valid options are index
, type
, id
, path
and routing
TermsQuery
:
returns
this
so that calls can be chained
The index to fetch the term values from. Defaults to the current index.
Note: The index
parameter in the terms filter, used to look up terms in
a dedicated index is mandatory in elasticsearch 6.0. Previously, the
index defaulted to the index the query was executed on. In 6.0, this
index must be explicitly set in the request.
(string)
The index to fetch the term values from.
Defaults to the current index.
TermsQuery
:
returns
this
so that calls can be chained
The type to fetch the term values from.
(string)
TermsQuery
:
returns
this
so that calls can be chained
The id of the document to fetch the term values from.
(string)
TermsQuery
:
returns
this
so that calls can be chained
The field specified as path to fetch the actual values for the terms
filter.
(string)
TermsQuery
:
returns
this
so that calls can be chained
A custom routing value to be used when retrieving the external terms doc.
(string)
TermsQuery
:
returns
this
so that calls can be chained
Returns any documents that match with at least one or more of the provided terms. The terms are not analyzed and thus must match exactly. The number of terms that must match varies per document and is either controlled by a minimum should match field or computed per document in a minimum should match script.
NOTE: This query was added in elasticsearch v6.1.
Extends Query
const qry = esb.termsSetQuery('codes', ['abc', 'def', 'ghi'])
.minimumShouldMatchField('required_matches')
Sets the field to search on.
(string)
TermsSetQuery
:
returns
this
so that calls can be chained.
Append given term to set of terms to run Terms Set Query with.
TermsSetQuery
:
returns
this
so that calls can be chained
Specifies the terms to run query for.
TermsSetQuery
:
returns
this
so that calls can be chained
terms
is not an instance of Array
Controls the number of terms that must match per document.
(string)
TermsSetQuery
:
returns
this
so that calls can be chained
Sets the script
for query. It controls how many terms are required to
match in a more dynamic way.
The params.num_terms
parameter is available in the script to indicate
the number of terms that have been specified.
ScriptQuery
:
returns
this
so that calls can be chained.
const qry = esb.termsSetQuery('codes', ['abc', 'def', 'ghi'])
.minimumShouldMatchScript({
source: "Math.min(params.num_terms, doc['required_matches'].value)"
})
Interface-like class used to group and identify various implementations of multi term queries:
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.
Extends ValueTermQueryBase
Matches documents with fields that have terms within a certain range.
Extends MultiTermQueryBase
(string?)
const qry = esb.rangeQuery('age')
.gte(10)
.lte(20)
.boost(2.0);
const qry = esb.rangeQuery('date').gte('now-1d/d').lt('now/d');
Greater-than or equal to
RangeQuery
:
returns
this
so that calls can be chained.
Less-than or equal to
RangeQuery
:
returns
this
so that calls can be chained.
Greater-than
RangeQuery
:
returns
this
so that calls can be chained.
Less-than
RangeQuery
:
returns
this
so that calls can be chained.
The lower bound. Defaults to start from the first.
RangeQuery
:
returns
this
so that calls can be chained.
The upper bound. Defaults to unbounded.
RangeQuery
:
returns
this
so that calls can be chained.
Should the first from (if set) be inclusive or not. Defaults to true
RangeQuery
:
returns
this
so that calls can be chained.
Should the last to (if set) be inclusive or not. Defaults to true
.
RangeQuery
:
returns
this
so that calls can be chained.
Time Zone to be applied to any range query related to dates.
(string)
RangeQuery
:
returns
this
so that calls can be chained.
Sets the format expression for parsing the upper and lower bounds. If no format is specified, then it will use the first format specified in the field mapping.
(string)
Format for parsing upper and lower bounds.
RangeQuery
:
returns
this
so that calls can be chained
const qry = esb.rangeQuery('born')
.gte('01/01/2012')
.lte('2013')
.format('dd/MM/yyyy||yyyy');
Sets the relationship between Query and indexed data that will be used to determine if a Document should be matched or not.
RangeQuery
:
returns
this
so that calls can be chained
Returns documents that have at least one non-null
value in the original field
Extends Query
(string?)
const qry = esb.existsQuery('user');
const qry = esb.boolQuery().mustNot(esb.existsQuery('user'));
Sets the field to search on.
(string)
ExistsQuery
:
returns
this
so that calls can be chained.
Matches documents that have fields containing terms with a specified prefix (not analyzed).
Extends MultiTermQueryBase
const qry = esb.prefixQuery('user', 'ki').boost(2.0);
Sets the rewrite method. Valid values are:
constant_score
- tries to pick the best constant-score rewrite
method based on term and document counts from the query.
Synonyms - constant_score_auto
, constant_score_filter
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
.
This is an advanced option, use with care.
Note: The deprecated multi term rewrite parameters constant_score_auto
,
constant_score_filter
(synonyms for constant_score
) have been removed
in elasticsearch 6.0.
(string)
The rewrite method as a string.
PrefixQuery
:
returns
this
so that calls can be chained.
rewrite
method is not valid.
Matches documents that have fields matching a wildcard expression (not analyzed).
Extends MultiTermQueryBase
const qry = esb.wildcardQuery('user', 'ki*y').boost(2.0);
Allow case insensitive matching or not (added in 7.10.0). Defaults to false.
(boolean)
RegexpQuery
:
returns
this
so that calls can be chained.
const qry = esb.wildcardQuery('user', 'ki*y')
.caseInsensitive(true);
Sets the rewrite method. Valid values are:
constant_score
- tries to pick the best constant-score rewrite
method based on term and document counts from the query.
Synonyms - constant_score_auto
, constant_score_filter
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
.
This is an advanced option, use with care.
Note: The deprecated multi term rewrite parameters constant_score_auto
,
constant_score_filter
(synonyms for constant_score
) have been removed
in elasticsearch 6.0.
(string)
The rewrite method as a string.
WildcardQuery
:
returns
this
so that calls can be chained.
rewrite
method is not valid.
Query for regular expression term queries. Elasticsearch will apply the regexp to the terms produced by the tokenizer for that field, and not to the original text of the field.
Extends MultiTermQueryBase
const qry = esb.regexpQuery('name.first', 's.*y').boost(1.2);
Set special flags. Possible flags are ALL
(default),
ANYSTRING
, COMPLEMENT
, EMPTY
, INTERSECTION
, INTERVAL
, or NONE
.
(string)
|
separated flags. Possible flags are
ALL
(default),
ANYSTRING
,
COMPLEMENT
,
EMPTY
,
INTERSECTION
,
INTERVAL
, or
NONE
.
RegexpQuery
:
returns
this
so that calls can be chained.
const qry = esb.regexpQuery('name.first', 's.*y')
.flags('INTERSECTION|COMPLEMENT|EMPTY');
Allow case insensitive matching or not (added in 7.10.0). Defaults to false.
(boolean)
RegexpQuery
:
returns
this
so that calls can be chained.
const qry = esb.regexpQuery('name.first', 's.*y')
.caseInsensitive(true);
Limit on how many automaton states regexp queries are allowed to create. This protects against too-difficult (e.g. exponentially hard) regexps. Defaults to 10000.
(number)
RegexpQuery
:
returns
this
so that calls can be chained.
const qry = esb.regexpQuery('name.first', 's.*y')
.flags('INTERSECTION|COMPLEMENT|EMPTY')
.maxDeterminizedStates(20000);
Sets the rewrite method. Valid values are:
constant_score
- tries to pick the best constant-score rewrite
method based on term and document counts from the query.
Synonyms - constant_score_auto
, constant_score_filter
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
.
This is an advanced option, use with care.
Note: The deprecated multi term rewrite parameters constant_score_auto
,
constant_score_filter
(synonyms for constant_score
) have been removed
in elasticsearch 6.0.
(string)
The rewrite method as a string.
RegexpQuery
:
returns
this
so that calls can be chained.
rewrite
method is not valid.
The fuzzy query generates all possible matching terms that are within
the maximum edit distance specified in fuzziness
and then checks
the term dictionary to find out which of those generated terms
actually exist in the index.
The fuzzy query uses similarity based on Levenshtein edit distance.
Extends MultiTermQueryBase
const qry = esb.fuzzyQuery('user', 'ki');
// More advanced settings
const qry = esb.fuzzyQuery('user', 'ki')
.fuzziness(2)
.prefixLength(0)
.maxExpansions(100)
.boost(1.0);
Sets the fuzziness
parameter which is interpreted as a Levenshtein Edit Distance —
the number of one character changes that need to be made to one string to make it
the same as another string.
FuzzyQuery
:
returns
this
so that calls can be chained.
The number of initial characters which will not be “fuzzified”.
This helps to reduce the number of terms which must be examined. Defaults to 0
.
FuzzyQuery
:
returns
this
so that calls can be chained.
The maximum number of terms that the fuzzy query will expand to. Defaults to 50
.
FuzzyQuery
:
returns
this
so that calls can be chained.
Transpositions (ab
→ ba
) are allowed by default but can be disabled
by setting transpositions
to false.
(boolean)
FuzzyQuery
:
returns
this
so that calls can be chained.
Filters documents matching the provided document / mapping type.
Extends Query
(string?)
The elasticsearch doc type
const qry = esb.typeQuery('my_type');
Filters documents that only have the provided ids. Note, this query uses the _uid field.
Extends Query
const qry = esb.idsQuery('my_type', ['1', '4', '100']);
A query that wraps another query and simply returns a constant score
equal to the query boost for every document in the filter.
Maps to Lucene ConstantScoreQuery
.
Constructs a query where each documents returned by the internal query or filter have a constant score equal to the boost factor.
Extends Query
(Query?)
Query to filter on.
const qry = esb.constantScoreQuery(esb.termQuery('user', 'kimchy')).boost(1.2);
Adds the query to apply a constant score to.
(Query)
Query to filter on.
ConstantScoreQuery
:
returns
this
so that calls can be chained.
Adds the query to apply a constant score to.
Alias for method filter
.
Note: This parameter has been removed in elasticsearch 6.0. Use filter
instead.
(Query)
Query to filter on.
ConstantScoreQuery
:
returns
this
so that calls can be chained.
A query that matches documents matching boolean combinations of other queries.
The bool query maps to Lucene BooleanQuery
. It is built using one or more
boolean clauses, each clause with a typed occurrence.
Extends Query
const qry = esb.boolQuery()
.must(esb.termQuery('user', 'kimchy'))
.filter(esb.termQuery('tag', 'tech'))
.mustNot(esb.rangeQuery('age').gte(10).lte(20))
.should([
esb.termQuery('tag', 'wow'),
esb.termQuery('tag', 'elasticsearch')
])
.minimumShouldMatch(1)
.boost(1.0);
Adds filter
query to boolean container.
The clause (query) must appear in matching documents. However unlike must
the score
of the query will be ignored. Filter clauses are executed in filter context, meaning that
scoring is ignored and clauses are considered for caching.
BoolQuery
:
returns
this
so that calls can be chained.
Query
// Assign score of `0` to all documents
const qry = esb.boolQuery().filter(esb.termQuery('status', 'active'));
// Assign a score of `1.0` to all documents
const qry = esb.boolQuery()
.must(esb.matchAllQuery())
.filter(esb.termQuery('status', 'active'));
Adds must_not
query to boolean container.
The clause (query) must not appear in the matching documents.
Clauses are executed in filter context meaning that scoring is ignored
and clauses are considered for caching. Because scoring is ignored,
a score of 0 for all documents is returned.
BoolQuery
:
returns
this
so that calls can be chained.
Query
Adds should
query to boolean container.
The clause (query) should appear in the matching document. In a boolean query with
no must or filter clauses, one or more should clauses must match a document.
The minimum number of should clauses to match can be set using the
minimum_should_match
parameter.
BoolQuery
:
returns
this
so that calls can be chained.
Query
Enables or disables similarity coordinate scoring of documents
commoning the CommonTermsQuery
. Default: false
.
NOTE: This has been removed in elasticsearch 6.0. If provided, it will be ignored and a deprecation warning will be issued.
(boolean)
BoolQuery
:
returns
this
so that calls can be chained.
Sets the value controlling how many should
clauses in the boolean
query should match. It can be an absolute value (2), a percentage (30%)
or a combination of both. By default no optional clauses are necessary for a match.
However, if the bool query is used in a filter context and it has should
clauses then,
at least one should
clause is required to match.
BoolQuery
:
returns
this
so that calls can be chained.
A query that generates the union of documents produced by its subqueries, and that scores each document with the maximum score for that document as produced by any subquery, plus a tie breaking increment for any additional matching subqueries.
Extends Query
const qry = esb.disMaxQuery()
.queries([esb.termQuery('age', 34), esb.termQuery('age', 35)])
.tieBreaker(0.7)
.boost(1.2);
const qry = esb.disMaxQuery()
.queries([
esb.matchQuery('subject', 'brown fox'),
esb.matchQuery('message', 'brown fox')
])
.tieBreaker(0.3);
The tie breaker value. The tie breaker capability allows results
that include the same term in multiple fields to be judged better than
results that include this term in only the best of those multiple
fields, without confusing this with the better case of two different
terms in the multiple fields. Default: 0.0
.
(number)
DisMaxQuery
:
returns
this
so that calls can be chained.
Add given query array or query to list of queries
DisMaxQuery
:
returns
this
so that calls can be chained.
The function_score
allows you to modify the score of documents that are
retrieved by a query. This can be useful if, for example, a score function
is computationally expensive and it is sufficient to compute the score on
a filtered set of documents.
Extends Query
// `function_score` with only one function
const qry = esb.functionScoreQuery()
.query(esb.matchAllQuery())
.function(esb.randomScoreFunction())
.boostMode('multiply')
.boost('5');
// Several functions combined
const qry = esb.functionScoreQuery()
.query(esb.matchAllQuery())
.functions([
esb.randomScoreFunction()
.filter(esb.matchQuery('test', 'bar'))
.weight(23),
esb.weightScoreFunction()
.filter(esb.matchQuery('test', 'cat'))
.weight(42)
])
.maxBoost(42)
.scoreMode('max')
.boostMode('multiply')
.minScore(42)
.boost('5');
// Combine decay functions
const qry = esb.functionScoreQuery()
.functions([
esb.decayScoreFunction('gauss', 'price').origin('0').scale('20'),
esb.decayScoreFunction('gauss', 'location')
.origin('11, 12')
.scale('2km')
])
.query(esb.matchQuery('properties', 'balcony'))
.scoreMode('multiply');
Sets the source query.
FunctionScoreQuery
:
returns
this
so that calls can be chained.
Controls the way the scores are combined.
FunctionScoreQuery
:
returns
this
so that calls can be chained.
Controls the way the query and function scores are combined.
FunctionScoreQuery
:
returns
this
so that calls can be chained.
Restricts new score to not exceed given limit. The default for max_boost
is FLT_MAX
.
(number)
FunctionScoreQuery
:
returns
this
so that calls can be chained.
Sets the minimum score limit for documents to be included in search result.
(number)
Minimum score threshold
FunctionScoreQuery
:
returns
this
so that calls can be chained.
Add a single score function to the list of existing functions.
FunctionScoreQuery
:
returns
this
so that calls can be chained.
Adds array of score functions to the list of existing functions.
FunctionScoreQuery
:
returns
this
so that calls can be chained.
The boosting query can be used to effectively demote results that match a given query. Unlike the "NOT" clause in bool query, this still selects documents that contain undesirable terms, but reduces their overall score.
Extends Query
const qry = esb.boostingQuery(
esb.termQuery('field1', 'value1'), // positiveQry
esb.termQuery('field2', 'value2'), // negativeQry
0.2 // negativeBoost
);
Sets the "master" query that determines which results are returned.
BoostingQuery
:
returns
this
so that calls can be chained.
Sets the query used to match documents in the positive
query that will be negatively boosted.
BoostingQuery
:
returns
this
so that calls can be chained.
Sets the negative boost value.
BoostingQuery
:
returns
this
so that calls can be chained.
The JoiningQueryBase
class provides support for common options used across
various joining 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.
Extends Query
Sets the nested query to be executed.
JoiningQueryBase
:
returns
this
so that calls can be chained.
Sets the scoring method.
Valid values are:
none
- no scoringmax
- the highest score of all matched child documents is usedmin
- the lowest score of all matched child documents is usedsum
- the sum the all the matched child documents is usedavg
- the default, the average of all matched child documents is used(string)
Can be one of
none
,
sum
,
min
,
max
,
avg
.
Defaults to
avg
for
NestedQuery
,
none
for
HasChildQuery
.
JoiningQueryBase
:
returns
this
so that calls can be chained.
const qry = esb.hasChildQuery(
esb.termQuery('tag', 'something'),
'blog_tag'
).scoreMode('min');
When set to true
will ignore an unmapped path
and will not match any
documents for this query. When set to false
(the default value) the query
will throw an exception if the path is not mapped.
JoiningQueryBase
:
returns
this
so that calls can be chained.
Sets the inner hits options
JoiningQueryBase
:
returns
this
so that calls can be chained.
Nested query allows to query nested objects. The query is executed against the nested objects / docs as if they were indexed as separate docs (they are, internally) and resulting in the root parent doc (or parent nested mapping).
Extends JoiningQueryBase
const qry = esb.nestedQuery()
.path('obj1')
.scoreMode('avg')
.query(
esb.boolQuery().must([
esb.matchQuery('obj1.name', 'blue'),
esb.rangeQuery('obj1.count').gt(5)
])
);
Sets the root context for the nested query.
(string)
NestedQuery
:
returns
this
so that calls can be chained.
The has_child
filter accepts a query and the child type to run against, and
results in parent documents that have child docs matching the query.
Extends JoiningQueryBase
// Scoring support
const qry = esb.hasChildQuery(
esb.termQuery('tag', 'something'),
'blog_tag'
).scoreMode('min');
// Sort by child documents' `click_count` field
const qry = esb.hasChildQuery()
.query(
esb.functionScoreQuery().function(
esb.scriptScoreFunction("_score * doc['click_count'].value")
)
)
.type('blog_tag')
.scoreMode('max');
Sets the child document type to search against.
Alias for method childType
.
(string)
A valid doc type name
HasChildQuery
:
returns
this
so that calls can be chained.
Sets the child document type to search against
(string)
A valid doc type name
HasChildQuery
:
returns
this
so that calls can be chained.
Specify the minimum number of children are required to match for the parent doc to be considered a match
HasChildQuery
:
returns
this
so that calls can be chained.
const qry = esb.hasChildQuery(esb.termQuery('tag', 'something'), 'blog_tag')
.minChildren(2)
.maxChildren(10)
.scoreMode('min');
Specify the maximum number of children are required to match for the parent doc to be considered a match
HasChildQuery
:
returns
this
so that calls can be chained.
const qry = esb.hasChildQuery(esb.termQuery('tag', 'something'), 'blog_tag')
.minChildren(2)
.maxChildren(10)
.scoreMode('min');
The has_parent
query accepts a query and a parent type. The query is
executed in the parent document space, which is specified by the parent
type. This query returns child documents which associated parents have
matched.
Extends JoiningQueryBase
const qry = esb.hasParentQuery(esb.termQuery('tag', 'something'), 'blog');
// Sorting tags by parent documents' `view_count` field
const qry = esb.hasParentQuery()
.parentType('blog')
.score(true)
.query(
esb.functionScoreQuery().function(
esb.scriptScoreFunction("_score * doc['view_count'].value")
)
);
Sets the child document type to search against
Alias for method parentType
(string)
A valid doc type name
HasParentQuery
:
returns
this
so that calls can be chained.
Sets the child document type to search against
(string)
A valid doc type name
HasParentQuery
:
returns
this
so that calls can be chained.
By default, scoring is false
which ignores the score from the parent document.
The score is in this case equal to the boost on the has_parent
query (Defaults to 1).
If the score is set to true
, then the score of the matching parent document is
aggregated into the child documents belonging to the matching parent document.
HasParentQuery
:
returns
this
so that calls can be chained.
const qry = esb.hasParentQuery(
esb.termQuery('tag', 'something'),
'blog'
).score(true);
The parent_id
query can be used to find child documents which belong to a particular parent.
Extends Query
const qry = esb.parentIdQuery('blog_tag', 1);
Sets the child type.
ParentIdQuery
:
returns
this
so that calls can be chained.
Sets the id.
ParentIdQuery
:
returns
this
so that calls can be chained.
When set to true
will ignore an unmapped path
and will not match any
documents for this query. When set to false
(the default value) the query
will throw an exception if the path is not mapped.
ParentIdQuery
:
returns
this
so that calls can be chained.
The GeoQueryBase
provides support for common options used across
various geo 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.
Extends Query
Sets the field to run the geo query on.
(string)
GeoQueryBase
:
returns
this
so that calls can be chained.
Sets the validation_method
parameter. Can be set to IGNORE_MALFORMED
to accept
geo points with invalid latitude or longitude, COERCE
to try and infer correct latitude
or longitude, or STRICT
(default is STRICT
).
Note: The ignore_malformed
and coerce
parameters have been removed
from geo_bounding_box
, geo_polygon
, and geo_distance
queries in
elasticsearch 6.0.
GeoQueryBase
:
returns
this
so that calls can be chained.
method
parameter is not one of
IGNORE_MALFORMED
,
COERCE
or
STRICT
Filter documents indexed using the geo_shape
type. Requires
the geo_shape
Mapping.
The geo_shape
query uses the same grid square representation as
the geo_shape
mapping to find documents that have a shape that
intersects with the query shape. It will also use the same PrefixTree
configuration as defined for the field mapping.
The query supports two ways of defining the query shape, either by providing a whole shape definition, or by referencing the name of a shape pre-indexed in another index.
Extends GeoQueryBase
(string?)
const geoQry = esb.geoShapeQuery('location')
.shape(esb.geoShape()
.type('envelope')
.coordinates([[13.0, 53.0], [14.0, 52.0]]))
.relation('within');
// Pre-indexed shape
const geoQry = esb.geoShapeQuery()
.field('location')
.indexedShape(esb.indexedShape()
.id('DEU')
.type('countries')
.index('shapes')
.path('location'))
Sets the shape definition for the geo query.
(GeoShape)
GeoShapeQuery
:
returns
this
so that calls can be chained.
shape
is not an instance of
GeoShape
Sets the reference name of a shape pre-indexed in another index.
(IndexedShape)
GeoShapeQuery
:
returns
this
so that calls can be chained.
shape
is not an instance of
IndexedShape
Sets the relationship between Query and indexed data that will be used to determine if a Document should be matched or not.
GeoShapeQuery
:
returns
this
so that calls can be chained
When set to true
will ignore an unmapped path
and will not match any
documents for this query. When set to false
(the default value) the query
will throw an exception if the path is not mapped.
GeoShapeQuery
:
returns
this
so that calls can be chained.
A query allowing to filter hits based on a point location using a bounding box.
Extends GeoQueryBase
(string?)
// Format of point in Geohash
const qry = esb.geoBoundingBoxQuery('pin.location')
.topLeft(esb.geoPoint().string('dr5r9ydj2y73'))
.bottomRight(esb.geoPoint().string('drj7teegpus6'));
// Format of point with lat lon as properties
const qry = esb.geoBoundingBoxQuery()
.field('pin.location')
.topLeft(esb.geoPoint()
.lat(40.73)
.lon(-74.1))
.bottomRight(esb.geoPoint()
.lat(40.10)
.lon(-71.12));
// Set bounding box values separately
const qry = esb.geoBoundingBoxQuery('pin.location')
.top(40.73)
.left(-74.1)
.bottom(40.01)
.right(-71.12);
Sets the top left coordinate for the Geo bounding box filter for querying documents
GeoBoundingBoxQuery
:
returns
this
so that calls can be chained.
Sets the bottom right coordinate for the Geo bounding box filter for querying documents
GeoBoundingBoxQuery
:
returns
this
so that calls can be chained.
Sets the top right coordinate for the Geo bounding box filter for querying documents
GeoBoundingBoxQuery
:
returns
this
so that calls can be chained.
Sets the bottom left coordinate for the Geo bounding box filter for querying documents
GeoBoundingBoxQuery
:
returns
this
so that calls can be chained.
Sets value for top of the bounding box.
(number)
GeoBoundingBoxQuery
:
returns
this
so that calls can be chained.
Sets value for left of the bounding box.
(number)
GeoBoundingBoxQuery
:
returns
this
so that calls can be chained.
Sets value for bottom of the bounding box.
(number)
GeoBoundingBoxQuery
:
returns
this
so that calls can be chained.
Sets value for right of the bounding box.
(number)
GeoBoundingBoxQuery
:
returns
this
so that calls can be chained.
Sets the type of execution for the bounding box query. The type of the bounding box execution by default is set to memory, which means in memory checks if the doc falls within the bounding box range. In some cases, an indexed option will perform faster (but note that the geo_point type must have lat and lon indexed in this case)
GeoBoundingBoxQuery
:
returns
this
so that calls can be chained.
const geoQry = esb.geoBoundingBoxQuery()
.field('pin.location')
.topLeft(esb.geoPoint()
.lat(40.73)
.lon(-74.1))
.bottomRight(esb.geoPoint()
.lat(40.10)
.lon(-71.12))
.type('indexed');
Filters documents that include only hits that exists within a specific distance from a geo point.
Extends GeoQueryBase
(string?)
(GeoPoint?)
Geo point used to measure and filter documents based on distance from it.
const qry = esb.geoDistanceQuery('pin.location', esb.geoPoint().lat(40).lon(-70))
.distance('12km');
const qry = esb.geoDistanceQuery()
.field('pin.location')
.distance('200km')
.geoPoint(esb.geoPoint().lat(40).lon(-70));
Sets the radius of the circle centred on the specified location. Points which fall into this circle are considered to be matches. The distance can be specified in various units.
GeoDistanceQuery
:
returns
this
so that calls can be chained.
Sets the distance calculation mode, arc
or plane
.
The arc
calculation is the more accurate.
The plane
is the faster but least accurate.
(string)
GeoDistanceQuery
:
returns
this
so that calls can be chained
type
is neither
plane
nor
arc
.
Sets the point to filter documents based on the distance from it.
(GeoPoint)
Geo point used to measure and filter documents based on distance from it.
GeoDistanceQuery
:
returns
this
so that calls can be chained
point
is not an instance of
GeoPoint
A query allowing to include hits that only fall within a polygon of points.
Extends GeoQueryBase
(string?)
const geoQry = esb.geoPolygonQuery('person.location')
.points([
{"lat" : 40, "lon" : -70},
{"lat" : 30, "lon" : -80},
{"lat" : 20, "lon" : -90}
]);
Sets the points which form the polygon.
Points can be instances of GeoPoint
, Object with lat
, lon
keys,
GeoJSON
array representation or string(geohash
/lat, lon
)
(Array<any>)
GeoPolygonQuery
:
returns
this
so that calls can be chained
points
parameter is not an instance of
Array
.
// Format in `[lon, lat]`
const qry = esb.geoPolygonQuery('person.location').points([
[-70, 40],
[-80, 30],
[-90, 20]
]);
// Format in lat,lon
const qry = esb.geoPolygonQuery('person.location').points([
'40, -70',
'30, -80',
'20, -90'
]);
// Geohash
const qry = esb.geoPolygonQuery('person.location').points([
'drn5x1g8cu2y',
'30, -80',
'20, -90'
]);
The More Like This Query (MLT Query) finds documents that are "like" a given set of documents. In order to do so, MLT selects a set of representative terms of these input documents, forms a query using these terms, executes the query and returns the results. The user controls the input documents, how the terms should be selected and how the query is formed.
Extends Query
// Ask for documents that are similar to a provided piece of text
const qry = esb.moreLikeThisQuery()
.fields(['title', 'description'])
.like('Once upon a time')
.minTermFreq(1)
.maxQueryTerms(12);
// Mixing texts with documents already existing in the index
const qry = esb.moreLikeThisQuery()
.fields(['title', 'description'])
.like({ _index: 'imdb', _type: 'movies', _id: '1' })
.like({ _index: 'imdb', _type: 'movies', _id: '2' })
.like('and potentially some more text here as well')
.minTermFreq(1)
.maxQueryTerms(12);
// Provide documents not present in the index
const qry = esb.moreLikeThisQuery()
.fields(['name.first', 'name.last'])
.like([
{
_index: 'marvel',
_type: 'quotes',
doc: {
name: { first: 'Ben', last: 'Grimm' },
tweet: "You got no idea what I'd... what I'd give to be invisible."
}
},
{ _index: 'marvel', _type: 'quotes', _id: '2' }
])
.minTermFreq(1)
.maxQueryTerms(12);
Sets the list of fields to fetch and analyze the text from. Defaults to
the _all
field for free text and to all possible fields for document inputs.
MoreLikeThisQuery
:
returns
this
so that calls can be chained
Sets the search clause for the query. It is the only required parameter of the MLT query
and follows a versatile syntax, in which the user can specify free form text and/or
a single or multiple documents (see examples above). The syntax to specify documents
is similar to the one used by the Multi GET API.
When specifying documents, the text is fetched from fields unless overridden
in each document request. The text is analyzed by the analyzer at the field,
but could also be overridden. The syntax to override the analyzer at the
field follows a similar syntax to the per_field_analyzer
parameter of the
Term Vectors API.
Additionally, to provide documents not necessarily present in the index,
artificial documents
are also supported.
If string or object is passed, it is appended to the list. If an array is passed, it replaces the existing list.
MoreLikeThisQuery
:
returns
this
so that calls can be chained
The unlike
parameter is used in conjunction with like
in order not to
select terms found in a chosen set of documents. In other words, we could ask
for documents like
: "Apple", but unlike
: "cake crumble tree".
The syntax is the same as like.
MoreLikeThisQuery
:
returns
this
so that calls can be chained
Sets the text to find documents like it.
Note: This parameter has been removed in elasticsearch 6.0. Use like
instead.
(string)
The text to find documents like it.
MoreLikeThisQuery
:
returns
this
so that calls can be chained
Sets the list of ids
for the documents with syntax similar to
the Multi GET API.
Note: This parameter has been removed in elasticsearch 6.0. Use like
instead.
MoreLikeThisQuery
:
returns
this
so that calls can be chained
Sets the list of docs
for the documents with syntax similar to
the Multi GET API.
Note: This parameter has been removed in elasticsearch 6.0. Use like
instead.
MoreLikeThisQuery
:
returns
this
so that calls can be chained
Sets the maximum number of query terms that will be selected.
Increasing this value gives greater accuracy at the expense of query execution speed.
Defaults to 25
.
(number)
The maximum number of query terms that will be selected.
MoreLikeThisQuery
:
returns
this
so that calls can be chained
Sets the minimum term frequency below which the terms will be ignored from the input document Defaults to 2.
(number)
MoreLikeThisQuery
:
returns
this
so that calls can be chained
Sets the minimum document frequency below which the terms will be ignored
from the input document. Defaults to 5
.
(number)
The minimum document frequency
MoreLikeThisQuery
:
returns
this
so that calls can be chained
Sets the maximum document frequency above which the terms will be ignored
from the input document. Defaults to unbounded (0
).
(number)
The minimum document frequency
MoreLikeThisQuery
:
returns
this
so that calls can be chained
Sets the minimum word length below which the terms will be ignored.
Defaults to 0
.
(number)
MoreLikeThisQuery
:
returns
this
so that calls can be chained
Sets the maximum word length above which the terms will be ignored.
Defaults to unbounded (0
).
(number)
MoreLikeThisQuery
:
returns
this
so that calls can be chained
Sets the array of stop words. Any word in this set is considered "uninteresting" and ignored.
MoreLikeThisQuery
:
returns
this
so that calls can be chained
Set the analyzer to control which analyzer will perform the analysis process on the text.
Defaults to the analyzer associated with the first field in fields
.
(string)
A valid text analyzer.
MoreLikeThisQuery
:
returns
this
so that calls can be chained.
Sets the value controlling how many should
clauses in the boolean
query should match. It can be an absolute value (2), a percentage (30%)
or a combination of both. (Defaults to "30%"
).
MoreLikeThisQuery
:
returns
this
so that calls can be chained.
Sets the boost factor to use when boosting terms.
Defaults to deactivated (0
).
(number)
A positive value to boost terms.
MoreLikeThisQuery
:
returns
this
so that calls can be chained.
Specifies whether the input documents should also be included in the
search results returned. Defaults to false
.
(boolean)
MoreLikeThisQuery
:
returns
this
so that calls can be chained.
A query allowing to define scripts as queries. They are typically used in a filter context.
Extends Query
(Script?)
const scriptQry = esb.scriptQuery(esb.script()
.lang('painless')
.inline("doc['num1'].value > 1"))
// Use in filter context
const qry = esb.boolQuery().must(scriptQry);
Sets the script
for query.
(Script)
ScriptQuery
:
returns
this
so that calls can be chained.
The percolate
query can be used to match queries stored in an index.
The percolate
query itself contains the document that will be used
as query to match with the stored queries.
Extends Query
(string?)
The type / mapping of the document being percolated.
const percolateQry = esb.percolateQuery('query', 'doctype')
.document({ message: 'A new bonsai tree in the office' });
const percolateQry = esb.percolateQuery()
.field('query')
.documentType('doctype')
.index('my-index')
.type('message')
.id('1')
.version(1);
Sets the field of type percolator
and that holds the indexed queries.
PercolateQuery
:
returns
this
so that calls can be chained.
Sets the type / mapping of the document being percolated.
Note: This param has been deprecated in elasticsearch 6.0. From 6.0 and
later, it is no longer required to specify the document_type
parameter.
(string)
The type / mapping of the document being percolated.
PercolateQuery
:
returns
this
so that calls can be chained.
Appends given source document to the list of source documents being percolated. Instead of specifying the source document being percolated, the source can also be retrieved from an already stored document.
(Object)
The source document being percolated.
PercolateQuery
:
returns
this
so that calls can be chained.
const qry = esb.percolateQuery('query', 'people')
.document({ name: 'Will Smith' });
Appends given source documents to the list of source documents being percolated. Instead of specifying the source documents being percolated, the source can also be retrieved from already stored documents.
PercolateQuery
:
returns
this
so that calls can be chained.
const qry = esb.percolateQuery('query', 'people')
.documents([{ name: 'Will Smith' }, { name: 'Willow Smith' }]);
Sets the index the document resides in. This is a required parameter if document
is not specified.
(string)
The index the document resides in.
PercolateQuery
:
returns
this
so that calls can be chained.
Sets the type of the document to fetch. This is a required parameter if document
is not specified.
(string)
The type of the document to fetch.
PercolateQuery
:
returns
this
so that calls can be chained.
Sets the id of the document to fetch. This is a required parameter if document
is not specified.
(string)
The id of the document to fetch.
PercolateQuery
:
returns
this
so that calls can be chained.
Sets the routing to be used to fetch document to percolate. Optional.
(string)
The routing to be used to fetch document to percolate.
PercolateQuery
:
returns
this
so that calls can be chained.
Sets the preference to be used to fetch document to percolate. Optional.
(string)
The preference to be used to fetch document to percolate.
PercolateQuery
:
returns
this
so that calls can be chained.
Sets the expected version of the document to be fetched. Optional. If the version does not match, the search request will fail with a version conflict error.
(string)
The expected version of the document to be fetched.
PercolateQuery
:
returns
this
so that calls can be chained.
Interface-like class used to group and identify various implementations of Span queries.
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.
Extends Query
Matches spans containing a term. The span term query maps to Lucene SpanTermQuery
.
Extends SpanQueryBase
const qry = esb.spanTermQuery('user', 'kimchy');
const qry = esb.spanTermQuery()
.field('user')
.value('kimchy')
.boost(2.0);
Sets the field to search on.
(string)
SpanTermQuery
:
returns
this
so that calls can be chained.
Sets the query string.
SpanTermQuery
:
returns
this
so that calls can be chained.
The span_multi
query allows you to wrap a multi term query
(one of wildcard,
fuzzy, prefix, range or regexp query) as a span query
, so it can be nested.
Extends SpanQueryBase
(MultiTermQueryBase?)
One of wildcard, fuzzy, prefix, range or regexp query
const spanQry = esb.spanMultiTermQuery()
.match(esb.prefixQuery('user', 'ki').boost(1.08));
Sets the multi term query.
(MultiTermQueryBase)
One of wildcard, fuzzy, prefix, range or regexp query
SpanMultiTermQuery
:
returns
this
so that calls can be chained.
Matches spans near the beginning of a field. The span first query maps to Lucene SpanFirstQuery
.
Extends SpanQueryBase
(SpanQueryBase?)
Any other span type query
const spanQry = esb.spanFirstQuery()
.match(esb.spanTermQuery('user', 'kimchy'))
.end(3);
Sets the match
clause which can be any other span type query.
(SpanQueryBase)
SpanFirstQuery
:
returns
this
so that calls can be chained.
Sets the maximum end position permitted in a match.
(number)
The maximum end position permitted in a match.
SpanFirstQuery
:
returns
this
so that calls can be chained.
Matches spans which are near one another. One can specify slop
, the maximum
number of intervening unmatched positions, as well as whether matches are
required to be in-order. The span near query maps to Lucene SpanNearQuery
.
Extends SpanQueryBase
const spanQry = esb.spanNearQuery()
.clauses([
esb.spanTermQuery('field', 'value1'),
esb.spanTermQuery('field', 'value2'),
esb.spanTermQuery('field', 'value3')
])
.slop(12)
.inOrder(false);
Sets the clauses element which is a list of one or more other span type queries.
(Array<SpanQueryBase>)
SpanNearQuery
:
returns
this
so that calls can be chained.
clauses
is not an instance of Array or if
any member of the array is not an instance of
SpanQueryBase
.
Configures the slop
(default is 0), the maximum number of intervening
unmatched positions permitted.
(number)
A positive integer value, defaults is 0.
SpanNearQuery
:
returns
this
so that calls can be chained.
(boolean)
SpanNearQuery
:
returns
this
so that calls can be chained.
Matches the union of its span clauses. The span or query maps to Lucene SpanOrQuery
.
Extends SpanQueryBase
const spanQry = esb.spanOrQuery()
.clauses([
esb.spanTermQuery('field', 'value1'),
esb.spanTermQuery('field', 'value2'),
esb.spanTermQuery('field', 'value3')
]);
Sets the clauses element which is a list of one or more other span type queries.
(Array<SpanQueryBase>)
SpanOrQuery
:
returns
this
so that calls can be chained.
clauses
is not an instance of Array or if
any member of the array is not an instance of
SpanQueryBase
.
Removes matches which overlap with another span query. The span not query
maps to Lucene SpanNotQuery
.
Extends SpanQueryBase
const spanQry = esb.spanNotQuery()
.include(esb.spanTermQuery('field1', 'hoya'))
.exclude(esb.spanNearQuery()
.clauses([
esb.spanTermQuery('field1', 'la'),
esb.spanTermQuery('field1', 'hoya')
])
.slop(0)
.inOrder(true));
Sets the include
clause which is the span query whose matches are filtered
(SpanQueryBase)
SpanNotQuery
:
returns
this
so that calls can be chained.
Sets the exclude
clause which is the span query whose matches must
not overlap those returned.
(SpanQueryBase)
SpanNotQuery
:
returns
this
so that calls can be chained.
If set the amount of tokens before the include span can't have overlap with the exclude span.
(number)
SpanNotQuery
:
returns
this
so that calls can be chained.
If set the amount of tokens after the include span can't have overlap with the exclude span.
(number)
SpanNotQuery
:
returns
this
so that calls can be chained.
If set the amount of tokens from within the include span can't have overlap
with the exclude span. Equivalent of setting both pre
and post
.
(number)
SpanNotQuery
:
returns
this
so that calls can be chained.
Base class for span queries with little
, big
clauses.
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.
Extends SpanQueryBase
Sets the little
clause.
(SpanQueryBase)
Any span type query
SpanLittleBigQueryBase
:
returns
this
so that calls can be chained.
Sets the big
clause.
(SpanQueryBase)
Any span type query
SpanLittleBigQueryBase
:
returns
this
so that calls can be chained.
Returns matches which enclose another span query. The span containing query
maps to Lucene SpanContainingQuery
.
Matching spans from big that contain matches from little are returned.
Extends SpanLittleBigQueryBase
const spanQry = esb.spanContainingQuery()
.little(esb.spanTermQuery('field1', 'foo'))
.big(esb.spanNearQuery()
.clauses([
esb.spanTermQuery('field1', 'bar'),
esb.spanTermQuery('field1', 'baz')
])
.slop(5)
.inOrder(true))
Returns matches which are enclosed inside another span query. The span within
query maps to Lucene SpanWithinQuery
.
Matching spans from little
that are enclosed within big
are returned.
Extends SpanLittleBigQueryBase
const spanQry = esb.spanWithinQuery()
.little(esb.spanTermQuery('field1', 'foo'))
.big(esb.spanNearQuery()
.clauses([
esb.spanTermQuery('field1', 'bar'),
esb.spanTermQuery('field1', 'baz')
])
.slop(5)
.inOrder(true));
Wrapper to allow span queries to participate in composite single-field
span queries by lying about their search field. The span field masking
query maps to Lucene's SpanFieldMaskingQuery
.
This can be used to support queries like span-near or span-or across different fields, which is not ordinarily permitted.
Span field masking query is invaluable in conjunction with multi-fields when same content is indexed with multiple analyzers. For instance we could index a field with the standard analyzer which breaks text up into words, and again with the english analyzer which stems words into their root form.
Extends SpanQueryBase
(string?)
(SpanQueryBase?)
Any other span type query
const spanQry = esb.spanNearQuery()
.clauses([
esb.spanTermQuery('text', 'quick brown'),
esb.spanFieldMaskingQuery()
.field('text')
.query(esb.spanTermQuery('text.stems', 'fox'))
])
.slop(5)
.inOrder(false);
Sets the span query.
(SpanQueryBase)
SpanFieldMaskingQuery
:
returns
this
so that calls can be chained.
Sets the field to mask.
(string)
SpanFieldMaskingQuery
:
returns
this
so that calls can be chained.
These classes can be used to leverage the aggregations framework which helps provide aggregated data based on a search query. They can be composed together with queries and other aggregations in order to build complex summaries of the data.
These classes can be used to leverage the aggregations framework which helps provide aggregated data based on a search query. They can be composed together with queries and other aggregations in order to build complex summaries of the data.
Base class implementation for all aggregation types.
NOTE: Instantiating this directly should not be required. However, if you wish to add a custom implementation for whatever reason, this class should be extended and used, as validation against the class type is present in various places.
Sets name for aggregation.
Aggregation
:
Sets nested aggregations. This method can be called multiple times in order to set multiple nested aggregations.
Aggregation
:
returns
this
so that calls can be chained.
agg
is not an instance of
Aggregation
Sets nested aggregation. This method can be called multiple times in order to set multiple nested aggregations.
Aggregation
:
returns
this
so that calls can be chained.
Sets multiple nested aggregation items. This method accepts an array to set multiple nested aggregations in one call.
Aggregation
:
returns
this
so that calls can be chained.
Sets multiple nested aggregation items.
Alias for method aggregations
Aggregation
:
returns
this
so that calls can be chained.
You can associate a piece of metadata with individual aggregations at request time that will be returned in place at response time.
(Object)
Aggregation
:
returns
this
so that calls can be chained.
The MetricsAggregationBase
provides support for common options used across
various metrics Aggregation
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.
Extends Aggregation
(string)
a valid aggregation name
(string)
type of aggregation
(string?)
The field to aggregate on
Sets field to run aggregation on.
(string)
a valid field name
MetricsAggregationBase
:
returns
this
so that calls can be chained
Sets script parameter for aggregation.
(Script)
MetricsAggregationBase
:
returns
this
so that calls can be chained
script
is not an instance of
Script
// Compute the average grade based on a script
const agg = esb.avgAggregation('avg_grade').script(
esb.script('inline', "doc['grade'].value").lang('painless')
);
// Value script, apply grade correction
const agg = esb.avgAggregation('avg_grade', 'grade').script(
esb.script('inline', '_value * params.correction')
.lang('painless')
.params({ correction: 1.2 })
);
Sets the missing parameter which defines how documents that are missing a value should be treated.
(string)
MetricsAggregationBase
:
returns
this
so that calls can be chained
const agg = esb.avgAggregation('avg_grade', 'grade').missing(10);
Sets the format expression if applicable.
(string)
Format mask to apply on aggregation response. Example: ####.00
MetricsAggregationBase
:
returns
this
so that calls can be chained
A single-value metrics aggregation that computes the average of numeric values that are extracted from the aggregated documents. These values can be extracted either from specific numeric fields in the documents, or be generated by a provided script.
Aggregation that computes the average of numeric values that are extracted from the aggregated documents.
Extends MetricsAggregationBase
(string)
The name which will be used to refer to this aggregation.
(string?)
The field to aggregate on
// Compute the average grade over all documents
const agg = esb.avgAggregation('avg_grade', 'grade');
// Compute the average grade based on a script
const agg = esb.avgAggregation('avg_grade').script(
esb.script('inline', "doc['grade'].value").lang('painless')
);
// Value script, apply grade correction
const agg = esb.avgAggregation('avg_grade', 'grade').script(
esb.script('inline', '_value * params.correction')
.lang('painless')
.params({ correction: 1.2 })
);
// Missing value
const agg = esb.avgAggregation('avg_grade', 'grade').missing(10);
A single-value metrics aggregation that calculates an approximate count of distinct values. Values can be extracted either from specific fields in the document or generated by a script.
Aggregation that calculates an approximate count of distinct values.
Extends MetricsAggregationBase
(string)
The name which will be used to refer to this aggregation.
(string?)
The field to aggregate on
const agg = esb.cardinalityAggregation('author_count', 'author');
const agg = esb.cardinalityAggregation('author_count').script(
esb.script(
'inline',
"doc['author.first_name'].value + ' ' + doc['author.last_name'].value"
).lang('painless')
);
The precision_threshold
options allows to trade memory for accuracy,
and defines a unique count below which counts are expected to be close to accurate.
(number)
The threshold value.
The maximum supported value is 40000, thresholds above this number
will have the same effect as a threshold of 40000. The default values is 3000.
CardinalityAggregation
:
returns
this
so that calls can be chained
const agg = esb.cardinalityAggregation(
'author_count',
'author_hash'
).precisionThreshold(100);
A multi-value metrics aggregation that computes stats over numeric values extracted from the aggregated documents. These values can be extracted either from specific numeric fields in the documents, or be generated by a provided script.
Aggregation that computes extra stats over numeric values extracted from the aggregated documents.
Extends MetricsAggregationBase
(string)
The name which will be used to refer to this aggregation.
(string?)
The field to aggregate on
const agg = esb.extendedStatsAggregation('grades_stats', 'grade');
// Compute the grade stats based on a script
const agg = esb.extendedStatsAggregation('grades_stats').script(
esb.script('inline', "doc['grade'].value").lang('painless')
);
// Value script, apply grade correction
const agg = esb.extendedStatsAggregation('grades_stats', 'grade').script(
esb.script('inline', '_value * params.correction')
.lang('painless')
.params({ correction: 1.2 })
);
// Missing value
const agg = esb.extendedStatsAggregation('grades_stats', 'grade').missing(0);
Set sigma in the request for getting custom boundary. sigma controls how many standard deviations +/- from the mean should be displayed
(number)
sigma can be any non-negative double,
meaning you can request non-integer values such as 1.5.
A value of 0 is valid, but will simply return the average for both upper and lower bounds.
ExtendedStatsAggregation
:
returns
this
so that calls can be chained
const agg = esb.extendedStatsAggregation('grades_stats', 'grade').sigma(3);
A metric aggregation that computes the bounding box containing all geo_point values for a field.
Extends MetricsAggregationBase
(string)
The name which will be used to refer to this aggregation.
(string?)
The field to aggregate on
const agg = esb.geoBoundsAggregation('viewport', 'location').wrapLongitude(true);
(boolean)
Optional parameter which specifies whether
the bounding box should be allowed to overlap the international date line.
The default value is true
GeoBoundsAggregation
:
returns
this
so that calls can be chained
A metric aggregation that computes the weighted centroid from all coordinate values for a Geo-point datatype field.
Extends MetricsAggregationBase
(string)
The name which will be used to refer to this aggregation.
(string?)
The field to aggregate on. field must be a Geo-point datatype type
const agg = esb.geoCentroidAggregation('centroid', 'location');
// Combined as a sub-aggregation to other bucket aggregations
const reqBody = esb.requestBodySearch()
.query(esb.matchQuery('crime', 'burglary'))
.agg(
esb.termsAggregation('towns', 'town').agg(
esb.geoCentroidAggregation('centroid', 'location')
)
);
A single-value metrics aggregation that keeps track and returns the maximum value among the numeric values extracted from the aggregated documents. These values can be extracted either from specific numeric fields in the documents, or be generated by a provided script.
Aggregation that keeps track and returns the maximum value among the numeric values extracted from the aggregated documents.
Extends MetricsAggregationBase
(string)
The name which will be used to refer to this aggregation.
(string?)
The field to aggregate on
const agg = esb.maxAggregation('max_price', 'price');
// Use a file script
const agg = esb.maxAggregation('max_price').script(
esb.script('file', 'my_script').params({ field: 'price' })
);
// Value script to apply the conversion rate to every value
// before it is aggregated
const agg = esb.maxAggregation('max_price').script(
esb.script('inline', '_value * params.conversion_rate').params({
conversion_rate: 1.2
})
);
A single-value metrics aggregation that keeps track and returns the minimum value among the numeric values extracted from the aggregated documents. These values can be extracted either from specific numeric fields in the documents, or be generated by a provided script.
Aggregation that keeps track and returns the minimum value among numeric values extracted from the aggregated documents.
Extends MetricsAggregationBase
(string)
The name which will be used to refer to this aggregation.
(string?)
The field to aggregate on
const agg = esb.minAggregation('min_price', 'price');
// Use a file script
const agg = esb.minAggregation('min_price').script(
esb.script('file', 'my_script').params({ field: 'price' })
);
// Value script to apply the conversion rate to every value
// before it is aggregated
const agg = esb.minAggregation('min_price').script(
esb.script('inline', '_value * params.conversion_rate').params({
conversion_rate: 1.2
})
);
A multi-value metrics aggregation that calculates one or more percentiles over numeric values extracted from the aggregated documents. These values can be extracted either from specific numeric fields in the documents, or be generated by a provided script.
Aggregation that calculates one or more percentiles over numeric values extracted from the aggregated documents.
Extends MetricsAggregationBase
(string)
The name which will be used to refer to this aggregation.
(string?)
The field to aggregate on
const agg = esb.percentilesAggregation('load_time_outlier', 'load_time');
// Convert load time from mills to seconds on-the-fly using script
const agg = esb.percentilesAggregation('load_time_outlier').script(
esb.script('inline', "doc['load_time'].value / params.timeUnit")
.lang('painless')
.params({ timeUnit: 1000 })
);
Enable the response to be returned as a keyed object where the key is the bucket interval.
(boolean)
To enable keyed response or not. True by default
PercentilesAggregation
:
returns
this
so that calls can be chained
// Return the ranges as an array rather than a hash
const agg = esb.percentilesAggregation('balance_outlier', 'balance').keyed(
false
);
Specifies the percents of interest. Requested percentiles must be a value between 0-100 inclusive
PercentilesAggregation
:
returns
this
so that calls can be chained
percents
is not an instance of Array
// Specify particular percentiles to calculate
const agg = esb.percentilesAggregation(
'load_time_outlier',
'load_time'
).percents([95, 99, 99.9]);
Compression controls memory usage and approximation error. The compression value limits the maximum number of nodes to 100 * compression. By increasing the compression value, you can increase the accuracy of your percentiles at the cost of more memory. Larger compression values also make the algorithm slower since the underlying tree data structure grows in size, resulting in more expensive operations. The default compression value is 100.
(number)
Parameter to balance memory utilization with estimation accuracy.
PercentilesAggregation
:
returns
this
so that calls can be chained
const agg = esb.percentilesAggregation(
'load_time_outlier',
'load_time'
).tdigest(200);
Compression controls memory usage and approximation error. The compression value limits the maximum number of nodes to 100 * compression. By increasing the compression value, you can increase the accuracy of your percentiles at the cost of more memory. Larger compression values also make the algorithm slower since the underlying tree data structure grows in size, resulting in more expensive operations. The default compression value is 100.
Alias for tdigest
(number)
Parameter to balance memory utilization with estimation accuracy.
PercentilesAggregation
:
returns
this
so that calls can be chained
const agg = esb.percentilesAggregation(
'load_time_outlier',
'load_time'
).compression(200);
HDR Histogram (High Dynamic Range Histogram) is an alternative implementation that can be useful when calculating percentiles for latency measurements as it can be faster than the t-digest implementation with the trade-off of a larger memory footprint.
The HDR Histogram can be used by specifying the method parameter in the request.
(number)
The resolution of values
for the histogram in number of significant digits
PercentilesAggregation
:
returns
this
so that calls can be chained
const agg = esb.percentilesAggregation('load_time_outlier', 'load_time')
.percents([95, 99, 99.9])
.hdr(3);
A multi-value metrics aggregation that calculates one or more percentile ranks over numeric values extracted from the aggregated documents. These values can be extracted either from specific numeric fields in the documents, or be generated by a provided script.
Aggregation that calculates one or more percentiles ranks over numeric values extracted from the aggregated documents.
Extends MetricsAggregationBase
(string)
The name which will be used to refer to this aggregation.
(string?)
The field to aggregate on. It must be a numeric field
(Array?)
Values to compute percentiles from.
values
is not an instance of Array
const agg = esb.percentileRanksAggregation(
'load_time_outlier',
'load_time',
[15, 30]
);
// Convert load time from mills to seconds on-the-fly using script
const agg = esb.percentileRanksAggregation('load_time_outlier')
.values([3, 5])
.script(
esb.script('inline', "doc['load_time'].value / params.timeUnit")
.lang('painless')
.params({ timeUnit: 1000 })
);
Enable the response to be returned as a keyed object where the key is the bucket interval.
(boolean)
To enable keyed response or not.
PercentilesRanksAggregation
:
returns
this
so that calls can be chained
// Return the ranges as an array rather than a hash
const agg = esb.percentileRanksAggregation('balance_outlier', 'balance')
.values([25000, 50000])
.keyed(false);
Specifies the values to compute percentiles from.
PercentileRanksAggregation
:
returns
this
so that calls can be chained
values
is not an instance of Array
Compression controls memory usage and approximation error. The compression value limits the maximum number of nodes to 100 * compression. By increasing the compression value, you can increase the accuracy of your percentiles at the cost of more memory. Larger compression values also make the algorithm slower since the underlying tree data structure grows in size, resulting in more expensive operations. The default compression value is 100.
(number)
Parameter to balance memory utilization with estimation accuracy.
PercentileRanksAggregation
:
returns
this
so that calls can be chained
Compression controls memory usage and approximation error. The compression value limits the maximum number of nodes to 100 * compression. By increasing the compression value, you can increase the accuracy of your percentiles at the cost of more memory. Larger compression values also make the algorithm slower since the underlying tree data structure grows in size, resulting in more expensive operations. The default compression value is 100.
Alias for tdigest
(number)
Parameter to balance memory utilization with estimation accuracy.
PercentileRanksAggregation
:
returns
this
so that calls can be chained
HDR Histogram (High Dynamic Range Histogram) is an alternative implementation that can be useful when calculating percentiles for latency measurements as it can be faster than the t-digest implementation with the trade-off of a larger memory footprint.
The HDR Histogram can be used by specifying the method parameter in the request.
(number)
The resolution of values
for the histogram in number of significant digits
PercentileRanksAggregation
:
returns
this
so that calls can be chained
const agg = esb.percentileRanksAggregation(
'load_time_outlier',
'load_time',
[15, 30]
).hdr(3);
A metric aggregation that executes using scripts to provide a metric output.
Aggregation that keeps track and returns the minimum value among numeric values extracted from the aggregated documents.
Extends MetricsAggregationBase
(string)
The name which will be used to refer to this aggregation.
const agg = esb.scriptedMetricAggregation('profit')
.initScript('params._agg.transactions = []')
.mapScript(
"params._agg.transactions.add(doc.type.value == 'sale' ? doc.amount.value : -1 * doc.amount.value)"
)
.combineScript(
'double profit = 0; for (t in params._agg.transactions) { profit += t } return profit'
)
.reduceScript(
'double profit = 0; for (a in params._aggs) { profit += a } return profit'
);
// Specify using file scripts
const agg = esb.scriptedMetricAggregation('profit')
.initScript(esb.script('file', 'my_init_script'))
.mapScript(esb.script('file', 'my_map_script'))
.combineScript(esb.script('file', 'my_combine_script'))
// script parameters for `init`, `map` and `combine` scripts must be
// specified in a global params object so that
// it can be shared between the scripts
.params({ field: 'amount', _agg: {} })
.reduceScript(esb.script('file', 'my_reduce_script'));
Sets the initialization script.
Executed prior to any collection of documents. Allows the aggregation to set up any initial state.
ScriptedMetricAggregation
:
returns
this
so that calls can be chained
Sets the map script. This is the only required script.
Executed once per document collected. If no combine_script is specified, the resulting state needs to be stored in an object named _agg.
ScriptedMetricAggregation
:
returns
this
so that calls can be chained
Sets the combine phase script.
Executed once on each shard after document collection is complete. Allows the aggregation to consolidate the state returned from each shard. If a combine_script is not provided the combine phase will return the aggregation variable.
ScriptedMetricAggregation
:
returns
this
so that calls can be chained
Sets the reduce phase script.
Executed once on the coordinating node after all shards have returned their results. The script is provided with access to a variable _aggs which is an array of the result of the combine_script on each shard. If a reduce_script is not provided the reduce phase will return the _aggs variable.
ScriptedMetricAggregation
:
returns
this
so that calls can be chained
Sets the params for scripts.
Optional object whose contents will be passed as variables to the init_script, map_script and combine_script
If you specify script parameters then you must specify "_agg": {}
.
ScriptedMetricAggregation
:
returns
this
so that calls can be chained
A multi-value metrics aggregation that computes stats over numeric values extracted from the aggregated documents. These values can be extracted either from specific numeric fields in the documents, or be generated by a provided script.
The stats that are returned consist of: min, max, sum, count and avg.
Aggregation that computes stats over numeric values extracted from the aggregated documents.
Extends MetricsAggregationBase
(string)
The name which will be used to refer to this aggregation.
(string?)
The field to aggregate on
const agg = esb.statsAggregation('grades_stats', 'grade');
// Use a file script
const agg = esb.statsAggregation('grades_stats').script(
esb.script('file', 'my_script').params({ field: 'price' })
);
// Value script to apply the conversion rate to every value
// before it is aggregated
const agg = esb.statsAggregation('grades_stats').script(
esb.script('inline', '_value * params.conversion_rate').params({
conversion_rate: 1.2
})
);
A single-value metrics aggregation that sums up numeric values that are extracted from the aggregated documents. These values can be extracted either from specific numeric fields in the documents, or be generated by a provided script.
Aggregation that sums up numeric values that are extracted from the aggregated documents.
Extends MetricsAggregationBase
(string)
The name which will be used to refer to this aggregation.
(string?)
The field to aggregate on
const reqBody = esb.requestBodySearch()
.query(esb.constantScoreQuery(esb.matchQuery('type', 'hat')))
.agg(esb.sumAggregation('hat_prices', 'price'));
// Script to fetch the sales price
const reqBody = esb.requestBodySearch()
.query(esb.constantScoreQuery(esb.matchQuery('type', 'hat')))
.agg(
esb.sumAggregation('hat_prices').script(
esb.script('inline', 'doc.price.value')
)
);
// Access the field value from the script using `_value`
const reqBody = esb.requestBodySearch()
.query(esb.constantScoreQuery(esb.matchQuery('type', 'hat')))
.agg(
esb.sumAggregation('square_hats', 'price').script(
esb.script('inline', '_value * _value')
)
);
// Treat documents missing price as if they had a value
const reqBody = esb.requestBodySearch()
.query(esb.constantScoreQuery(esb.matchQuery('type', 'hat')))
.agg(esb.sumAggregation('hat_prices', 'price').missing(100));
A top_hits
metric aggregator keeps track of the most relevant document being
aggregated. This aggregator is intended to be used as a sub aggregator, so that
the top matching documents can be aggregated per bucket.
top_hits
metric aggregator keeps track of the most relevant document being
aggregated.
Extends MetricsAggregationBase
(string)
The name which will be used to refer to this aggregation.
const reqBody = esb.requestBodySearch()
.agg(
esb.termsAggregation('top_tags', 'type')
.size(3)
.agg(
esb.topHitsAggregation('top_sales_hits')
.sort(esb.sort('date', 'desc'))
.source({ includes: ['date', 'price'] })
.size(1)
)
)
.size(0);
// Field collapsing(logically groups a result set into
// groups and per group returns top documents)
const reqBody = esb.requestBodySearch()
.query(esb.matchQuery('body', 'elections'))
.agg(
esb.termsAggregation('top-sites', 'domain')
.order('top_hit', 'desc')
.agg(esb.topHitsAggregation('top_tags_hits'))
.agg(
esb.maxAggregation('top_hit').script(
esb.script('inline', '_score')
)
)
);
Sets the offset for fetching result.
(number)
The offset from the first result you want to fetch.
TopHitsAggregation
:
returns
this
so that calls can be chained
Sets the maximum number of top matching hits to return per bucket.
(number)
The numer of aggregation entries to be returned per bucket.
TopHitsAggregation
:
returns
this
so that calls can be chained
How the top matching hits should be sorted. Allows to add sort on specific field.
The sort can be reversed as well. The sort is defined on a per field level,
with special field name for _score
to sort by score, and _doc
to sort by
index order.
(Sort)
How the top matching hits should be sorted.
TopHitsAggregation
:
returns
this
so that calls can be chained.
sort
is not an instance of
Sort
.
Allows to add multiple sort on specific fields. Each sort can be reversed as well. The sort is defined on a per field level, with special field name for _score to sort by score, and _doc to sort by index order.
TopHitsAggregation
:
returns
this
so that calls can be chained.
sorts
is not an instance of
Sort
.
Enables score computation and tracking during sorting. By default, sorting scores are not computed.
(boolean)
If scores should be computed and tracked. Defaults to false.
TopHitsAggregation
:
returns
this
so that calls can be chained
Enable/Disable returning version number for each hit.
(boolean)
true to enable, false to disable
TopHitsAggregation
:
returns
this
so that calls can be chained
Enable/Disable explanation of score for each hit.
(boolean)
true to enable, false to disable
TopHitsAggregation
:
returns
this
so that calls can be chained
Performs highlighting based on the Highlight
settings.
(Highlight)
TopHitsAggregation
:
returns
this
so that calls can be chained
Allows to control how the _source
field is returned with every hit.
You can turn off _source
retrieval by passing false
.
It also accepts one(string) or more wildcard(array) patterns to control
what parts of the _source
should be returned
An object can also be used to specify the wildcard patterns for includes
and excludes
.
TopHitsAggregation
:
returns
this
so that calls can be chained
The storedfields parameter is about fields that are explicitly marked as stored in the mapping. Selectively load specific stored fields for each document represented by a search hit using array of stored fields. An empty array will cause only the _id and _type for each hit to be returned. To disable the stored fields (and metadata fields) entirely use: '_none'
TopHitsAggregation
:
returns
this
so that calls can be chained
Computes a document property dynamically based on the supplied Script
.
TopHitsAggregation
:
returns
this
so that calls can be chained
Sets given dynamic document properties to be computed using supplied Script
s.
Object should have scriptFieldName
as key and script
as the value.
TopHitsAggregation
:
returns
this
so that calls can be chained
Allows to return the doc value representation of a field for each hit. Doc value fields can work on fields that are not stored.
TopHitsAggregation
:
returns
this
so that calls can be chained
A single-value metrics aggregation that counts the number of values that are extracted from the aggregated documents. These values can be extracted either from specific fields in the documents, or be generated by a provided script. Typically, this aggregator will be used in conjunction with other single-value aggregations.
Aggregation that counts the number of values that are extracted from the aggregated documents.
Extends MetricsAggregationBase
(string)
The name which will be used to refer to this aggregation.
(string?)
The field to aggregate on
const agg = esb.valueCountAggregation('types_count', 'type');
const agg = esb.valueCountAggregation('types_count').script(
esb.script('inline', "doc['type'].value")
);
The BucketAggregationBase
provides support for common options used across
various bucket Aggregation
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.
Extends Aggregation
(string)
a valid aggregation name
(string)
type of aggregation
(string?)
The field to aggregate on
Sets field to run aggregation on.
(string)
a valid field name
BucketAggregationBase
:
returns
this
so that calls can be chained
Sets script parameter for aggregation.
(Script)
BucketAggregationBase
:
returns
this
so that calls can be chained
script
is not an instance of
Script
// Generating the terms using a script
const agg = esb.termsAggregation('genres').script(
esb.script('file', 'my_script').params({ field: 'genre' })
);
// Value script
const agg = esb.termsAggregation('genres', 'genre').script(
esb.script('inline', "'Genre: ' +_value").lang('painless')
);
A bucket aggregation returning a form of adjacency matrix.
The request provides a collection of named filter expressions,
similar to the filters
aggregation request. Each bucket in the response
represents a non-empty cell in the matrix of intersecting filters.
Extends BucketAggregationBase
(string)
The name which will be used to refer to this aggregation.
const agg = esb.adjacencyMatrixAggregation('interactions').filters({
grpA: esb.termsQuery('accounts', ['hillary', 'sidney']),
grpB: esb.termsQuery('accounts', ['donald', 'mitt']),
grpC: esb.termsQuery('accounts', ['vladimir', 'nigel'])
});
Sets a named filter query.
(string)
Name for the filter.
(Query)
Query to filter on. Example - term query.
AdjacencyMatrixAggregation
:
returns
this
so that calls can be chained
filterQuery
is not an instance of
Query
Assigns filters to already added filters. Does not mix with anonymous filters. If anonymous filters are present, they will be overwritten.
(Object)
Object with multiple key value pairs
where filter name is the key and filter query is the value.
AdjacencyMatrixAggregation
:
returns
this
so that calls can be chained
filterQueries
is not an instance of object
Sets the separator
parameter to use a separator string other than
the default of the ampersand.
(string)
the string used to separate keys in intersections buckets
e.g. & character for keyed filters A and B would return an
intersection bucket named A&B
AdjacencyMatrixAggregation
:
returns
this
so that calls can be chained
A multi-bucket aggregation similar to the Date histogram aggregation except instead of providing an interval to use as the width of each bucket, a target number of buckets is provided indicating the number of buckets needed and the interval of the buckets is automatically chosen to best achieve that target. The number of buckets returned will always be less than or equal to this target number.
Extends BucketAggregationBase
(string)
The name which will be used to refer to this aggregation.
(string)
The field to aggregate on
(number)
Bucket count to generate histogram over.
const agg = esb.autoDateHistogramAggregation('sales_over_time', 'date', 15);
Sets the histogram bucket count. Buckets are generated based on this value.
(number)
Bucket count to generate histogram over.
AutoDateHistogramAggregation
:
returns
this
so that calls can be chained
The minimum_interval allows the caller to specify the minimum rounding interval that should be used. This can make the collection process more efficient, as the aggregation will not attempt to round at any interval lower than minimum_interval.
Accepted units: year, month, day, hour, minute, second
(string)
Minimum Rounding Interval Example: 'minute'
AutoDateHistogramAggregation
:
returns
this
so that calls can be chained
const agg = esb.autoDateHistogramAggregation(
'sales_over_time',
'date',
5
).minimumInterval('minute');
Sets the format expression for key_as_string
in response buckets.
If no format is specified, then it will use the first format specified in the field mapping.
(string)
Format mask to apply on aggregation response. Example: ####.00.
For Date Histograms, supports expressive
date format pattern
AutoDateHistogramAggregation
:
returns
this
so that calls can be chained
const agg = esb.autoDateHistogramAggregation(
'sales_over_time',
'date',
5
).format('yyyy-MM-dd');
Sets the missing parameter which defines how documents that are missing a value should be treated.
(string)
AutoDateHistogramAggregation
:
returns
this
so that calls can be chained
const agg = esb.autoDateHistogramAggregation('quantity', 'quantity', 10).missing(0);
Date-times are stored in Elasticsearch in UTC.
By default, all bucketing and rounding is also done in UTC.
The time_zone
parameter can be used to indicate that bucketing should use a different time zone.
Sets the date time zone
(string)
Time zone. Time zones may either be specified
as an ISO 8601 UTC offset (e.g. +01:00 or -08:00) or as a timezone id,
an identifier used in the TZ database like America/Los_Angeles.
AutoDateHistogramAggregation
:
returns
this
so that calls can be chained
const agg = esb.autoDateHistogramAggregation('by_day', 'date', 15).timeZone(
'-01:00'
);
A special single bucket aggregation that enables aggregating from buckets on parent document types to buckets on child documents.
This aggregation relies on the _parent
field in the mapping.
Extends BucketAggregationBase
(string)
The name which will be used to refer to this aggregation.
const reqBody = esb.requestBodySearch()
.agg(
esb.termsAggregation('top-tags', 'tags.keyword')
.size(10)
.agg(
esb.childrenAggregation('to-answers')
.type('answer')
.agg(
esb.termsAggregation(
'top-names',
'owner.display_name.keyword'
).size(10)
)
)
)
.size(0);
Sets the child type/mapping for aggregation.
(string)
The child type that the buckets in the parent space should be mapped to.
ChildrenAggregation
:
returns
this
so that calls can be chained
CompositeAggregation is a multi-bucket values source based aggregation that can be used to calculate unique composite values from source documents.
Unlike the other multi-bucket aggregation the composite aggregation can be used to paginate all buckets from a multi-level aggregation efficiently. This aggregation provides a way to stream all buckets of a specific aggregation similarly to what scroll does for documents.
Extends Aggregation
(string)
a valid aggregation name
const reqBody = esb.requestBodySearch()
.agg(
esb.compositeAggregation('my_buckets')
.sources(
esb.CompositeAggregation.termsValuesSource('product', 'product')
)
)
NOTE: This query was added in elasticsearch v6.1.
Specifies the Composite Aggregation values sources to use in the aggregation.
(...ValuesSourceBase)
CompositeAggregation
:
returns
this
so that calls can be chained
sources
is not an
instance of
ValuesSourceBase
const { CompositeAggregation } = esb;
const reqBody = esb.requestBodySearch()
.agg(
esb.compositeAggregation('my_buckets')
.sources(
CompositeAggregation.dateHistogramValuesSource(
'date',
'timestamp',
'1d'
),
CompositeAggregation.termsValuesSource('product', 'product')
)
);
Defines how many composite buckets should be returned. Each composite bucket is considered as a single bucket so setting a size of 10 will return the first 10 composite buckets created from the values source. The response contains the values for each composite bucket in an array containing the values extracted from each value source.
(number)
CompositeAggregation
:
returns
this
so that calls can be chained
The after
parameter can be used to retrieve the composite buckets that
are after the last composite buckets returned in a previous round.
(Object)
CompositeAggregation
:
returns
this
so that calls can be chained
const { CompositeAggregation } = esb;
const reqBody = esb.requestBodySearch().agg(
esb.compositeAggregation('my_buckets')
.size(2)
.sources(
CompositeAggregation.dateHistogramValuesSource(
'date',
'timestamp',
'1d'
).order('desc'),
CompositeAggregation.termsValuesSource('product', 'product').order('asc')
)
.after({ date: 1494288000000, product: 'mad max' })
);
The sources parameter controls the sources that should be used to build the composite buckets. There are three different types of values source:
The sources parameter controls the sources that should be used to build the composite buckets. There are three different types of values source:
Base class implementation for all Composite Aggregation values sources.
NOTE: Instantiating this directly should not be required.
(string)
Type of value source
(string)
Elasticsearch reference URL
(string)
(string?)
The field to aggregate on
Field to use for this source.
(string)
a valid field name
ValuesSourceBase
:
returns
this
so that calls can be chained
Script to use for this source.
ValuesSourceBase
:
returns
this
so that calls can be chained
script
is not an instance of
Script
Specifies the type of values produced by this source, e.g. string
or
date
.
(string)
ValuesSourceBase
:
returns
this
so that calls can be chained
Order specifies the order in the values produced by this source. It can
be either asc
or desc
.
(string)
The
order
option can have the following values.
asc
,
desc
to sort in ascending, descending order respectively.
ValuesSourceBase
:
returns
this
so that calls can be chained.
Missing specifies the value to use when the source finds a missing value in a document.
Note: This option was deprecated in
Elasticsearch v6.
From 6.4 and later, use missing_bucket
instead.
ValuesSourceBase
:
returns
this
so that calls can be chained
Specifies whether to include documents without a value for a given source
in the response. Defaults to false
(not included).
Note: This method is incompatible with elasticsearch 6.3 and older. Use it only with elasticsearch 6.4 and later.
(boolean)
ValuesSourceBase
:
returns
this
so that calls can be chained
TermsValuesSource
is a source for the CompositeAggregation
that handles
terms. It works very similar to a terms aggregation with a slightly different
syntax.
Extends ValuesSourceBase
const valueSrc = esb.CompositeAggregation.termsValuesSource('product').script({
source: "doc['product'].value",
lang: 'painless'
});
HistogramValuesSource
is a source for the CompositeAggregation
that handles
histograms. It works very similar to a histogram aggregation with a slightly
different syntax.
Extends ValuesSourceBase
(string)
(string?)
The field to aggregate on
(number?)
Interval to generate histogram over.
const valueSrc = esb.CompositeAggregation.histogramValuesSource(
'histo', // name
'price', // field
5 // interval
);
Sets the histogram interval. Buckets are generated based on this interval value.
(number)
Interval to generate histogram over.
HistogramValuesSource
:
returns
this
so that calls can be chained
DateHistogramValuesSource
is a source for the CompositeAggregation
that
handles date histograms. It works very similar to a histogram aggregation
with a slightly different syntax.
Extends ValuesSourceBase
const valueSrc = esb.CompositeAggregation.dateHistogramValuesSource(
'date', // name
'timestamp', // field
'1d' // interval
);
Sets the histogram interval. Buckets are generated based on this interval value.
DateHistogramValuesSource
:
returns
this
so that calls can be chained
Calendar-aware intervals are configured with the calendarInterval parameter. The combined interval field for date histograms is deprecated from ES 7.2.
(string)
Interval to generate histogram over.
You can specify calendar intervals using the unit name, such as month, or as
a single unit quantity, such as 1M. For example, day and 1d are equivalent.
Multiple quantities, such as 2d, are not supported.
DateHistogramValuesSource
:
returns
this
so that calls can be chained
const agg = esb.dateHistogramValuesSource('by_month', 'date').calendarInterval(
'month'
);
Fixed intervals are configured with the fixedInterval parameter. The combined interval field for date histograms is deprecated from ES 7.2.
(string)
Interval to generate histogram over.
Intervals are a fixed number of SI units and never deviate, regardless
of where they fall on the calendar. However, it means fixed intervals
cannot express other units such as months, since the duration of a
month is not a fixed quantity.
The accepted units for fixed intervals are:
millseconds (ms), seconds (s), minutes (m), hours (h) and days (d).
DateHistogramValuesSource
:
returns
this
so that calls can be chained
const agg = esb.dateHistogramValuesSource('by_minute', 'date').calendarInterval(
'60s'
);
Sets the date time zone
Date-times are stored in Elasticsearch in UTC. By default, all bucketing
and rounding is also done in UTC. The time_zone
parameter can be used
to indicate that bucketing should use a different time zone.
(string)
Time zone. Time zones may either be specified
as an ISO 8601 UTC offset (e.g. +01:00 or -08:00) or as a timezone id,
an identifier used in the TZ database like America/Los_Angeles.
DateHistogramValuesSource
:
returns
this
so that calls can be chained
Sets the format expression for key_as_string
in response buckets.
If no format is specified, then it will use the first format specified
in the field mapping.
(string)
Format mask to apply on aggregation response.
For Date Histograms, supports expressive
date format pattern
DateHistogramValuesSource
:
returns
this
so that calls can be chained
const valueSrc = esb.CompositeAggregation.valuesSource
.dateHistogram('date', 'timestamp', '1d')
.format('yyyy-MM-dd');
The HistogramAggregationBase
provides support for common options used across
various histogram Aggregation
implementations like Histogram Aggregation,
Date Histogram aggregation.
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.
Extends BucketAggregationBase
(string)
The name which will be used to refer to this aggregation.
(string)
Type of aggregation
(string?)
The field to aggregate on
Sets the histogram interval. Buckets are generated based on this interval value.
(string)
Interval to generate histogram over.
For date histograms, available expressions for interval:
year, quarter, month, week, day, hour, minute, second
HistogramAggregationBase
:
returns
this
so that calls can be chained
Sets the format expression for key_as_string
in response buckets.
If no format is specified, then it will use the first format specified in the field mapping.
(string)
Format mask to apply on aggregation response. Example: ####.00.
For Date Histograms, supports expressive
date format pattern
HistogramAggregationBase
:
returns
this
so that calls can be chained
const agg = esb.dateHistogramAggregation(
'sales_over_time',
'date',
'1M'
).format('yyyy-MM-dd');
The offset parameter is used to change the start value of each bucket by the specified positive (+) or negative offset (-). Negative offset is not applicable on HistogramAggregation. In case of DateHistogramAggregation, duration can be a value such as 1h for an hour, or 1d for a day.
(string)
Time or bucket key offset for bucketing.
HistogramAggregationBase
:
returns
this
so that calls can be chained
const agg = esb.dateHistogramAggregation('by_day', 'date', 'day').offset('6h');
Sets the ordering for buckets
HistogramAggregationBase
:
returns
this
so that calls can be chained
const agg = esb.histogramAggregation('prices', 'price', 50)
.order('_count', 'desc');
const agg = esb.histogramAggregation('prices', 'price', 50)
.order('promoted_products>rating_stats.avg', 'desc')
.agg(
esb.filterAggregation('promoted_products')
.filter(esb.termQuery('promoted', 'true'))
.agg(esb.statsAggregation('rating_stats', 'rating'))
);
Sets the minimum number of matching documents in range to return the bucket.
(number)
Integer value for minimum number of documents
required to return bucket in response
HistogramAggregationBase
:
returns
this
so that calls can be chained
const agg = esb.histogramAggregation('prices', 'price', 50).minDocCount(1);
Set's the range/bounds for the histogram aggregation. Useful when you want to include buckets that might be outside the bounds of indexed documents.
HistogramAggregationBase
:
returns
this
so that calls can be chained
const agg = esb.histogramAggregation('prices', 'price', 50).extendedBounds(0, 500);
Set's the range/bounds for the histogram aggregation. Useful when you want to limit the range of buckets in the histogram. It is particularly useful in the case of open data ranges that can result in a very large number of buckets. NOTE: Only available in Elasticsearch v7.10.0+
HistogramAggregationBase
:
returns
this
so that calls can be chained
const agg = esb.histogramAggregation('prices', 'price', 50).hardBounds(0, 500);
Sets the missing parameter which defines how documents that are missing a value should be treated.
(string)
HistogramAggregationBase
:
returns
this
so that calls can be chained
const agg = esb.histogramAggregation('quantity', 'quantity', 10).missing(0);
Enable the response to be returned as a keyed object where the key is the bucket interval.
(boolean)
To enable keyed response or not.
HistogramAggregationBase
:
returns
this
so that calls can be chained
const agg = esb.dateHistogramAggregation('sales_over_time', 'date', '1M')
.keyed(true)
.format('yyyy-MM-dd');
A multi-bucket aggregation similar to the histogram except it can only be applied on date values. The interval can be specified by date/time expressions.
Extends HistogramAggregationBase
(string)
The name which will be used to refer to this aggregation.
(string?)
The field to aggregate on
(string?)
Interval to generate histogram over.
Available expressions for interval: year, quarter, month, week, day, hour, minute, second
const agg = esb.dateHistogramAggregation('sales_over_time', 'date', 'month');
const agg = esb.dateHistogramAggregation(
'sales_over_time',
'date',
'1M'
).format('yyyy-MM-dd');
Date-times are stored in Elasticsearch in UTC.
By default, all bucketing and rounding is also done in UTC.
The time_zone
parameter can be used to indicate that bucketing should use a different time zone.
Sets the date time zone
(string)
Time zone. Time zones may either be specified
as an ISO 8601 UTC offset (e.g. +01:00 or -08:00) or as a timezone id,
an identifier used in the TZ database like America/Los_Angeles.
DateHistogramAggregation
:
returns
this
so that calls can be chained
const agg = esb.dateHistogramAggregation('by_day', 'date', 'day').timeZone(
'-01:00'
);
Calendar-aware intervals are configured with the calendarInterval parameter. The combined interval field for date histograms is deprecated from ES 7.2.
(string)
Interval to generate histogram over.
You can specify calendar intervals using the unit name, such as month, or as
a single unit quantity, such as 1M. For example, day and 1d are equivalent.
Multiple quantities, such as 2d, are not supported.
DateHistogramAggregation
:
returns
this
so that calls can be chained
const agg = esb.dateHistogramAggregation('by_month', 'date').calendarInterval(
'month'
);
Fixed intervals are configured with the fixedInterval parameter. The combined interval field for date histograms is deprecated from ES 7.2.
(string)
Interval to generate histogram over.
Intervals are a fixed number of SI units and never deviate, regardless
of where they fall on the calendar. However, it means fixed intervals
cannot express other units such as months, since the duration of a
month is not a fixed quantity.
DateHistogramAggregation
:
returns
this
so that calls can be chained
const agg = esb.dateHistogramAggregation('by_minute', 'date').calendarInterval(
'60s'
);
The accepted units for fixed intervals are:
millseconds (ms), seconds (s), minutes (m), hours (h) and days (d).
The RangeAggregationBase
provides support for common options used across
various range Aggregation
implementations like Range Aggregation and
Date Range aggregation.
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.
Extends BucketAggregationBase
(string)
The name which will be used to refer to this aggregation.
(string)
Type of aggregation
(string?)
The field to aggregate on
Sets the format expression for key_as_string
in response buckets.
If no format is specified, then it will use the format specified in the field mapping.
RangeAggregationBase
:
returns
this
so that calls can be chained
Adds a range to the list of existing range expressions.
RangeAggregationBase
:
returns
this
so that calls can be chained
Adds the list of ranges to the list of existing range expressions.
RangeAggregationBase
:
returns
this
so that calls can be chained
Sets the missing parameter ehich defines how documents that are missing a value should be treated.
(string)
RangeAggregationBase
:
returns
this
so that calls can be chained
Enable the response to be returned as a keyed object where the key is the bucket interval.
(boolean)
To enable keyed response or not.
RangeAggregationBase
:
returns
this
so that calls can be chained
const agg = esb.dateRangeAggregation('range', 'date')
.format('MM-yyy')
.ranges([{ to: 'now-10M/M' }, { from: 'now-10M/M' }])
.keyed(true);
const agg = esb.geoDistanceAggregation('rings_around_amsterdam', 'location')
.origin(esb.geoPoint().string('52.3760, 4.894'))
.ranges([
{ to: 100000, key: 'first_ring' },
{ from: 100000, to: 300000, key: 'second_ring' },
{ from: 300000, key: 'third_ring' }
])
.keyed(true);
A range aggregation that is dedicated for date values. The main difference between this aggregation and the normal range aggregation is that the from and to values can be expressed in Date Math expressions, and it is also possible to specify a date format by which the from and to response fields will be returned.
Extends RangeAggregationBase
(string)
The name which will be used to refer to this aggregation.
(string?)
The field to aggregate on
const agg = esb.dateRangeAggregation('range', 'date')
.format('MM-yyy')
.ranges([{ to: 'now-10M/M' }, { from: 'now-10M/M' }]);
Sets the date time zone.
Date-times are stored in Elasticsearch in UTC.
By default, all bucketing and rounding is also done in UTC.
The time_zone
parameter can be used to indicate that
bucketing should use a different time zone.
(string)
Time zone. Time zones may either be specified
as an ISO 8601 UTC offset (e.g. +01:00 or -08:00) or as a timezone id,
an identifier used in the TZ database like America/Los_Angeles.
DateRangeAggregation
:
returns
this
so that calls can be chained
const agg = esb.dateRangeAggregation('range', 'date')
.timeZone('CET')
.ranges([
{ to: '2016/02/01' },
{ from: '2016/02/01', to: 'now/d' },
{ from: 'now/d' }
]);
A filtering aggregation used to limit any sub aggregations' processing to a sample of the top-scoring documents. Diversity settings are used to limit the number of matches that share a common value such as an "author".
Extends BucketAggregationBase
(string)
The name which will be used to refer to this aggregation.
(string?)
The field to aggregate on
const reqBody = esb.requestBodySearch()
.query(esb.queryStringQuery('tags:elasticsearch'))
.agg(
esb.diversifiedSamplerAggregation('my_unbiased_sample', 'author')
.shardSize(200)
.agg(
esb.significantTermsAggregation(
'keywords',
'tags'
).exclude(['elasticsearch'])
)
);
// Use a script to produce a hash of the multiple values in a tags field
// to ensure we don't have a sample that consists of the same repeated
// combinations of tags
const reqBody = esb.requestBodySearch()
.query(esb.queryStringQuery('tags:kibana'))
.agg(
esb.diversifiedSamplerAggregation('my_unbiased_sample')
.shardSize(200)
.maxDocsPerValue(3)
.script(esb.script('inline', "doc['tags'].values.hashCode()"))
.agg(
esb.significantTermsAggregation(
'keywords',
'tags'
).exclude(['kibana'])
)
);
The shard_size parameter limits how many top-scoring documents are collected in the sample processed on each shard. The default value is 100.
(number)
Maximum number of documents to return from each shard(Integer)
DiversifiedSamplerAggregation
:
returns
this
so that calls can be chained
Used to control the maximum number of documents collected on any one shard which share a common value. Applies on a per-shard basis only for the purposes of shard-local sampling.
(number)
Default 1.(Integer)
DiversifiedSamplerAggregation
:
returns
this
so that calls can be chained
This setting can influence the management of the values used for de-duplication. Each option will hold up to shard_size values in memory while performing de-duplication but the type of value held can be controlled
(string)
the possible values are
map
,
global_ordinals
,
global_ordinals_hash
and
global_ordinals_low_cardinality
DiversifiedSamplerAggregation
:
returns
this
so that calls can be chained
Defines a single bucket of all the documents in the current document set context that match a specified filter. Often this will be used to narrow down the current aggregation context to a specific set of documents.
Extends BucketAggregationBase
(string)
The name which will be used to refer to this aggregation.
(Query?)
Query to filter on. Example - term query.
const reqBody = esb.requestBodySearch()
.agg(
esb.filterAggregation(
't_shirts',
esb.termQuery('type', 't-shirt')
).agg(esb.avgAggregation('avg_price', 'price'))
)
.size(0);
Set the filter query for Filter Aggregation.
(Query)
Query to filter on. Example - term query.
FilterAggregation
:
returns
this
so that calls can be chained
filterQuery
is not an instance of
Query
Defines a single bucket of all the documents in the current document set context that match a specified filter. Often this will be used to narrow down the current aggregation context to a specific set of documents.
Extends BucketAggregationBase
(string)
The name which will be used to refer to this aggregation.
const agg = esb.filtersAggregation('messages')
.filter('errors', esb.matchQuery('body', 'error'))
.filter('warnings', esb.matchQuery('body', 'warning'));
const agg = esb.filtersAggregation('messages')
.anonymousFilters([
esb.matchQuery('body', 'error'),
esb.matchQuery('body', 'warning')
])
Sets a named filter query. Does not mix with anonymous filters. If anonymous filters are present, they will be overwritten.
(string)
Name for bucket which will collect
all documents that match its associated filter.
(Query)
Query to filter on. Example - term query.
FiltersAggregation
:
returns
this
so that calls can be chained
filterQuery
is not an instance of
Query
Assigns filters to already added filters. Does not mix with anonymous filters. If anonymous filters are present, they will be overwritten.
(Object)
Object with multiple key value pairs
where bucket name is the key and filter query is the value.
FiltersAggregation
:
returns
this
so that calls can be chained
filterQueries
is not an instance of object
Appends an anonymous filter query. Does not mix with named filters. If named filters are present, they will be overwritten.
(any)
Query to filter on. Example - term query.
FiltersAggregation
:
returns
this
so that calls can be chained
filterQuery
is not an instance of
Query
Appends an array of anonymous filters. Does not mix with named filters. If named filters are present, they will be overwritten.
(any)
Array of queries to filter on and generate buckets.
Example - term query.
FiltersAggregation
:
returns
this
so that calls can be chained
filterQueries
is not an instance of Array
Adds a bucket to the response which will contain all documents
that do not match any of the given filters.
Returns the other bucket bucket either in a bucket
(named _other_
by default) if named filters are being used,
or as the last bucket if anonymous filters are being used
(boolean)
True
to return
other
bucket with documents
that do not match any filters and
False
to disable computation
FiltersAggregation
:
returns
this
so that calls can be chained
Sets the key for the other bucket to a value other than the default _other_
.
Setting this parameter will implicitly set the other_bucket parameter to true.
If anonymous filters are being used, setting this parameter will not make sense.
(string)
FiltersAggregation
:
returns
this
so that calls can be chained
const agg = esb.filtersAggregation('messages')
.filter('errors', esb.matchQuery('body', 'error'))
.filter('warnings', esb.matchQuery('body', 'warning'))
.otherBucketKey('other_messages');
A multi-bucket aggregation that works on geo_point fields and conceptually works very similar to the range aggregation. The user can define a point of origin and a set of distance range buckets. The aggregation evaluate the distance of each document value from the origin point and determines the buckets it belongs to based on the ranges (a document belongs to a bucket if the distance between the document and the origin falls within the distance range of the bucket).
Extends RangeAggregationBase
(string)
The name which will be used to refer to this aggregation.
(string?)
The field to aggregate on
const agg = esb.geoDistanceAggregation('rings_around_amsterdam', 'location')
.origin(esb.geoPoint().string('52.3760, 4.894'))
.ranges([{ to: 100000 }, { from: 100000, to: 300000 }, { from: 300000 }]);
Sets the point of origin from where distances will be measured.
GeoDistanceAggregation
:
returns
this
so that calls can be chained
point
is not an instance of
GeoPoint
Sets the distance unit. Valid values are: mi (miles), in (inches), yd (yards), km (kilometers), cm (centimeters), mm (millimeters), ft(feet), NM(nauticalmiles)
GeoDistanceAggregation
:
returns
this
so that calls can be chained
const agg = esb.geoDistanceAggregation('rings_around_amsterdam', 'location')
.origin(esb.geoPoint().string('52.3760, 4.894'))
.unit('km')
.ranges([{ to: 100 }, { from: 100, to: 300 }, { from: 300 }]);
Sets the distance calculation mode, arc
or plane
.
The arc
calculation is the more accurate.
The plane
is the faster but least accurate.
(string)
GeoDistanceAggregation
:
returns
this
so that calls can be chained
type
is neither
plane
nor
arc
.
const agg = esb.geoDistanceAggregation('rings_around_amsterdam', 'location')
.origin(esb.geoPoint().string('52.3760, 4.894'))
.unit('km')
.distanceType('plane')
.ranges([{ to: 100 }, { from: 100, to: 300 }, { from: 300 }]);
A multi-bucket aggregation that works on geo_point fields and groups points into buckets that represent cells in a grid. The resulting grid can be sparse and only contains cells that have matching data. Each cell is labeled using a geohash which is of user-definable precision.
Extends BucketAggregationBase
(string)
The name which will be used to refer to this aggregation.
(string?)
The field to aggregate on
const agg = esb.geoHashGridAggregation('large-grid', 'location').precision(3);
Sets the precision for the generated geohash.
(number)
Precision can be between 1 and 12
GeoHashGridAggregation
:
returns
this
so that calls can be chained
Sets the maximum number of geohash buckets to return. When results are trimmed, buckets are prioritised based on the volumes of documents they contain.
(number)
Optional. The maximum number of geohash
buckets to return (defaults to 10,000).
GeoHashGridAggregation
:
returns
this
so that calls can be chained
Determines how many geohash_grid the coordinating node will request from each shard.
(number)
Optional.
GeoHashGridAggregation
:
returns
this
so that calls can be chained
Defines a single bucket of all the documents within the search execution context. This context is defined by the indices and the document types you’re searching on, but is not influenced by the search query itself.
Extends BucketAggregationBase
(string)
The name which will be used to refer to this aggregation.
const reqBody = esb.requestBodySearch()
.query(esb.matchQuery('type', 't-shirt'))
.agg(
esb.globalAggregation('all_products').agg(
esb.avgAggregation('avg_price', 'price')
)
)
.agg(esb.avgAggregation('t_shirts', 'price'));
A multi-bucket values source based aggregation that can be applied on numeric values extracted from the documents. It dynamically builds fixed size (a.k.a. interval) buckets over the values.
Extends HistogramAggregationBase
(string)
The name which will be used to refer to this aggregation.
(string?)
The field to aggregate on
(number?)
Interval to generate histogram over.
const agg = esb.histogramAggregation('prices', 'price', 50);
const agg = esb.histogramAggregation('prices', 'price', 50).minDocCount(1);
const agg = esb.histogramAggregation('prices', 'price', 50)
.extendedBounds(0, 500);
const agg = esb.histogramAggregation('quantity', 'quantity', 10).missing(0);
Dedicated range aggregation for IP typed fields.
Extends RangeAggregationBase
(string)
The name which will be used to refer to this aggregation.
(string?)
The field to aggregate on
const agg = esb.ipRangeAggregation('ip_ranges', 'ip').ranges([
{ to: '10.0.0.5' },
{ from: '10.0.0.5' }
]);
const agg = esb.ipRangeAggregation('ip_ranges', 'ip').ranges([
{ mask: '10.0.0.0/25' },
{ mask: '10.0.0.127/25' }
]);
A field data based single bucket aggregation, that creates a bucket of all documents in the current document set context that are missing a field value (effectively, missing a field or having the configured NULL value set).
Extends BucketAggregationBase
(string)
The name which will be used to refer to this aggregation.
(string?)
The field to aggregate on
const agg = esb.missingAggregation('products_without_a_price', 'price');
A special single bucket aggregation that enables aggregating nested documents.
Extends BucketAggregationBase
(string)
The name which will be used to refer to this aggregation.
const reqBody = esb.requestBodySearch()
.query(esb.matchQuery('name', 'led tv'))
.agg(
esb.nestedAggregation('resellers', 'resellers').agg(
esb.minAggregation('min_price', 'resellers.price')
)
);
Sets the nested path
NestedAggregation
:
returns
this
so that calls can be chained
A special single bucket aggregation that enables aggregating from buckets on child document types to buckets on parent documents.
This aggregation relies on the _parent
field in the mapping.
Extends BucketAggregationBase
(string)
The name which will be used to refer to this aggregation.
(string?)
The type of the child document.
const reqBody = esb.requestBodySearch()
.agg(
esb.termsAggregation('top-names', 'owner.display_name.keyword')
.size(10)
.agg(
esb.parentAggregation('to-questions')
.type('answer')
.agg(
esb.termsAggregation(
'top-tags',
'tags.keyword'
).size(10)
)
)
)
.size(0);
Sets the child type/mapping for aggregation.
(string)
The child type that the buckets in the parent space should be mapped to.
ParentAggregation
:
returns
this
so that calls can be chained
A multi-bucket value source based aggregation that enables the user to define a set of ranges - each representing a bucket. During the aggregation process, the values extracted from each document will be checked against each bucket range and "bucket" the relevant/matching document.
Note that this aggregration includes the from value and excludes the to value for each range.
Extends RangeAggregationBase
(string)
The name which will be used to refer to this aggregation.
(string?)
The field to aggregate on
const agg = esb.rangeAggregation('price_ranges', 'price').ranges([
{ to: 50 },
{ from: 50, to: 100 },
{ from: 100 }
]);
const agg = esb.rangeAggregation('price_ranges')
.script(esb.script('inline', "doc['price'].value").lang('painless'))
.ranges([{ to: 50 }, { from: 50, to: 100 }, { from: 100 }]);
// Value script for on-the-fly conversion before aggregation
const agg = esb.rangeAggregation('price_ranges', 'price')
.script(
esb.script('inline', '_value * params.conversion_rate')
.lang('painless')
.params({ conversion_rate: 0.8 })
)
.ranges([{ to: 50 }, { from: 50, to: 100 }, { from: 100 }]);
// Compute statistics over the prices in each price range
const agg = esb.rangeAggregation('price_ranges', 'price')
.ranges([{ to: 50 }, { from: 50, to: 100 }, { from: 100 }])
// Passing price to Stats Aggregation is optional(same value source)
.agg(esb.statsAggregation('price_stats', 'price'));
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
NOTE: Only available in Elasticsearch 7.3.0+.
Extends BucketAggregationBase
(string)
The name which will be used to refer to this aggregation.
(string)
The field we wish to find rare terms in
const agg = esb.rareTermsAggregation('genres', 'genre');
Sets the maximum number of documents a term should appear in.
(number)
Integer value for maximum number of documents a term should appear in.
Max doc count can be between 1 and 100.
RareTermsAggregation
:
returns
this
so that calls can be chained
const agg = esb.rareTermsAggregation('genres', 'genre').maxDocCount(2);
Sets the precision of the internal CuckooFilters. Smaller precision leads to better approximation, but higher memory usage. Cannot be smaller than 0.00001
(number)
Float value for precision of the internal CuckooFilters. Default is 0.01
RareTermsAggregation
:
returns
this
so that calls can be chained
const agg = esb.rareTermsAggregation('genres', 'genre').precision(0.001);
Sets terms that should be included in the aggregation
(string)
Regular expression that will determine what values
are "allowed" to be aggregated
RareTermsAggregation
:
returns
this
so that calls can be chained
const agg = esb.rareTermsAggregation('genres', 'genre').include('swi*');
Sets terms that should be excluded from the aggregation
(string)
Regular expression that will determine what values
should not be aggregated
RareTermsAggregation
:
returns
this
so that calls can be chained
const agg = esb.rareTermsAggregation('genres', 'genre').exclude('electro*');
Sets the missing parameter which defines how documents that are missing a value should be treated.
(string)
RareTermsAggregation
:
returns
this
so that calls can be chained
A special single bucket aggregation that enables aggregating on parent docs from nested documents. Effectively this aggregation can break out of the nested block structure and link to other nested structures or the root document, which allows nesting other aggregations that aren’t part of the nested object in a nested aggregation.
The reverse_nested
aggregation must be defined inside a nested aggregation.
Extends BucketAggregationBase
(string)
The name which will be used to refer to this aggregation.
(string?)
Defines to what nested object field should be joined back.
The default is empty, which means that it joins back to the root / main document
level.
const reqBody = esb.requestBodySearch()
.query(esb.matchQuery('name', 'led tv'))
.agg(
esb.nestedAggregation('comments', 'comments').agg(
esb.termsAggregation('top_usernames', 'comments.username').agg(
esb.reverseNestedAggregation('comment_to_issue').agg(
esb.termsAggregation('top_tags_per_comment', 'tags')
)
)
)
);
Sets the level to join back for subsequent aggregations in a multiple layered nested object types
(string)
Defines to what nested object field should be joined back.
The default is empty, which means that it joins back to the root / main document
level.
ReverseNestedAggregation
:
returns
this
so that calls can be chained
A filtering aggregation used to limit any sub aggregations' processing to a sample of the top-scoring documents.
Extends BucketAggregationBase
(string)
The name which will be used to refer to this aggregation.
(string?)
The field to aggregate on
const reqBody = esb.requestBodySearch()
.query(esb.queryStringQuery('tags:kibana OR tags:javascript'))
.agg(
esb.samplerAggregation('sample')
.shardSize(200)
.agg(
esb.significantTermsAggregation(
'keywords',
'tags'
).exclude(['kibana', 'javascript'])
)
);
The shard_size parameter limits how many top-scoring documents are collected in the sample processed on each shard. The default value is 100.
(number)
Maximum number of documents to return from each shard(Integer)
SamplerAggregation
:
returns
this
so that calls can be chained
The TermsAggregationBase
provides support for common options used across
various terms Aggregation
implementations like Significant terms and
Terms aggregation.
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.
Extends BucketAggregationBase
(string)
The name which will be used to refer to this aggregation.
(string)
Type of aggregation
(string)
Elasticsearch reference URL.
(string?)
The field to aggregate on
Sets the format expression for key_as_string
in response buckets.
If no format is specified, then it will use the first format specified in the field mapping.
(string)
Format mask to apply on aggregation response. Example: ####.00.
TermsAggregationBase
:
returns
this
so that calls can be chained
Sets the minimum number of matching hits required to return the terms.
(number)
Integer value for minimum number of documents
required to return bucket in response
TermsAggregationBase
:
returns
this
so that calls can be chained
const agg = esb.significantTermsAggregation('tags', 'tag').minDocCount(10);
Sets the parameter which regulates the certainty a shard has if the term
should actually be added to the candidate list or not with respect to
the min_doc_count
.
Terms will only be considered if their local shard frequency within
the set is higher than the shard_min_doc_count
.
(number)
Sets the
shard_min_doc_count
parameter. Default is 1
and has no effect unless you explicitly set it.
TermsAggregationBase
:
returns
this
so that calls can be chained
Defines how many term buckets should be returned out of the overall terms list.
(number)
TermsAggregationBase
:
returns
this
so that calls can be chained
const agg = esb.termsAggregation('products', 'product').size(5);
Sets the shard_size
parameter to control the volumes of candidate terms
produced by each shard. For the default, -1, shard_size will be automatically
estimated based on the number of shards and the size parameter.
shard_size
cannot be smaller than size (as it doesn’t make much sense).
When it is, elasticsearch will override it and reset it to be equal to size.
(number)
TermsAggregationBase
:
returns
this
so that calls can be chained
Sets the missing parameter which defines how documents that are missing a value should be treated.
(string)
TermsAggregationBase
:
returns
this
so that calls can be chained
Filter the values for which buckets will be created.
TermsAggregationBase
:
returns
this
so that calls can be chained
const agg = esb.termsAggregation('tags', 'tags')
.include('.*sport.*')
.exclude('water_.*');
// Match on exact values
const reqBody = esb.requestBodySearch()
.agg(
esb.termsAggregation('JapaneseCars', 'make').include([
'mazda',
'honda'
])
)
.agg(
esb.termsAggregation('ActiveCarManufacturers', 'make').exclude([
'rover',
'jensen'
])
);
Filter the values for which buckets will be created.
TermsAggregationBase
:
returns
this
so that calls can be chained
const agg = esb.termsAggregation('tags', 'tags')
.include('.*sport.*')
.exclude('water_.*');
// Match on exact values
const reqBody = esb.requestBodySearch()
.agg(
esb.termsAggregation('JapaneseCars', 'make').include([
'mazda',
'honda'
])
)
.agg(
esb.termsAggregation('ActiveCarManufacturers', 'make').exclude([
'rover',
'jensen'
])
);
This setting can influence the management of the values used for de-duplication. Each option will hold up to shard_size values in memory while performing de-duplication but the type of value held can be controlled
(string)
the possible values are
map
,
global_ordinals
,
global_ordinals_hash
and
global_ordinals_low_cardinality
TermsAggregationBase
:
returns
this
so that calls can be chained
const agg = esb.significantTermsAggregation('tags', 'tag').executionHint('map');
const agg = esb.termsAggregation('tags', 'tags').executionHint('map');
The SignificantAggregationBase
provides support for common options used
in SignificantTermsAggregation
and SignificantTextAggregation
.
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.
Extends TermsAggregationBase
Use JLH score as significance score.
SignificantAggregationBase
:
returns
this
so that calls can be chained
Use mutual_information
as significance score
(boolean?
= true
)
Default
true
. If set to
false
,
filters out the terms that appear less often in the subset than in
documents outside the subset
(boolean?
= true
)
true
(default) if the documents in the bucket
are also contained in the background. If instead you defined a custom background filter
that represents a different set of documents that you want to compare to, pass
false
SignificantAggregationBase
:
returns
this
so that calls can be chained
Use chi_square
as significance score
(boolean
= true
)
Default
true
. If set to
false
,
filters out the terms that appear less often in the subset than in
documents outside the subset
(boolean
= true
)
true
(default) if the documents in the bucket
are also contained in the background. If instead you defined a custom background filter
that represents a different set of documents that you want to compare to, pass
false
SignificantAggregationBase
:
returns
this
so that calls can be chained
Sets gnd
, google normalized score to be used as significance score.
(boolean
= true
)
true
(default) if the documents in the bucket
are also contained in the background. If instead you defined a custom background filter
that represents a different set of documents that you want to compare to, pass
false
SignificantAggregationBase
:
returns
this
so that calls can be chained
Use a simple calculation of the number of documents in the foreground sample with a term divided by the number of documents in the background with the term. By default this produces a score greater than zero and less than one.
SignificantAggregationBase
:
returns
this
so that calls can be chained
Sets script for customized score calculation.
(Script)
SignificantAggregationBase
:
returns
this
so that calls can be chained
Sets the background_filter
to narrow the scope of statistical information
for background term frequencies instead of using the entire index.
(Query)
Filter query
SignificantAggregationBase
:
returns
this
so that calls can be chained
const reqBody = esb.requestBodySearch()
.query(esb.matchQuery('text', 'madrid'))
.agg(
esb.significantAggregationBase('tags', 'tag').backgroundFilter(
esb.termQuery('text', 'spain')
)
);
An aggregation that returns interesting or unusual occurrences of terms in a set.
Extends SignificantAggregationBase
(string)
The name which will be used to refer to this aggregation.
(string?)
The field to aggregate on
const reqBody = esb.requestBodySearch()
.query(esb.termsQuery('force', 'British Transport Police'))
.agg(
esb.significantTermsAggregation(
'significantCrimeTypes',
'crime_type'
)
);
// Use parent aggregation for segregated data analysis
const agg = esb.termsAggregation('forces', 'force').agg(
esb.significantTermsAggregation('significantCrimeTypes', 'crime_type')
);
An aggregation that returns interesting or unusual occurrences of free-text
terms in a set. It is like the SignificantTermsAggregation
but differs in
that:
text
fieldsNOTE: This query was added in elasticsearch v6.0.
Extends SignificantAggregationBase
(string)
The name which will be used to refer to this aggregation.
(string?)
The field to aggregate on
const reqBody = esb.requestBodySearch()
.query(esb.matchQuery('content', 'Bird flu'))
.agg(
esb.samplerAggregation('my_sample')
.shardSize(100)
.agg(esb.significantTextAggregation('keywords', 'content'))
);
Control if duplicate paragraphs of text should try be filtered from the
statistical text analysis. Can improve results but slows down analysis.
Default is false
.
(boolean)
SignificantTextAggregation
:
returns
this
so that calls can be chained
const reqBody = esb.requestBodySearch()
.query(esb.matchQuery('content', 'elasticsearch'))
.agg(
esb.samplerAggregation('sample')
.shardSize(100)
.agg(
esb.significantTextAggregation('keywords', 'content')
.filterDuplicateText(true)
)
);
Selects the fields to load from _source
JSON and analyze. If none are
specified, the indexed "fieldName" value is assumed to also be the name
of the JSON field holding the value
SignificantTextAggregation
:
returns
this
so that calls can be chained
const reqBody = esb.requestBodySearch()
.query(esb.matchQuery('custom_all', 'elasticsearch'))
.agg(
esb.significantTextAggregation('tags', 'custom_all')
.sourceFields(['content', 'title'])
);
A multi-bucket value source based aggregation where buckets are dynamically built - one per unique value.
Extends TermsAggregationBase
(string)
The name which will be used to refer to this aggregation.
(string?)
The field to aggregate on
const agg = esb.termsAggregation('genres', 'genre');
When set to true
, shows an error value for each term returned by the aggregation
which represents the worst case error in the document count and can be useful
when deciding on a value for the shard_size parameter.
(boolean)
TermsAggregation
:
returns
this
so that calls can be chained
Break the analysis up into multiple requests by grouping the field’s values into a number of partitions at query-time and processing only one partition in each request.
Note that this method is a special case as the name doesn't map to the
elasticsearch parameter name. This is required because there is already
a method for include
applicable for Terms aggregations. However, this
could change depending on community interest.
TermsAggregation
:
returns
this
so that calls can be chained
const agg = esb.termsAggregation('expired_sessions', 'account_id')
.includePartition(0, 20)
.size(10000)
.order('last_access', 'asc')
.agg(esb.maxAggregation('last_access', 'access_date'));
Can be used for deferring calculation of child aggregations by using
breadth_first
mode. In depth_first
mode all branches of the aggregation
tree are expanded in one depth-first pass and only then any pruning occurs.
TermsAggregation
:
returns
this
so that calls can be chained
const agg = esb.termsAggregation('actors', 'actors')
.size(10)
.collectMode('breadth_first')
.agg(esb.termsAggregation('costars', 'actors').size(5));
Sets the ordering for buckets
TermsAggregation
:
returns
this
so that calls can be chained
// Ordering the buckets by their doc `_count` in an ascending manner
const agg = esb.termsAggregation('genres', 'genre').order('_count', 'asc');
// Ordering the buckets alphabetically by their terms in an ascending manner
const agg = esb.termsAggregation('genres', 'genre').order('_term', 'asc');
// Ordering the buckets by single value metrics sub-aggregation
// (identified by the aggregation name)
const agg = esb.termsAggregation('genres', 'genre')
.order('max_play_count', 'asc')
.agg(esb.maxAggregation('max_play_count', 'play_count'));
// Ordering the buckets by multi value metrics sub-aggregation
// (identified by the aggregation name):
const agg = esb.termsAggregation('genres', 'genre')
.order('playback_stats.max', 'desc')
.agg(esb.statsAggregation('playback_stats', 'play_count'));
// Multiple order criteria
const agg = esb.termsAggregation('countries')
.field('artist.country')
.order('rock>playback_stats.avg', 'desc')
.order('_count', 'desc')
.agg(
esb.filterAggregation('rock')
.filter(esb.termQuery('genre', 'rock'))
.agg(esb.statsAggregation('playback_stats', 'play_count'))
);
The PipelineAggregationBase
provides support for common options used across
various pipeline Aggregation
implementations.
Pipeline aggregations cannot have sub-aggregations but depending on the type it can reference another pipeline in the buckets_path allowing pipeline aggregations to be chained. For example, you can chain together two derivatives to calculate the second derivative (i.e. a derivative of a derivative).
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.
Extends Aggregation
(string)
a valid aggregation name
(string)
type of aggregation
(string)
Elasticsearch reference URL
Sets the relative path, buckets_path
, which refers to the metric to aggregate over.
Required.
PipelineAggregationBase
:
returns
this
so that calls can be chained
const reqBody = esb.requestBodySearch()
.agg(
esb.dateHistogramAggregation('histo', 'date')
.interval('day')
.agg(esb.termsAggregation('categories', 'category'))
.agg(
esb.bucketSelectorAggregation('min_bucket_selector')
.bucketsPath({ count: 'categories._bucket_count' })
.script(esb.script('inline', 'params.count != 0'))
)
)
.size(0);
Set policy for missing data. Optional.
PipelineAggregationBase
:
returns
this
so that calls can be chained
Sets the format expression if applicable. Optional.
(string)
Format mask to apply on aggregation response. Example: ####.00
PipelineAggregationBase
:
returns
this
so that calls can be chained
A sibling pipeline aggregation which calculates the (mean) average value of a specified metric in a sibling aggregation. The specified metric must be numeric and the sibling aggregation must be a multi-bucket aggregation.
Extends PipelineAggregationBase
(string)
The name which will be used to refer to this aggregation.
(string?)
The relative path of metric to aggregate over
const reqBody = esb.requestBodySearch()
.agg(
esb.dateHistogramAggregation('sales_per_month', 'date')
.interval('month')
.agg(esb.sumAggregation('sales', 'price'))
)
.agg(
esb.avgBucketAggregation(
'avg_monthly_sales',
'sales_per_month>sales'
)
)
.size(0);
A parent pipeline aggregation which calculates the derivative of a specified metric in a parent histogram (or date_histogram) aggregation. The specified metric must be numeric and the enclosing histogram must have min_doc_count set to 0 (default for histogram aggregations).
Extends PipelineAggregationBase
(string)
The name which will be used to refer to this aggregation.
(string?)
The relative path of metric to aggregate over
const reqBody = esb.requestBodySearch()
.agg(
esb.dateHistogramAggregation('sales_per_month', 'date')
.interval('month')
.agg(esb.sumAggregation('sales', 'price'))
.agg(esb.derivativeAggregation('sales_deriv', 'sales'))
)
.size(0);
// First and second order derivative of the monthly sales
const reqBody = esb.requestBodySearch()
.agg(
esb.dateHistogramAggregation('sales_per_month', 'date')
.interval('month')
.agg(esb.sumAggregation('sales', 'price'))
.agg(esb.derivativeAggregation('sales_deriv', 'sales'))
.agg(esb.derivativeAggregation('sales_2nd_deriv', 'sales_deriv'))
)
.size(0);
Set the units of the derivative values. unit
specifies what unit to use for
the x-axis of the derivative calculation
DerivativeAggregation
:
returns
this
so that calls can be chained
const reqBody = esb.requestBodySearch()
.agg(
esb.dateHistogramAggregation('sales_per_month', 'date')
.interval('month')
.agg(esb.sumAggregation('sales', 'price'))
.agg(esb.derivativeAggregation('sales_deriv', 'sales').unit('day'))
)
.size(0);
A sibling pipeline aggregation which identifies the bucket(s) with the maximum value of a specified metric in a sibling aggregation and outputs both the value and the key(s) of the bucket(s). The specified metric must be numeric and the sibling aggregation must be a multi-bucket aggregation.
Extends PipelineAggregationBase
(string)
The name which will be used to refer to this aggregation.
(string?)
The relative path of metric to aggregate over
const reqBody = esb.requestBodySearch()
.agg(
esb.dateHistogramAggregation('sales_per_month', 'date')
.interval('month')
.agg(esb.sumAggregation('sales', 'price'))
)
.agg(
// Metric embedded in sibling aggregation
// Get the maximum value of `sales` aggregation in
// `sales_per_month` histogram
esb.maxBucketAggregation(
'max_monthly_sales',
'sales_per_month>sales'
)
)
.size(0);
A sibling pipeline aggregation which identifies the bucket(s) with the minimum value of a specified metric in a sibling aggregation and outputs both the value and the key(s) of the bucket(s). The specified metric must be numeric and the sibling aggregation must be a multi-bucket aggregation.
Extends PipelineAggregationBase
(string)
The name which will be used to refer to this aggregation.
(string?)
The relative path of metric to aggregate over
const reqBody = esb.requestBodySearch()
.agg(
esb.dateHistogramAggregation('sales_per_month', 'date')
.interval('month')
.agg(esb.sumAggregation('sales', 'price'))
)
.agg(
// Metric embedded in sibling aggregation
// Get the minimum value of `sales` aggregation in
// `sales_per_month` histogram
esb.minBucketAggregation(
'min_monthly_sales',
'sales_per_month>sales'
)
)
.size(0);
A sibling pipeline aggregation which calculates the sum across all bucket of a specified metric in a sibling aggregation. The specified metric must be numeric and the sibling aggregation must be a multi-bucket aggregation.
Extends PipelineAggregationBase
(string)
The name which will be used to refer to this aggregation.
(string?)
The relative path of metric to aggregate over
const reqBody = esb.requestBodySearch()
.agg(
esb.dateHistogramAggregation('sales_per_month', 'date')
.interval('month')
.agg(esb.sumAggregation('sales', 'price'))
)
.agg(
// Get the sum of all the total monthly `sales` buckets
esb.sumBucketAggregation(
'sum_monthly_sales',
'sales_per_month>sales'
)
)
.size(0);
A sibling pipeline aggregation which calculates a variety of stats across all bucket of a specified metric in a sibling aggregation. The specified metric must be numeric and the sibling aggregation must be a multi-bucket aggregation.
Extends PipelineAggregationBase
(string)
The name which will be used to refer to this aggregation.
(string?)
The relative path of metric to aggregate over
const reqBody = esb.requestBodySearch()
.agg(
esb.dateHistogramAggregation('sales_per_month', 'date')
.interval('month')
.agg(esb.sumAggregation('sales', 'price'))
)
.agg(
// Calculates stats for monthly sales
esb.statsBucketAggregation(
'stats_monthly_sales',
'sales_per_month>sales'
)
)
.size(0);
A sibling pipeline aggregation which calculates a variety of stats across all bucket of a specified metric in a sibling aggregation. The specified metric must be numeric and the sibling aggregation must be a multi-bucket aggregation.
Extends PipelineAggregationBase
(string)
The name which will be used to refer to this aggregation.
(string?)
The relative path of metric to aggregate over
const reqBody = esb.requestBodySearch()
.agg(
esb.dateHistogramAggregation('sales_per_month', 'date')
.interval('month')
.agg(esb.sumAggregation('sales', 'price'))
)
.agg(
// Calculates extended stats for monthly sales
esb.extendedStatsBucketAggregation(
'stats_monthly_sales',
'sales_per_month>sales'
)
)
.size(0);
Sets the number of standard deviations above/below the mean to display. Optional.
(number)
Default is 2.
ExtendedStatsBucketAggregation
:
returns
this
so that calls can be chained
A sibling pipeline aggregation which calculates percentiles across all bucket of a specified metric in a sibling aggregation. The specified metric must be numeric and the sibling aggregation must be a multi-bucket aggregation.
Extends PipelineAggregationBase
(string)
The name which will be used to refer to this aggregation.
(string?)
The relative path of metric to aggregate over
const reqBody = esb.requestBodySearch()
.agg(
esb.dateHistogramAggregation('sales_per_month', 'date')
.interval('month')
.agg(esb.sumAggregation('sales', 'price'))
)
.agg(
// Calculates stats for monthly sales
esb.percentilesBucketAggregation(
'percentiles_monthly_sales',
'sales_per_month>sales'
).percents([25.0, 50.0, 75.0])
)
.size(0);
Sets the list of percentiles to calculate
PercentilesBucketAggregation
:
returns
this
so that calls can be chained
Given an ordered series of data, the Moving Average aggregation will slide a window across the data and emit the average value of that window.
moving_avg
aggregations must be embedded inside of a histogram or
date_histogram aggregation.
Extends PipelineAggregationBase
(string)
The name which will be used to refer to this aggregation.
(string?)
The relative path of metric to aggregate over
const agg = esb.movingAverageAggregation('the_movavg', 'the_sum')
.model('holt')
.window(5)
.gapPolicy('insert_zeros')
.settings({ alpha: 0.8 });
const reqBody = esb.requestBodySearch()
.agg(
esb.dateHistogramAggregation('my_date_histo', 'timestamp')
.interval('day')
.agg(esb.sumAggregation('the_sum', 'lemmings'))
// Relative path to sibling metric `the_sum`
.agg(esb.movingAverageAggregation('the_movavg', 'the_sum'))
)
.size(0);
const reqBody = esb.requestBodySearch()
.agg(
esb.dateHistogramAggregation('my_date_histo', 'timestamp')
.interval('day')
// Use the document count as it's input
.agg(esb.movingAverageAggregation('the_movavg', '_count'))
)
.size(0);
Sets the moving average weighting model that we wish to use. Optional.
(string)
Can be
simple
,
linear
,
ewma
(aka "single-exponential"),
holt
(aka "double exponential")
or
holt_winters
(aka "triple exponential").
Default is
simple
MovingAverageAggregation
:
returns
this
so that calls can be chained
const agg = esb.movingAverageAggregation('the_movavg', 'the_sum')
.model('simple')
.window(30);
const agg = esb.movingAverageAggregation('the_movavg', 'the_sum')
.model('ewma')
.window(30)
.settings({ alpha: 0.8 });
Sets the size of window to "slide" across the histogram. Optional.
(number)
Default is 5
MovingAverageAggregation
:
returns
this
so that calls can be chained
const agg = esb.movingAverageAggregation('the_movavg', 'the_sum')
.model('simple')
.window(30)
If the model should be algorithmically minimized. Optional.
Applicable on EWMA, Holt-Linear, Holt-Winters.
Minimization is disabled by default for ewma
and holt_linear
,
while it is enabled by default for holt_winters
.
MovingAverageAggregation
:
returns
this
so that calls can be chained
const agg = esb.movingAverageAggregation('the_movavg', 'the_sum')
.model('holt_winters')
.window(30)
.minimize(true)
.settings({ period: 7 });
Model-specific settings, contents which differ depending on the model specified. Optional.
(Object)
MovingAverageAggregation
:
returns
this
so that calls can be chaineds
const agg = esb.movingAverageAggregation('the_movavg', 'the_sum')
.model('ewma')
.window(30)
.settings({ alpha: 0.8 });
Enable "prediction" mode, which will attempt to extrapolate into the future given the current smoothed, moving average
(number)
the number of predictions you would like appended to the
end of the series
MovingAverageAggregation
:
returns
this
so that calls can be chained
const agg = esb.movingAverageAggregation('the_movavg', 'the_sum')
.model('simple')
.window(30)
.predict(10);
Given an ordered series of data, the Moving Function aggregation will slide a window across the data and allow the user to specify a custom script that is executed on each window of data. For convenience, a number of common functions are predefined such as min/max, moving averages, etc.
moving_fn
aggregations must be embedded inside of a histogram or
date_histogram aggregation.
NOTE: Only available in Elasticsearch 6.4.0+.
Extends PipelineAggregationBase
(string)
The name which will be used to refer to this aggregation.
(string?)
The relative path of metric to aggregate over.
(string?)
The size of window to "slide" across the histogram.
(string?)
The script that should be executed on each window of data.
const agg = esb.movingFunctionAggregation('the_movfn', 'the_sum')
.model('holt')
.window(5)
.gapPolicy('insert_zeros')
.settings({ alpha: 0.8 });
const reqBody = esb.requestBodySearch()
.agg(
esb.dateHistogramAggregation('my_date_histo', 'timestamp')
.interval('day')
.agg(esb.sumAggregation('the_sum', 'lemmings'))
// Relative path to sibling metric `the_sum`
.agg(esb.movingFunctionAggregation('the_movfn', 'the_sum'))
)
.size(0);
const reqBody = esb.requestBodySearch()
.agg(
esb.dateHistogramAggregation('my_date_histo', 'timestamp')
.interval('day')
// Use the document count as it's input
.agg(esb.movingFunctionAggregation('the_movfn', '_count'))
)
.size(0);
Sets the size of window to "slide" across the histogram. Optional.
(number)
Default is 5
MovingFunctionAggregation
:
returns
this
so that calls can be chained
const agg = esb.movingFunctionAggregation('the_movfn', 'the_sum')
.window(30)
Sets shift of window position. Optional.
(number)
Default is 0
MovingFunctionAggregation
:
returns
this
so that calls can be chained
const agg = esb.movingFunctionAggregation('the_movfn', 'the_sum')
.shift(30)
Sets the script that should be executed on each window of data. Required.
(string)
MovingFunctionAggregation
:
returns
this
so that calls can be chained
const agg = esb.movingFunctionAggregation('the_movfn', 'the_sum', "MovingFunctions.unweightedAvg(values)"") .script("MovingFunctions.unweightedAvg(values)")
A parent pipeline aggregation which calculates the cumulative sum of a specified metric in a parent histogram (or date_histogram) aggregation. The specified metric must be numeric and the enclosing histogram must have min_doc_count set to 0 (default for histogram aggregations).
Extends PipelineAggregationBase
(string)
The name which will be used to refer to this aggregation.
(string?)
The relative path of metric to aggregate over
const reqBody = esb.requestBodySearch()
.agg(
esb.dateHistogramAggregation('sales_per_month', 'date', 'month')
.agg(esb.sumAggregation('sales', 'price'))
.agg(esb.cumulativeSumAggregation('cumulative_sales', 'sales'))
)
.size(0);
A parent pipeline aggregation which executes a script which can perform per bucket computations on specified metrics in the parent multi-bucket aggregation. The specified metric must be numeric and the script must return a numeric value.
Extends PipelineAggregationBase
(string)
The name which will be used to refer to this aggregation.
(string?)
The relative path of metric to aggregate over
const reqBody = esb.requestBodySearch()
.agg(
esb.dateHistogramAggregation('sales_per_month', 'date', 'month')
.agg(esb.sumAggregation('total_sales', 'price'))
.agg(
esb.filterAggregation('t-shirts')
.filter(esb.termQuery('type', 't-shirt'))
.agg(esb.sumAggregation('sales', 'price'))
)
.agg(
esb.bucketScriptAggregation('t-shirt-percentage')
.bucketsPath({
tShirtSales: 't-shirts>sales',
totalSales: 'total_sales'
})
.script('params.tShirtSales / params.totalSales * 100')
)
)
.size(0);
Sets script parameter for aggregation.
BucketScriptAggregation
:
returns
this
so that calls can be chained
script
is not an instance of
Script
A parent pipeline aggregation which executes a script which determines whether the current bucket will be retained in the parent multi-bucket aggregation. The specified metric must be numeric and the script must return a boolean value. If the script language is expression then a numeric return value is permitted. In this case 0.0 will be evaluated as false and all other values will evaluate to true.
Extends PipelineAggregationBase
(string)
The name which will be used to refer to this aggregation.
(string?)
The relative path of metric to aggregate over
const reqBody = esb.requestBodySearch()
.agg(
esb.dateHistogramAggregation('histo', 'date')
.interval('day')
.agg(esb.termsAggregation('categories', 'category'))
.agg(
esb.bucketSelectorAggregation('min_bucket_selector')
.bucketsPath({ count: 'categories._bucket_count' })
.script(esb.script('inline', 'params.count != 0'))
)
)
.size(0);
const reqBody = esb.requestBodySearch()
.agg(
esb.dateHistogramAggregation('sales_per_month', 'date')
.interval('month')
.agg(esb.sumAggregation('sales', 'price'))
.agg(
esb.bucketSelectorAggregation('sales_bucket_filter')
.bucketsPath({ totalSales: 'total_sales' })
.script('params.totalSales > 200')
)
)
.size(0);
Sets script parameter for aggregation. Required.
BucketSelectorAggregation
:
returns
this
so that calls can be chained
script
is not an instance of
Script
Serial differencing is a technique where values in a time series are subtracted from itself at different time lags or periods.
Serial differences are built by first specifying a histogram
or date_histogram
over a field.
Extends PipelineAggregationBase
(string)
The name which will be used to refer to this aggregation.
(string?)
The relative path of metric to aggregate over
const reqBody = esb.requestBodySearch()
.agg(
esb.dateHistogramAggregation('my_date_histo', 'timestamp')
.interval('day')
.agg(esb.sumAggregation('the_sum', 'lemmings'))
.agg(
esb.serialDifferencingAggregation(
'thirtieth_difference',
'the_sum'
).lag(30)
)
)
.size(0);
The historical bucket to subtract from the current value. Optional.
(number)
Default is 1.
SerialDifferencingAggregation
:
returns
this
so that calls can be chained
A parent pipeline aggregation which sorts the buckets of its parent multi-bucket aggregation. Zero or more sort fields may be specified together with the corresponding sort order. Each bucket may be sorted based on its _key, _count or its sub-aggregations. In addition, parameters from and size may be set in order to truncate the result buckets.
Extends PipelineAggregationBase
(string)
The name which will be used to refer to this aggregation.
const reqBody = esb.requestBodySearch()
.agg(
esb.bucketSortAggregation('sort')
.sort([
esb.sort('user', 'desc')
])
.from(5)
.size(10)
)
);
Sets the list of fields to sort on. Optional.
BucketSortAggregation
:
returns
this
so that calls can be chained
Sets the value buckets in positions prior to which will be truncated. Optional.
(number)
Buckets in positions prior to the set value will be truncated.
BucketSortAggregation
:
returns
this
so that calls can be chained
Sets the number of buckets to return. Optional.
(number)
The number of buckets to return.
BucketSortAggregation
:
returns
this
so that calls can be chained
The matrix_stats
aggregation is a numeric aggregation that computes
statistics over a set of document fields
Extends Aggregation
const agg = esb.matrixStatsAggregation('matrixstats', ['poverty', 'income']);
The fields
setting defines the set of fields (as an array) for computing
the statistics.
MatrixStatsAggregation
:
returns
this
so that calls can be chained
const agg = esb.matrixStatsAggregation('matrixstats')
.fields(['poverty', 'income']);
The mode
parameter controls what array value the aggregation will use for
array or multi-valued fields
MatrixStatsAggregation
:
returns
this
so that calls can be chained
The missing parameter defines how documents that are missing a value should be treated. By default they will be ignored but it is also possible to treat them as if they had a value.
(Object)
Set of fieldname : value mappings to specify default
values per field
MatrixStatsAggregation
:
returns
this
so that calls can be chained
const agg = esb.matrixStatsAggregation('matrixstats')
.fields(['poverty', 'income'])
.missing({ income: 50000 });
ScoreFunction
provides support for common options used across
various ScoreFunction
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.
(string)
Adds a filter query whose matching documents will have the score function applied.
ScoreFunction
:
returns
this
so that calls can be chained.
Sets the weight of the score function
(number)
The weight of this score function.
ScoreFunction
:
returns
this
so that calls can be chained.
The script_score
function allows you to wrap another query and customize
the scoring of it optionally with a computation derived from other numeric
field values in the doc using a script expression.
Extends ScoreFunction
const scoreFunc = esb.scriptScoreFunction(
esb.script('inline', "_score * doc['my_numeric_field'].value")
.lang('painless')
);
// Script with parameters
const scoreFunc = esb.scriptScoreFunction(
esb.script(
'inline',
"_score * doc['my_numeric_field'].value / Math.pow(params.param1, params.param2)"
)
.lang('painless')
.params({ param1: 'value1', param2: 'value2' })
);
ScriptScoreFunction
:
returns
this
so that calls can be chained.
The weight
score allows you to multiply the score by the provided weight
.
This can sometimes be desired since boost value set on specific queries gets
normalized, while for this score function it does not.
The number value is of type float.
Extends ScoreFunction
(number?)
The weight of this score function.
const scoreFunc = esb.weightScoreFunction(42);
The random_score
generates scores using a hash of the _uid
field,
with a seed
for variation. If seed
is not specified, the current time is used.
Extends ScoreFunction
const scoreFunc = esb.randomScoreFunction().seed(299792458);
Sets random seed value.
(number)
A seed value.
RandomScoreFunction
:
returns
this
so that calls can be chained.
The field_value_factor
function allows you to use a field from a document
to influence the score. It's similar to using the script_score
function, however,
it avoids the overhead of scripting. If used on a multi-valued field, only the
first value of the field is used in calculations.
Extends ScoreFunction
(string?)
the field to be extracted from the document.
// Scoring formula - sqrt(1.2 * doc['popularity'].value)
const scoreFunc = esb.fieldValueFactorFunction('popularity')
.factor(1.2)
.modifier('sqrt')
.missing(1);
Sets the field to be extracted from the document.
(string)
the field to be extracted from the document.
FieldValueFactorFunction
:
returns
this
so that calls can be chained.
Optional factor to multiply the field value with, defaults to 1
.
(number)
Factor to multiply the field with.
FieldValueFactorFunction
:
returns
this
so that calls can be chained.
Modifier to apply to the field value, can be one of: none
, log
,
log1p
, log2p
, ln
, ln1p
, ln2p
, square
, sqrt
, or reciprocal
.
Defaults to none
.
(string)
Modified to apply on field. Can be one of:
none
,
log
,
log1p
,
log2p
,
ln
,
ln1p
,
ln2p
,
square
,
sqrt
, or
reciprocal
.
Defaults to
none
.
FieldValueFactorFunction
:
returns
this
so that calls can be chained.
Value used if the document doesn’t have that field. The modifier and factor are still applied to it as though it were read from the document.
(number)
To be used with documents which do not have field value.
FieldValueFactorFunction
:
returns
this
so that calls can be chained.
Decay functions score a document with a function that decays depending on the distance of a numeric field value of the document from a user given origin. This is similar to a range query, but with smooth edges instead of boxes.
Supported decay functions are: linear
, exp
, and gauss
.
If no mode
is supplied, gauss
will be used.
Extends ScoreFunction
(string?)
the document field to run decay function against.
// Defaults to decay function `gauss`
const decayFunc = esb.decayScoreFunction()
.field('location') // field is a geo_point
.origin('11, 12') // geo format
.scale('2km')
.offset('0km')
.decay(0.33);
const decayFunc = esb.decayScoreFunction('gauss', 'date')
.origin('2013-09-17')
.scale('10d')
.offset('5d')
.decay(0.5);
Set the decay mode.
DecayScoreFunction
:
returns
this
so that calls can be chained.
Sets the decay mode to linear.
Alias for mode('linear')
DecayScoreFunction
:
returns
this
so that calls can be chained.
Sets the decay mode to exp.
Alias for mode('exp')
DecayScoreFunction
:
returns
this
so that calls can be chained.
Sets the decay mode to gauss.
Alias for mode('gauss')
DecayScoreFunction
:
returns
this
so that calls can be chained.
Sets the document field to run decay function against.
(string)
the document field to run decay function against.
DecayScoreFunction
:
returns
this
so that calls can be chained.
The point of origin used for calculating distance. Must be given as a number
for numeric field, date for date fields and geo point for geo fields.
Required for geo and numeric field. For date fields the default is now
.
Date math (for example now-1h
) is supported for origin.
DecayScoreFunction
:
returns
this
so that calls can be chained.
Required for all types. Defines the distance from origin + offset at which
the computed score will equal decay parameter. For geo fields: Can be defined
as number+unit (1km
, 12m
,…). Default unit is meters. For date fields: Can be
defined as a number+unit (1h
, 10d
,…). Default unit is milliseconds.
For numeric field: Any number.
DecayScoreFunction
:
returns
this
so that calls can be chained.
If an offset
is defined, the decay function will only compute the decay function
for documents with a distance greater that the defined offset. The default is 0
.
DecayScoreFunction
:
returns
this
so that calls can be chained.
The decay
parameter defines how documents are scored at the distance given at scale
.
If no decay
is defined, documents at the distance scale
will be scored 0.5
.
(number)
A decay value as a double.
DecayScoreFunction
:
returns
this
so that calls can be chained.
Base class implementation for all suggester types.
NOTE: Instantiating this directly should not be required. However, if you wish to add a custom implementation for whatever reason, this class should be extended and used, as validation against the class type is present in various places.
(string)
The name of the Suggester, an arbitrary identifier
(string?)
The field to fetch the candidate suggestions from.
The AnalyzedSuggesterBase
provides support for common options used
in TermSuggester
and PhraseSuggester
.
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.
Extends Suggester
(string)
The name of the Suggester, an arbitrary identifier
(string?)
The field to fetch the candidate suggestions from.
(string?)
A string to get suggestions for.
Sets the text to get suggestions for. If not set, the global suggestion text will be used.
(string)
A string to get suggestions for.
AnalyzedSuggesterBase
:
returns
this
so that calls can be chained.
Sets the analyzer to analyse the suggest text with. Defaults to the search analyzer of the suggest field.
(string)
The analyzer to analyse the suggest text with.
AnalyzedSuggesterBase
:
returns
this
so that calls can be chained.
Sets the maximum number of suggestions to be retrieved from each individual shard.
During the reduce phase only the top N suggestions are returned based on the size
option. Defaults to the size
option. Setting this to a value higher than the size
can be useful in order to get a more accurate document frequency for spelling
corrections at the cost of performance. Due to the fact that terms are partitioned
amongst shards, the shard level document frequencies of spelling corrections
may not be precise. Increasing this will make these document frequencies
more precise.
(number)
AnalyzedSuggesterBase
:
returns
this
so that calls can be chained.
The term suggester suggests terms based on edit distance. The provided suggest text is analyzed before terms are suggested. The suggested terms are provided per analyzed suggest text token. The term suggester doesn’t take the query into account that is part of request.
Extends AnalyzedSuggesterBase
(string)
The name of the Suggester, an arbitrary identifier
(string?)
The field to fetch the candidate suggestions from.
(string?)
A string to get suggestions for.
name
is empty
const suggest = esb.termSuggester(
'my-suggestion',
'message',
'tring out Elasticsearch'
);
Sets the sort to control how suggestions should be sorted per suggest text term.
Two possible values:
score
: Sort by score first, then document frequency and
then the term itself.frequency
: Sort by document frequency first, then similarity
score and then the term itself.TermSuggester
:
returns
this
so that calls can be chained.
sort
is neither
score
nor
frequency
.
Sets the suggest mode which controls what suggestions are included or controls for what suggest text terms, suggestions should be suggested.
Three possible values can be specified:
missing
: Only provide suggestions for suggest text terms that
are not in the index. This is the default.popular
: Only suggest suggestions that occur in more docs
than the original suggest text term.always
: Suggest any matching suggestions based on terms in the suggest text.TermSuggester
:
returns
this
so that calls can be chained.
mode
is not one of
missing
,
popular
or
always
.
Sets the maximum edit distance candidate suggestions can have in order to be considered as a suggestion. Can only be a value between 1 and 2. Any other value result in an bad request error being thrown. Defaults to 2.
(number)
Value between 1 and 2. Defaults to 2.
TermSuggester
:
returns
this
so that calls can be chained.
Sets the number of minimal prefix characters that must match in order to be a candidate suggestions. Defaults to 1.
Increasing this number improves spellcheck performance. Usually misspellings don't occur in the beginning of terms.
(number)
The number of minimal prefix characters that must match in order
to be a candidate suggestions. Defaults to 1.
TermSuggester
:
returns
this
so that calls can be chained.
Sets the minimum length a suggest text term must have in order to be included. Defaults to 4.
(number)
The minimum length a suggest text term must have in order
to be included. Defaults to 4.
TermSuggester
:
returns
this
so that calls can be chained.
Sets factor that is used to multiply with the shards_size
in order to inspect
more candidate spell corrections on the shard level.
Can improve accuracy at the cost of performance. Defaults to 5.
(number)
Factor used to multiple with
shards_size
in
order to inspect more candidate spell corrections on the shard level.
Defaults to 5
TermSuggester
:
returns
this
so that calls can be chained.
Sets the minimal threshold in number of documents a suggestion should appear in. This can be specified as an absolute number or as a relative percentage of number of documents. This can improve quality by only suggesting high frequency terms. Defaults to 0f and is not enabled. If a value higher than 1 is specified then the number cannot be fractional. The shard level document frequencies are used for this option.
(number)
Threshold in number of documents a suggestion
should appear in. Defaults to 0f and is not enabled.
TermSuggester
:
returns
this
so that calls can be chained.
Sets the maximum threshold in number of documents a suggest text token can exist in order to be included. Can be a relative percentage number (e.g 0.4) or an absolute number to represent document frequencies. If an value higher than 1 is specified then fractional can not be specified. Defaults to 0.01f. This can be used to exclude high frequency terms from being spellchecked. High frequency terms are usually spelled correctly on top of this also improves the spellcheck performance. The shard level document frequencies are used for this option.
(number)
Maximum threshold in number of documents a suggest text
token can exist in order to be included. Defaults to 0.01f.
TermSuggester
:
returns
this
so that calls can be chained.
Sets the string distance implementation to use for comparing how similar suggested terms are.
Five possible values can be specified:
internal
: The default based on damerau_levenshtein
but highly optimized for
comparing string distance for terms inside the index.damerau_levenshtein
: String distance algorithm based on Damerau-Levenshtein
algorithm.levenstein
: String distance algorithm based on Levenstein edit distance
algorithm.jarowinkler
: String distance algorithm based on Jaro-Winkler algorithm.ngram
: String distance algorithm based on character n-grams.TermSuggester
:
returns
this
so that calls can be chained.
implMethod
is not one of
internal
,
damerau_levenshtein
,
levenstein
,
jarowinkler
or ngram`.
The phrase
suggester uses candidate generators to produce a list of possible
terms per term in the given text. A single candidate generator is similar
to a term
suggester called for each individual term in the text. The output
of the generators is subsequently scored in combination with the candidates
from the other terms to for suggestion candidates.
The Phrase suggest API accepts a list of generators under the key direct_generator
each of the generators in the list are called per term in the original text.
(string?)
The field to fetch the candidate suggestions from.
Sets field to fetch the candidate suggestions from. This is a required option that either needs to be set globally or per suggestion.
(string)
a valid field name
DirectGenerator
:
returns
this
so that calls can be chained
Sets the number of suggestions to return (defaults to 5
).
(number)
DirectGenerator
:
returns
this
so that calls can be chained.
Sets the suggest mode which controls what suggestions are included
or controls for what suggest text terms, suggestions should be suggested.
All values other than always
can be thought of as an optimization to
generate fewer suggestions to test on each shard and are not rechecked
when combining the suggestions generated on each shard. Thus missing
will generate suggestions for terms on shards that do not contain them
even other shards do contain them. Those should be filtered out
using confidence
.
Three possible values can be specified:
missing
: Only provide suggestions for suggest text terms that
are not in the index. This is the default.popular
: Only suggest suggestions that occur in more docs
than the original suggest text term.always
: Suggest any matching suggestions based on terms in the suggest text.DirectGenerator
:
returns
this
so that calls can be chained.
mode
is not one of
missing
,
popular
or
always
.
Sets the maximum edit distance candidate suggestions can have in order to be considered as a suggestion. Can only be a value between 1 and 2. Any other value result in an bad request error being thrown. Defaults to 2.
(number)
Value between 1 and 2. Defaults to 2.
DirectGenerator
:
returns
this
so that calls can be chained.
Sets the number of minimal prefix characters that must match in order to be a candidate suggestions. Defaults to 1.
Increasing this number improves spellcheck performance. Usually misspellings don't occur in the beginning of terms.
(number)
The number of minimal prefix characters that must match in order
to be a candidate suggestions. Defaults to 1.
DirectGenerator
:
returns
this
so that calls can be chained.
Sets the minimum length a suggest text term must have in order to be included. Defaults to 4.
(number)
The minimum length a suggest text term must have in order
to be included. Defaults to 4.
DirectGenerator
:
returns
this
so that calls can be chained.
Sets factor that is used to multiply with the shards_size
in order to inspect
more candidate spell corrections on the shard level.
Can improve accuracy at the cost of performance. Defaults to 5.
(number)
Factor used to multiple with
shards_size
in
order to inspect more candidate spell corrections on the shard level.
Defaults to 5
DirectGenerator
:
returns
this
so that calls can be chained.
Sets the minimal threshold in number of documents a suggestion should appear in. This can be specified as an absolute number or as a relative percentage of number of documents. This can improve quality by only suggesting high frequency terms. Defaults to 0f and is not enabled. If a value higher than 1 is specified then the number cannot be fractional. The shard level document frequencies are used for this option.
(number)
Threshold in number of documents a suggestion
should appear in. Defaults to 0f and is not enabled.
DirectGenerator
:
returns
this
so that calls can be chained.
Sets the maximum threshold in number of documents a suggest text token can exist in order to be included. Can be a relative percentage number (e.g 0.4) or an absolute number to represent document frequencies. If an value higher than 1 is specified then fractional can not be specified. Defaults to 0.01f. This can be used to exclude high frequency terms from being spellchecked. High frequency terms are usually spelled correctly on top of this also improves the spellcheck performance. The shard level document frequencies are used for this option.
(number)
Maximum threshold in number of documents a suggest text
token can exist in order to be included. Defaults to 0.01f.
DirectGenerator
:
returns
this
so that calls can be chained.
Sets the filter (analyzer) that is applied to each of the tokens passed to this candidate generator. This filter is applied to the original token before candidates are generated.
(string)
a filter (analyzer) that is applied to each of the
tokens passed to this candidate generator.
DirectGenerator
:
returns
this
so that calls can be chained.
Sets the filter (analyzer) that is applied to each of the generated tokens before they are passed to the actual phrase scorer.
(string)
a filter (analyzer) that is applied to each of the
generated tokens before they are passed to the actual phrase scorer.
DirectGenerator
:
returns
this
so that calls can be chained.
The phrase suggester adds additional logic on top of the term
suggester
to select entire corrected phrases instead of individual tokens weighted
based on ngram-language
models. In practice this suggester will be able
to make better decisions about which tokens to pick based on co-occurrence
and frequencies.
Extends AnalyzedSuggesterBase
(string)
The name of the Suggester, an arbitrary identifier
(string?)
The field to fetch the candidate suggestions from.
(string?)
A string to get suggestions for.
name
is empty
const suggest = esb.phraseSuggester(
'simple_phrase',
'title.trigram',
'noble prize'
)
.size(1)
.gramSize(3)
.directGenerator(esb.directGenerator('title.trigram').suggestMode('always'))
.highlight('<em>', '</em>');
Sets max size of the n-grams (shingles) in the field
. If the field
doesn't contain n-grams (shingles) this should be omitted or set to 1
.
Note: Elasticsearch tries to detect the gram size based on
the specified field
. If the field uses a shingle
filter the gram_size
is set to the max_shingle_size
if not explicitly set.
PhraseSuggester
:
returns
this
so that calls can be chained.
Sets the likelihood of a term being a misspelled even if the term exists
in the dictionary. The default is 0.95
corresponding to 5% of the
real words are misspelled.
PhraseSuggester
:
returns
this
so that calls can be chained.
Sets the confidence level defines a factor applied to the input phrases score
which is used as a threshold for other suggest candidates. Only candidates
that score higher than the threshold will be included in the result.
For instance a confidence level of 1.0
will only return suggestions
that score higher than the input phrase. If set to 0.0
the top N candidates
are returned. The default is 1.0
.
(number)
Factor applied to the input phrases score, used as
a threshold for other suggest candidates.
PhraseSuggester
:
returns
this
so that calls can be chained.
Sets the maximum percentage of the terms that at most considered to be
misspellings in order to form a correction. This method accepts a float
value in the range [0..1)
as a fraction of the actual query terms or a
number >=1
as an absolute number of query terms. The default is set
to 1.0
which corresponds to that only corrections with at most
1 misspelled term are returned. Note that setting this too high can
negatively impact performance. Low values like 1 or 2 are recommended
otherwise the time spend in suggest calls might exceed the time spend
in query execution.
(number)
The maximum percentage of the terms that at most considered
to be misspellings in order to form a correction.
PhraseSuggester
:
returns
this
so that calls can be chained.
Sets the separator that is used to separate terms in the bigram field. If not set the whitespace character is used as a separator.
(string)
The separator that is used to separate terms in the
bigram field.
PhraseSuggester
:
returns
this
so that calls can be chained.
Sets up suggestion highlighting. If not provided then no highlighted
field
is returned. If provided must contain exactly pre_tag
and post_tag
which
are wrapped around the changed tokens. If multiple tokens in a row are changed
the entire phrase of changed tokens is wrapped rather than each token.
PhraseSuggester
:
returns
this
so that calls can be chained.
Checks each suggestion against the specified query
to prune suggestions
for which no matching docs exist in the index. The collate query for
a suggestion is run only on the local shard from which the suggestion
has been generated from. The query
must be specified, and it is run
as a template
query.
The current suggestion is automatically made available as the
{{suggestion}}
variable, which should be used in your query.
Additionally, you can specify a prune
to control if all phrase
suggestions will be returned, when set to true
the suggestions will
have an additional option collate_match
, which will be true if matching
documents for the phrase was found, false
otherwise. The default value
for prune is false
.
(Object)
The options for
collate
. Can include the following:
query
: The query
to prune suggestions for which
no matching docs exist in the index. It is run as a template
query.params
: The parameters to be passed to the template. The suggestion
value will be added to the variables you specify.prune
: When set to true
, the suggestions will
have an additional option collate_match
, which will be true if matching
documents for the phrase was found, false
otherwise. The default value
for prune is false
.PhraseSuggester
:
returns
this
so that calls can be chained.
const suggest = esb.phraseSuggester('simple_phrase', 'title.trigram')
.size(1)
.directGenerator(
esb.directGenerator('title.trigram')
.suggestMode('always')
.minWordLength(1)
)
.collate({
query: {
inline: {
match: {
'{{field_name}}': '{{suggestion}}'
}
}
},
params: { field_name: 'title' },
prune: true
});
Sets the smoothing model to balance weight between infrequent grams (grams (shingles) are not existing in the index) and frequent grams (appear at least once in the index).
Three possible values can be specified:
stupid_backoff
: a simple backoff model that backs off to lower order
n-gram models if the higher order count is 0 and discounts the lower order
n-gram model by a constant factor. The default discount
is 0.4
.
Stupid Backoff is the default modellaplace
: a smoothing model that uses an additive smoothing where a
constant (typically 1.0
or smaller) is added to all counts to balance weights,
The default alpha
is 0.5
.linear_interpolation
: a smoothing model that takes the weighted mean of the
unigrams, bigrams and trigrams based on user supplied weights (lambdas).
Linear Interpolation doesn’t have any default values.
All parameters (trigram_lambda
, bigram_lambda
, unigram_lambda
)
must be supplied.PhraseSuggester
:
returns
this
so that calls can be chained.
Sets the given list of candicate generators which produce a list of possible terms per term in the given text. Each of the generators in the list are called per term in the original text.
The output of the generators is subsequently scored in combination with the candidates from the other terms to for suggestion candidates.
((Array<DirectGenerator> | DirectGenerator))
Array of
DirectGenerator
instances or a single instance of
DirectGenerator
PhraseSuggester
:
returns
this
so that calls can be chained.
const suggest = esb.phraseSuggester('simple_phrase', 'title.trigram')
.size(1)
.directGenerator([
esb.directGenerator('title.trigram').suggestMode('always'),
esb.directGenerator('title.reverse')
.suggestMode('always')
.preFilter('reverse')
.postFilter('reverse')
]);
The completion suggester provides auto-complete/search-as-you-type functionality. This is a navigational feature to guide users to relevant results as they are typing, improving search precision. It is not meant for spell correction or did-you-mean functionality like the term or phrase suggesters.
Ideally, auto-complete functionality should be as fast as a user types to provide instant feedback relevant to what a user has already typed in. Hence, completion suggester is optimized for speed. The suggester uses data structures that enable fast lookups, but are costly to build and are stored in-memory.
Elasticsearch reference
Extends Suggester
(string)
The name of the Suggester, an arbitrary identifier
(string?)
The field to fetch the candidate suggestions from.
name
is empty
const suggest = esb.completionSuggester('song-suggest', 'suggest').prefix('nir');
const suggest = new esb.CompletionSuggester('place_suggestion', 'suggest')
.prefix('tim')
.size(10)
.contexts('place_type', ['cafe', 'restaurants']);
Sets the prefix
for the CompletionSuggester
query.
(string)
CompletionSuggester
:
returns
this
so that calls can be chained.
Sets whether duplicate suggestions should be filtered out (defaults to false).
NOTE: This option was added in elasticsearch v6.1.
(boolean
= true
)
Enable/disable skipping duplicates
CompletionSuggester
:
returns
this
so that calls can be chained.
Sets the fuzzy
parameter. Can be customised with specific fuzzy parameters.
CompletionSuggester
:
returns
this
so that calls can be chained.
Sets the fuzziness
parameter which is interpreted as a Levenshtein Edit Distance —
the number of one character changes that need to be made to one string to make it
the same as another string.
CompletionSuggester
:
returns
this
so that calls can be chained.
const suggest = esb.completionSuggester('song-suggest', 'suggest')
.prefix('nor')
.fuzziness(2);
Transpositions (ab
→ ba
) are allowed by default but can be disabled
by setting transpositions
to false.
(boolean)
CompletionSuggester
:
returns
this
so that calls can be chained.
Sets the minimum length of the input before fuzzy suggestions are returned, defaults 3
(number)
Minimum length of the input before fuzzy suggestions
are returned, defaults 3
CompletionSuggester
:
returns
this
so that calls can be chained.
The number of initial characters which will not be "fuzzified".
This helps to reduce the number of terms which must be examined. Defaults to 1
.
CompletionSuggester
:
returns
this
so that calls can be chained.
If true
, all measurements (like fuzzy edit distance, transpositions,
and lengths) are measured in Unicode code points instead of in bytes.
This is slightly slower than raw bytes, so it is set to false
by default.
CompletionSuggester
:
returns
this
so that calls can be chained.
Sets the regular expression for completion suggester which supports regex queries.
(string)
Regular expression
CompletionSuggester
:
returns
this
so that calls can be chained.
const suggest = esb.completionSuggester('song-suggest', 'suggest')
.regex('n[ever|i]r');
Set special flags. Possible flags are ALL
(default),
ANYSTRING
, COMPLEMENT
, EMPTY
, INTERSECTION
, INTERVAL
, or NONE
.
(string)
|
separated flags. Possible flags are
ALL
(default),
ANYSTRING
,
COMPLEMENT
,
EMPTY
,
INTERSECTION
,
INTERVAL
, or
NONE
.
CompletionSuggester
:
returns
this
so that calls can be chained.
Limit on how many automaton states regexp queries are allowed to create. This protects against too-difficult (e.g. exponentially hard) regexps. Defaults to 10000. You can raise this limit to allow more complex regular expressions to execute.
(number)
CompletionSuggester
:
returns
this
so that calls can be chained.
The completion suggester considers all documents in the index, but it is often desirable to serve suggestions filtered and/or boosted by some criteria.
To achieve suggestion filtering and/or boosting, you can add context mappings while configuring a completion field. You can define multiple context mappings for a completion field. Every context mapping has a unique name and a type.
CompletionSuggester
:
returns
this
so that calls can be chained.
const suggest = new esb.CompletionSuggester('place_suggestion', 'suggest')
.prefix('tim')
.size(10)
.contexts('place_type', [
{ context: 'cafe' },
{ context: 'restaurants', boost: 2 }
]);
// Suggestions can be filtered and boosted with respect to how close they
// are to one or more geo points. The following filters suggestions that
// fall within the area represented by the encoded geohash of a geo point:
const suggest = new esb.CompletionSuggester('place_suggestion', 'suggest')
.prefix('tim')
.size(10)
.contexts('location', { lat: 43.662, lon: -79.38 });
// Suggestions that are within an area represented by a geohash can also be
// boosted higher than others
const suggest = new esb.CompletionSuggester('place_suggestion', 'suggest')
.prefix('tim')
.size(10)
.contexts('location', [
{
lat: 43.6624803,
lon: -79.3863353,
precision: 2
},
{
context: {
lat: 43.6624803,
lon: -79.3863353
},
boost: 2
}
]);
Allows to highlight search results on one or more fields. In order to perform highlighting, the actual content of the field is required. If the field in question is stored (has store set to yes in the mapping), it will be used, otherwise, the actual _source will be loaded and the relevant field will be extracted from it.
If no term_vector information is provided (by setting it to
with_positions_offsets
in the mapping), then the plain highlighter will be
used. If it is provided, then the fast vector highlighter will be used.
When term vectors are available, highlighting will be performed faster at
the cost of bigger index size.
const reqBody = esb.requestBodySearch()
.query(esb.matchAllQuery())
.highlight(esb.highlight('content'));
const highlight = esb.highlight()
.numberOfFragments(3)
.fragmentSize(150)
.fields(['_all', 'bio.title', 'bio.author', 'bio.content'])
.preTags('<em>', '_all')
.postTags('</em>', '_all')
.numberOfFragments(0, 'bio.title')
.numberOfFragments(0, 'bio.author')
.numberOfFragments(5, 'bio.content')
.scoreOrder('bio.content');
highlight.toJSON()
{
"number_of_fragments" : 3,
"fragment_size" : 150,
"fields" : {
"_all" : { "pre_tags" : ["<em>"], "post_tags" : ["</em>"] },
"bio.title" : { "number_of_fragments" : 0 },
"bio.author" : { "number_of_fragments" : 0 },
"bio.content" : { "number_of_fragments" : 5, "order" : "score" }
}
}
Sets the pre tags for highlighted fragments. You can apply the tags to a specific field by passing the optional field name parameter.
Highlight
:
returns
this
so that calls can be chained
const highlight = esb.highlight('_all')
.preTags('<tag1>')
.postTags('</tag1>');
const highlight = esb.highlight('_all')
.preTags(['<tag1>', '<tag2>'])
.postTags(['</tag1>', '</tag2>']);
Sets the post tags for highlighted fragments. You can apply the tags to a specific field by passing the optional field name parameter.
Highlight
:
returns
this
so that calls can be chained
const highlight = esb.highlight('_all')
.preTags('<tag1>')
.postTags('</tag1>');
const highlight = esb.highlight('_all')
.preTags(['<tag1>', '<tag2>'])
.postTags(['</tag1>', '</tag2>']);
Sets the order of highlight fragments to be sorted by score. You can apply the score order to a specific field by passing the optional field name parameter.
(string?)
An optional field name
Highlight
:
returns
this
so that calls can be chained
const highlight = esb.highlight('content').scoreOrder()
Sets the size of each highlight fragment in characters. You can apply the option to a specific field by passing the optional field name parameter.
(number)
The fragment size in characters. Defaults to 100.
(string?)
An optional field name
Highlight
:
returns
this
so that calls can be chained
const highlight = esb.highlight('content')
.fragmentSize(150, 'content')
.numberOfFragments(3, 'content');
Sets the maximum number of fragments to return. You can apply the option to a specific field by passing the optional field name parameter.
Highlight
:
returns
this
so that calls can be chained
const highlight = esb.highlight('content')
.fragmentSize(150, 'content')
.numberOfFragments(3, 'content');
const highlight = esb.highlight(['_all', 'bio.title'])
.numberOfFragments(0, 'bio.title');
If no_match_size
is set, in the case where there is no matching fragment
to highlight, a snippet of text, with the specified length, from the beginning
of the field will be returned.
The actual length may be shorter than specified as it tries to break on a word boundary.
Default is 0
.
Highlight
:
returns
this
so that calls can be chained
const highlight = esb.highlight('content')
.fragmentSize(150, 'content')
.numberOfFragments(3, 'content')
.noMatchSize(150, 'content');
Highlight against a query other than the search query. Useful if you use a rescore query because those are not taken into account by highlighting by default.
Highlight
:
returns
this
so that calls can be chained
query
is not an instance of
Query
const highlight = esb.highlight('content')
.fragmentSize(150, 'content')
.numberOfFragments(3, 'content')
.highlightQuery(
esb.boolQuery()
.must(esb.matchQuery('content', 'foo bar'))
.should(
esb.matchPhraseQuery('content', 'foo bar').slop(1).boost(10)
)
.minimumShouldMatch(0),
'content'
);
Combine matches on multiple fields to highlight a single field.
Useful for multifields that analyze the same string in different ways.
Sets the highlight type to Fast Vector Highlighter(fvh
).
Highlight
:
returns
this
so that calls can be chained
const highlight = esb.highlight('content')
.scoreOrder('content')
.matchedFields(['content', 'content.plain'], 'content');
highlight.toJSON();
{
"order": "score",
"fields": {
"content": {
"matched_fields": ["content", "content.plain"],
"type" : "fvh"
}
}
}
The fast vector highlighter has a phrase_limit parameter that prevents it from analyzing too many phrases and eating tons of memory. It defaults to 256 so only the first 256 matching phrases in the document scored considered. You can raise the limit with the phrase_limit parameter.
If using matched_fields
, phrase_limit
phrases per matched field
are considered.
(number)
Defaults to 256.
Highlight
:
returns
this
so that calls can be chained
Can be used to define how highlighted text will be encoded.
(string)
It can be either default (no encoding)
or
html
(will escape
html
, if you use html highlighting tags)
Highlight
:
returns
this
so that calls can be chained
default
or
html
By default only fields that hold a query match will be highlighted. This can be set to false to highlight the field regardless of whether the query matched specifically on them. You can apply the option to a specific field by passing the optional field name parameter.
Highlight
:
returns
this
so that calls can be chained
const highlight = esb.highlight('_all')
.preTags('<em>', '_all')
.postTags('</em>', '_all')
.requireFieldMatch(false);
Allows to control how far to look for boundary characters, and defaults to 20. You can apply the option to a specific field by passing the optional field name parameter.
Highlight
:
returns
this
so that calls can be chained
Defines what constitutes a boundary for highlighting.
It is a single string with each boundary character defined in it.
It defaults to .,!? \t\n
. You can apply the
option to a specific field by passing the optional field name parameter.
Highlight
:
returns
this
so that calls can be chained
Allows to force a specific highlighter type. This is useful for instance when needing to use the plain highlighter on a field that has term_vectors enabled. You can apply the option to a specific field by passing the optional field name parameter.
Note: The postings
highlighter has been removed in elasticsearch 6.0.
The unified
highlighter outputs the same highlighting when
index_options
is set to offsets
.
(string?)
An optional field name
Highlight
:
returns
this
so that calls can be chained
plain
,
postings
or
fvh
.
const highlight = esb.highlight('content').type('plain', 'content');
Forces the highlighting to highlight fields based on the source even if fields are stored separately. Defaults to false.
Highlight
:
returns
this
so that calls can be chained
const highlight = esb.highlight('content').forceSource(true, 'content');
Sets the fragmenter type. You can apply the option to a specific field by passing the optional field name parameter. Valid values for order are:
simple
- breaks text up into same-size fragments with no concerns
over spotting sentence boundaries.span
- breaks text up into same-size fragments but does not split
up Spans.Highlight
:
returns
this
so that calls can be chained
simple
or
span
const highlight = esb.highlight('message')
.fragmentSize(15, 'message')
.numberOfFragments(3, 'message')
.fragmenter('simple', 'message');
Class supporting the Elasticsearch scripting API.
Note: inline
script type was deprecated in elasticsearch v5.0.
source
should be used instead. And similarly for stored
scripts, type
id
must be used instead. file
scripts were removed as part of the
breaking changes in elasticsearch v6.0
(string?)
Source of the script.
This needs to be specified if optional argument
type
is passed.
const script = esb.script('inline', "doc['my_field'] * multiplier")
.lang('expression')
.params({ multiplier: 2 });
// cat "log(_score * 2) + my_modifier" > config/scripts/calculate-score.groovy
const script = esb.script()
.lang('groovy')
.file('calculate-score')
.params({ my_modifier: 2 });
Sets the type of script to be source
and specifies the source of the script.
Note: source
is an alias for the inline
type which was deprecated
in elasticsearch v5.0. So this type is supported only in versions
>= 5.0
.
(string)
Script
:
returns
this
so that calls can be chained.
Specify the stored
script by id
which will be retrieved from cluster state.
Note: This type was deprecated in elasticsearch v5.0. Use id
instead if you are using elasticsearch >= 5.0
.
(string)
The unique identifier for the stored script.
Script
:
returns
this
so that calls can be chained.
Specify the stored script to be used by it's id
which will be retrieved
from cluster state.
Note: id
is an alias for the stored
type which was deprecated in
elasticsearch v5.0. So this type is supported only in versions >= 5.0
.
(string)
The unique identifier for the stored script.
Script
:
returns
this
so that calls can be chained.
Specify the file
script by stored as a file in the scripts folder.
Note: File scripts have been removed in elasticsearch 6.0. Instead, use stored scripts.
(string)
The name of the script stored as a file in the scripts folder.
For script file
config/scripts/calculate-score.groovy
,
fileName
should be
calculate-score
Script
:
returns
this
so that calls can be chained.
Specifies the language the script is written in. Defaults to painless
but
may be set to any of languages listed in Scripting.
The default language may be changed in the elasticsearch.yml
config file by setting
script.default_lang
to the appropriate language.
For a file
script, it should correspond with the script file suffix.
groovy
for config/scripts/calculate-score.groovy
.
Note: The Groovy, JavaScript, and Python scripting languages were deprecated in elasticsearch 5.0 and removed in 6.0. Use painless instead.
(string)
The language for the script.
Script
:
returns
this
so that calls can be chained.
A GeoPoint
object that can be used in queries and filters that
take a GeoPoint
. GeoPoint
supports various input formats.
Shape object that can be used in queries and filters that take a Shape. Shape uses the GeoJSON format.
(string?)
A valid shape type.
Can be one of
point
,
linestring
,
polygon
,
multipoint
,
multilinestring
,
multipolygon
,
geometrycollection
,
envelope
and
circle
(Array?)
A valid coordinat definition for the given shape.
// Pass options using method
const shape = esb.geoShape()
.type('linestring')
.coordinates([[-77.03653, 38.897676], [-77.009051, 38.889939]]);
// Pass parameters using contructor
const shape = esb.geoShape('multipoint', [[102.0, 2.0], [103.0, 2.0]])
Sets the GeoJSON format type used to represent shape.
(string)
A valid shape type.
Can be one of
point
,
linestring
,
polygon
,
multipoint
,
multilinestring
,
multipolygon
,
geometrycollection
,
envelope
,
circle
GeoShape
:
returns
this
so that calls can be chained.
const shape = esb.geoShape()
.type('envelope')
.coordinates([[-45.0, 45.0], [45.0, -45.0]])
Sets the coordinates for the shape definition. Note, the coordinates are not validated in this api. Please see GeoJSON and ElasticSearch documentation for correct coordinate definitions.
GeoShape
:
returns
this
so that calls can be chained.
const shape = esb.geoShape()
.type('point')
.coordinates([-77.03653, 38.897676])
Sets the radius for parsing a circle GeoShape
.
GeoShape
:
returns
this
so that calls can be chained.
const shape = esb.geoShape()
.type('circle')
.coordinates([-45.0, 45.0])
.radius('100m')
A shape which has already been indexed in another index and/or index type. This is particularly useful for when you have a pre-defined list of shapes which are useful to your application and you want to reference this using a logical name (for example 'New Zealand') rather than having to provide their coordinates each time.
(string?)
The document id of the shape.
(string?)
The name of the type where the shape is indexed.
const shape = esb.indexedShape('DEU', 'countries')
.index('shapes')
.path('location');
const shape = esb.indexedShape()
.id('DEU')
.type('countries')
.index('shapes')
.path('location');
Sets the ID of the document that containing the pre-indexed shape.
(string)
The document id of the shape.
IndexedShape
:
returns
this
so that calls can be chained.
Sets the index type where the pre-indexed shape is.
(string)
The name of the type where the shape is indexed.
IndexedShape
:
returns
this
so that calls can be chained.
Sets the name of the index where the pre-indexed shape is. Defaults to shapes
.
(string)
A valid index name
IndexedShape
:
returns
this
so that calls can be chained.
Sets the field specified as path containing the pre-indexed shape.
Defaults to shape
.
(string)
field name.
IndexedShape
:
returns
this
so that calls can be chained.
Allows creating and configuring sort on specified field.
(string?)
The field to sort on.
If a script is used to specify the sort order,
field
should be omitted.
(string?)
The
order
option can have the following values.
asc
,
desc
to sort in ascending, descending order respectively.
const reqBody = esb.requestBodySearch()
.query(esb.termQuery('user', 'kimchy'))
.sort(esb.sort('post_date', 'asc'))
Set order for sorting. The order defaults to desc
when sorting on the _score
,
and defaults to asc
when sorting on anything else.
(string)
The
order
option can have the following values.
asc
,
desc
to sort in ascending, descending order respectively.
Sort
:
returns
this
so that calls can be chained.
Elasticsearch supports sorting by array or multi-valued fields.
The mode
option controls what array value is picked for sorting the
document it belongs to.
The mode
option can have the following values:
min
- Pick the lowest value.max
- Pick the highest value.sum
- Use the sum of all values as sort value.
Only applicable for number based array fields.avg
- Use the average of all values as sort value.
Only applicable for number based array fields.median
- Use the median of all values as sort value.
Only applicable for number based array fields.Sort
:
returns
this
so that calls can be chained.
const sort = esb.sort('price', 'asc').mode('avg');
Defines on which nested object to sort. The actual sort field must be a direct field inside this nested object. When sorting by nested field, this field is mandatory.
Note: This method has been deprecated in elasticsearch 6.1. From 6.1 and
later, use nested
method instead.
(string)
Nested object to sort on
Sort
:
returns
this
so that calls can be chained.
const sort = esb.sort('offer.price', 'asc')
.nestedPath('offer')
.nestedFilter(esb.termQuery('offer.color', 'blue'));
A filter that the inner objects inside the nested path should match with in order
for its field values to be taken into account by sorting. By default no
nested_filter
is active.
Note: This method has been deprecated in elasticsearch 6.1. From 6.1 and
later, use nested
method instead.
(Query)
Filter query
Sort
:
returns
this
so that calls can be chained.
Query
const sort = esb.sort('offer.price', 'asc')
.nestedPath('offer')
.nestedFilter(esb.termQuery('offer.color', 'blue'));
Defines on which nested object to sort and the filter that the inner objects inside the nested path should match with in order for its field values to be taken into account by sorting
Note: This method is incompatible with elasticsearch 6.0 and older. Use it only with elasticsearch 6.1 and later.
Sort
:
returns
this
so that calls can be chained.
Query
const sort = esb.sort('offer.price', 'asc')
.nested({
path: 'offer',
filter: esb.termQuery('offer.color', 'blue')
});
The missing parameter specifies how docs which are missing the field should
be treated: The missing value can be set to _last
, _first
, or a custom value
(that will be used for missing docs as the sort value). The default is _last
.
Sort
:
returns
this
so that calls can be chained.
const sort = esb.sort('price').missing('_last');
By default, the search request will fail if there is no mapping associated with
a field. The unmapped_type
option allows to ignore fields that have no mapping
and not sort by them. The value of this parameter is used to determine what sort
values to emit.
(string)
Sort
:
returns
this
so that calls can be chained.
const sort = esb.sort('price').unmappedType('long');
Sorts documents by distance of the geo point field from reference point.
If multiple reference points are specified, the final distance for a
document will then be min
/max
/avg
(defined via mode
) distance of all
points contained in the document to all points given in the sort request.
Sort
:
returns
this
so that calls can be chained.
const sort = esb.sort('pin.location', 'asc')
.geoDistance([-70, 40])
.unit('km')
.mode('min')
.distanceType('arc');
Sorts based on custom script. When sorting on a field, scores are not computed.
(Script)
Sort
:
returns
this
so that calls can be chained
script
is not an instance of
Script
const sort = esb.sort()
.type('number')
.script(
esb.script('inline', "doc['field_name'].value * params.factor")
.lang('painless')
.params({ factor: 1.1 })
)
.order('asc');
Sets the format of the date when sorting a date field.
Note: The format argument is supported since version 7.13 of ElasticSearch.
(string)
Sort
:
returns
this
so that calls can be chained
A rescore
request can help to improve precision by reordering just
the top (eg 100 - 500) documents returned by the query
and post_filter
phases, using a secondary (usually more costly) algorithm, instead of
applying the costly algorithm to all documents in the index.
The rescore phase is not executed when sort is used.
const reqBody = esb.requestBodySearch()
.query(esb.matchQuery('message', 'the quick brown').operator('or'))
.rescore(
esb.rescore(
50,
esb.matchPhraseQuery('message', 'the quick brown').slop(2)
)
.queryWeight(0.7)
.rescoreQueryWeight(1.2)
);
const rescore = esb.rescore(
10,
esb.functionScoreQuery().function(
esb.scriptScoreFunction(
esb.script('inline', 'Math.log10(doc.likes.value + 2)')
)
)
).scoreMode('multiply');
Controls the way the scores are combined.
Rescore
:
returns
this
so that calls can be chained.
Inner hits returns per search hit in the search response additional
nested hits that caused a search hit to match in a different scope.
Inner hits can be used by defining an inner_hits
definition on a
nested
, has_child
or has_parent
query and filter.
(string?)
The name to be used for the particular inner hit definition
in the response. Useful when multiple inner hits have been defined in a single
search request. The default depends in which query the inner hit is defined.
const reqBody = esb.requestBodySearch().query(
esb.nestedQuery(
esb.matchQuery('comments.message', '[actual query]')
).innerHits(
esb.innerHits().source(false).storedFields(['comments.text'])
)
);
The name to be used for the particular inner hit definition in the response. Useful when multiple inner hits have been defined in a single search request. The default depends in which query the inner hit is defined.
(number)
InnerHits
:
returns
this
so that calls can be chained.
Allows to add multiple sort on specific fields. Each sort can be reversed as well. The sort is defined on a per field level, with special field name for _score to sort by score, and _doc to sort by index order.
InnerHits
:
returns
this
so that calls can be chained.
sorts
is not an instance of
Sort
.
Allows to highlight search results on one or more fields. The implementation
uses either the lucene plain
highlighter, the fast vector highlighter (fvh
)
or postings
highlighter.
Note: The postings
highlighter has been removed in elasticsearch 6.0.
The unified
highlighter outputs the same highlighting when
index_options
is set to offsets
.
(Highlight)
InnerHits
:
returns
this
so that calls can be chained
Allows to control how the _source
field is returned with every hit.
You can turn off _source
retrieval by passing false
.
It also accepts one(string) or more wildcard(array) patterns to control
what parts of the _source
should be returned
An object can also be used to specify the wildcard patterns for includes
and excludes
.
InnerHits
:
returns
this
so that calls can be chained
Computes a document property dynamically based on the supplied Script
.
InnerHits
:
returns
this
so that calls can be chained
Sets given dynamic document properties to be computed using supplied Script
s.
Object should have scriptFieldName
as key and script
as the value.
InnerHits
:
returns
this
so that calls can be chained
Class supporting the Elasticsearch search template API.
The /_search/template
endpoint allows to use the mustache language to
pre render search requests, before they are executed and fill existing
templates with template parameters.
const templ = esb.searchTemplate('inline', {
query: esb.matchQuery('{{my_field}}', '{{my_value}}'),
size: '{{my_size}}'
}).params({
my_field: 'message',
my_value: 'some message',
my_size: 5
});
const templ = new esb.SearchTemplate(
'inline',
'{ "query": { "terms": {{#toJson}}statuses{{/toJson}} }}'
).params({
statuses: {
status: ['pending', 'published']
}
});
const templ = new esb.SearchTemplate(
'inline',
'{ "query": { "bool": { "must": {{#toJson}}clauses{{/toJson}} } } }'
).params({
clauses: [
esb.termQuery('user', 'boo'),
esb.termQuery('user', 'bar'),
esb.termQuery('user', 'baz')
]
});
Sets the type of search template to be inline
and specifies the
template with query
and other optional fields such as size
.
SearchTemplate
:
returns
this
so that calls can be chained.
Specify the indexed search template by templateName
which will be
retrieved from cluster state.
(string)
The unique identifier for the indexed template.
SearchTemplate
:
returns
this
so that calls can be chained.
Specify the indexed search template by templateName
which will be
retrieved from cluster state.
Alias for SearchTemplate.id
(string)
The unique identifier for the indexed template.
SearchTemplate
:
returns
this
so that calls can be chained.
Specify the search template by filename stored in the scripts folder,
with mustache
extension.
(string)
The name of the search template stored as a file
in the scripts folder.
For file
config/scripts/storedTemplate.mustache
,
fileName
should be
storedTemplate
SearchTemplate
:
returns
this
so that calls can be chained.
// `templId` - Name of the query template in config/scripts/, i.e.,
// storedTemplate.mustache.
const templ = new esb.SearchTemplate('file', 'storedTemplate').params({
query_string: 'search for these words'
});
Specifies any named parameters that are used to render the search template.
(Object)
Named parameters to be used for rendering.
SearchTemplate
:
returns
this
so that calls can be chained.
Helper recipes for common query use cases.
If you have any recipes, please do share or better yet, create a pull request.
Recipes:
These can be accessed under the recipes
namespace or
using the cook[Recipe Name]
alias for ease of use.
// `recipes` namespace
const qry = esb.recipes.missingQuery('user');
// `cookMissingQuery` alias
const qry = esb.cookMissingQuery('user');
Class representing a k-Nearest Neighbors (k-NN) query. This class extends the Query class to support the specifics of k-NN search, including setting up the field, query vector, number of neighbors (k), and number of candidates.
(any)
(any)
(any)
const qry = esb.kNN('my_field', 100, 1000).vector([1,2,3]);
const qry = esb.kNN('my_field', 100, 1000).queryVectorBuilder('model_123', 'Sample model text');
NOTE: kNN search was added to Elasticsearch in v8.0
[Elasticsearch reference](https://www.elastic.co/guide/en/elasticsearch/reference/current/knn-search.html)
Sets the query vector builder for the k-NN search. This method configures a query vector builder using a specified model ID and model text. It's important to note that either a direct query vector or a query vector builder can be provided, but not both.
(string)
The ID of the model to be used for generating the query vector.
(string)
The text input based on which the query vector is generated.
KNN
:
Returns the instance of KNN for method chaining.
let knn = new esb.KNN().queryVectorBuilder('model_123', 'Sample model text');
Adds one or more filter queries to the k-NN search.
This method is designed to apply filters to the k-NN search. It accepts either a single
query or an array of queries. Each query acts as a filter, refining the search results
according to the specified conditions. These queries must be instances of the Query
class.
If any provided query is not an instance of Query
, a TypeError is thrown.
KNN
:
Returns
this
to allow method chaining.
Query
.
let knn = new esb.KNN().filter(new esb.TermQuery('field', 'value')); // Applying a single filter query
let knn = new esb.KNN().filter([
new esb.TermQuery('field1', 'value1'),
new esb.TermQuery('field2', 'value2')
]); // Applying multiple filter queries
Echos the value of a value. Trys to print the value out in the best way possible given the different types.
string
:
Class supporting the Elasticsearch runtime field.
Added in Elasticsearch v7.11.0 Release note
(string?)
One of
boolean
,
composite
,
date
,
double
,
geo_point
,
ip
,
keyword
,
long
,
lookup
.
(string?)
Source of the script.
const field = esb.runtimeField('keyword', `emit(doc['sessionId'].value + '::' + doc['name'].value)`);
Sets the source of the script.
(string)
RuntimeField
:
returns
this
so that calls can be chained.
Sets the type of the runtime field.
(string)
One of
boolean
,
composite
,
date
,
double
,
geo_point
,
ip
,
keyword
,
long
,
lookup
.
RuntimeField
:
returns
this
so that calls can be chained.
Specifies the language the script is written in. Defaults to painless
but
may be set to any of languages listed in Scripting.
(string)
The language for the script.
RuntimeField
:
returns
this
so that calls can be chained.
Specifies any named parameters that are passed into the script as variables.
(Object)
Named parameters to be passed to script.
RuntimeField
:
returns
this
so that calls can be chained.
Extends FullTextQueryBase
const qry = esb.combinedFieldsQuery(['subject', 'message'], 'this is a test');
NOTE: This query was added in elasticsearch v7.13.
Appends given field to the list of fields to search against.
Fields can be specified with wildcards.
Individual fields can be boosted with the caret (^) notation.
Example - "subject^3"
(string)
One of the fields to be queried
CombinedFieldsQuery
:
returns
this
so that calls can be chained.
Appends given fields to the list of fields to search against. Fields can be specified with wildcards. Individual fields can be boosted with the caret (^) notation.
CombinedFieldsQuery
:
returns
this
so that calls can be chained.
// Boost individual fields with caret `^` notation
const qry = esb.combinedFieldsQuery(['subject^3', 'message'], 'this is a test');
// Specify fields with wildcards
const qry = esb.combinedFieldsQuery(['title', '*_name'], 'Will Smith');
If true, match phrase queries are automatically created for multi-term synonyms.
CombinedFieldsQuery
:
returns
this
so that calls can be chained.
The operator to be used in the boolean query which is constructed
by analyzing the text provided. The operator
flag can be set to or
or
and
to control the boolean clauses (defaults to or
).
CombinedFieldsQuery
:
returns
this
so that calls can be chained.
If the analyzer used removes all tokens in a query like a stop
filter does,
the default behavior is to match no documents at all. In order to change that
the zero_terms_query
option can be used, which accepts none
(default) and all
which corresponds to a match_all
query.
CombinedFieldsQuery
:
returns
this
so that calls can be chained.
const qry = esb.combinedFieldsQuery('message', 'to be or not to be')
.operator('and')
.zeroTermsQuery('all');
A query that uses a script to provide a custom score for returned documents.
NOTE: This query was added in elasticsearch v7.0.
Extends Query
const qry = esb.scriptScoreQuery()
.query(esb.matchQuery("message", "elasticsearch"))
.script(esb.script().source("doc['my-int'].value / 10"))
Sets the query used to return documents.
ScriptScoreQuery
:
returns
this
so that calls can be chained.
Sets the script used to compute the score of documents returned by the query.
ScriptScoreQuery
:
returns
this
so that calls can be chained.
Sets the minimum score limit for documents to be included in search result.
(number)
Minimum score threshold
ScriptScoreQuery
:
returns
this
so that calls can be chained.
The distance_feature
query can be used to filter documents that are inside
a timeframe or radius given an origin point. For dates the difference can be
minutes, hours, etc and for coordinates it can be meters, kilometers..
NOTE: Only available in Elasticsearch 7.1.0+.
Extends Query
(string)
The field inside the document to be used in the query
const query = new DistanceFeatureQuery('time');
query
.origin('now')
.pivot('1h')
.toJSON();
Sets the field for the distance_feature
query
(string)
Name of the field inside the document
DistanceFeatureQuery
:
Instance of the distance feature query
Sets the origin of the function. Date or point of coordinates used to calculate distances
DistanceFeatureQuery
:
Instance of the distance feature query
Distance from the origin at which relevance scores receive half of the boost value.
(string)
Distance value. If the field value is date then this must be a
time unit
.
If it's a geo point field, then a
distance unit
DistanceFeatureQuery
:
Instance of the distance feature query
The rank_feature query boosts the relevance score on the numeric value of document with a rank_feature/rank_features field.
NOTE: This query was added in elasticsearch v7.0.
Extends Query
(string)
The field inside the document to be used in the query
const query = new RankFeatureQuery('rank_feature_field');
query
.linear()
.toJSON();
Sets the field for the rank_feature
query
(string)
Name of the field inside the document
RankFeatureQuery
:
Instance of the distance feature query
Linear function to boost relevance scores based on the value of the rank feature field
RankFeatureQuery
:
Saturation function to boost relevance scores based on the value of the rank feature field. Uses a default pivot value computed by Elasticsearch.
RankFeatureQuery
:
Saturation function to boost relevance scores based on the value of the rank feature field.
(number)
RankFeatureQuery
:
The log function gives a score equal to log(scaling_factor + S), where S is the value of the rank feature field and scaling_factor is a configurable scaling factor.
(any)
(number)
RankFeatureQuery
:
The sigmoid function extends the saturation function with a configurable exponent.
RankFeatureQuery
:
A single-value metrics aggregation that computes the weighted average of numeric values that are extracted from the aggregated documents. These values can be extracted either from specific numeric fields in the documents.
Added in Elasticsearch v6.4.0 Release notes
As a formula, a weighted average is ∑(value * weight) / ∑(weight)
Extends MetricsAggregationBase
(string)
The name which will be used to refer to this aggregation.
(string?)
The field or script to be used as the value.
(string?)
The field or script to be used as the weighting.
// Compute the average grade over all documents, weighing by teacher score.
const agg = esb.weightedAverageAggregation('avg_grade', 'grade', 'teacher_score');
// Compute the average grade where the weight is calculated by a script.
// Filling in missing values as '10'.
const agg = esb.weightedAverageAggregation('avg_grade', 'grade')
.weight(esb.script('inline', "doc['teacher_score'].value").lang('painless'), 10)
);
// Compute the average grade, weighted by teacher score, filling in missing values.
const agg = esb.weightedAverageAggregation('avg_grade').value('grade', 5).weight('teacher_score', 10));
// Compute the average grade over all documents, weighing by teacher score.
const agg = esb.weightedAverageAggregation('avg_grade').value('grade').weight('teacher_score');
Sets the value
(number?)
Sets the missing parameter which defines how documents
that are missing a value should be treated.
WeightedAverageAggregation
:
returns
this
so that calls can be chained
Sets the weight
(number?)
Sets the missing parameter which defines how documents
that are missing a value should be treated.
WeightedAverageAggregation
:
returns
this
so that calls can be chained
This is a multi-bucket aggregation similar to Histogram. However, the width of each bucket is not specified. Rather, a target number of buckets is provided and bucket intervals are dynamically determined based on the document distribution. This is done using a simple one-pass document clustering algorithm that aims to obtain low distances between bucket centroids. Unlike other multi-bucket aggregations, the intervals will not necessarily have a uniform width.
NOTE: Only available in Elasticsearch v7.9.0+
Extends BucketAggregationBase
(string)
The name which will be used to refer to this aggregation.
(string?)
The field to aggregate on
(number?)
Bucket count to generate histogram over.
const agg = esb.variableWidthHistogramAggregation('price', 'lowestPrice', 10)
Sets the histogram bucket count. Buckets are generated based on this value.
(number)
Bucket count to generate histogram over.
VariableWidthHistogramAggregation
:
returns
this
so that calls can be chained
A multi-bucket aggregation that groups geo_point and geo_shape values into buckets that represent a grid. The resulting grid can be sparse and only contains cells that have matching data. Each cell corresponds to a H3 cell index and is labeled using the H3Index representation.
NOTE: This aggregation was added in elasticsearch v8.1.0.
Extends BucketAggregationBase
(string)
The name which will be used to refer to this aggregation.
(string?)
The field to aggregate on
const agg = esb.geoHexGridAggregation('hex-grid', 'location').precision(3);
Sets the precision for the generated geohex.
(number)
Precision can be between 0 and 15
GeoHexGridAggregation
:
returns
this
so that calls can be chained
Sets the maximum number of geohex buckets to return. When results are trimmed, buckets are prioritised based on the volumes of documents they contain.
(number)
Optional. The maximum number of geohex
buckets to return (defaults to 10,000).
GeoHexGridAggregation
:
returns
this
so that calls can be chained
Determines how many geohex_grid the coordinating node will request from each shard.
(number)
Optional.
GeoHexGridAggregation
:
returns
this
so that calls can be chained
A multi-bucket aggregation that works on geo_point fields and groups points into buckets that represent cells in a grid. The resulting grid can be sparse and only contains cells that have matching data. Each cell corresponds to a map tile as used by many online map sites. Each cell is labeled using a "{zoom}/{x}/{y}" format, where zoom is equal to the user-specified precision.
NOTE: This query was added in elasticsearch v7.0.
Extends BucketAggregationBase
(string)
The name which will be used to refer to this aggregation.
(string?)
The field to aggregate on
const agg = esb.geoTileGridAggregation('large-grid', 'location').precision(8);
The integer zoom of the key used to define cells/buckets in the results. Defaults to 7.
(number)
Precision can be between 0 and 29
GeoTileGridAggregation
:
returns
this
so that calls can be chained
Sets the maximum number of geotile buckets to return. When results are trimmed, buckets are prioritised based on the volumes of documents they contain.
(number)
Optional. The maximum number of geotile
buckets to return (defaults to 10,000).
GeoTileGridAggregation
:
returns
this
so that calls can be chained
Determines how many geotile_grid buckets the coordinating node
will request from each shard. To allow for more accurate counting of the
top cells returned in the final result the aggregation defaults to
returning max(10,(size x number-of-shards))
buckets from each shard.
If this heuristic is undesirable, the number considered from each shard
can be over-ridden using this parameter.
(number)
Optional.
GeoTileGridAggregation
:
returns
this
so that calls can be chained
Sets the top left coordinate for the bounding box used to filter the points in the bucket.
GeoTileGridAggregation
:
returns
this
so that calls can be chained.
Sets the bottom right coordinate for the bounding box used to filter the points in the bucket.
GeoTileGridAggregation
:
returns
this
so that calls can be chained.
Sets the top right coordinate for the bounding box used to filter the points in the bucket.
GeoTileGridAggregation
:
returns
this
so that calls can be chained.
Sets the bottom left coordinate for the bounding box used to filter the points in the bucket.
GeoTileGridAggregation
:
returns
this
so that calls can be chained.
Sets value for top of the bounding box.
(number)
GeoTileGridAggregation
:
returns
this
so that calls can be chained.
Sets value for left of the bounding box.
(number)
GeoTileGridAggregation
:
returns
this
so that calls can be chained.
Sets value for bottom of the bounding box.
(number)
GeoTileGridAggregation
:
returns
this
so that calls can be chained.
Sets value for right of the bounding box.
(number)
GeoTileGridAggregation
:
returns
this
so that calls can be chained.
Recipe for the now removed missing
query.
Can be accessed using esb.recipes.missingQuery
OR esb.cookMissingQuery
.
(string)
The field which should be missing the value.
BoolQuery
:
A boolean query with a
must_not
exists
clause is returned.
const qry = esb.cookMissingQuery('user');
qry.toJSON();
{
"bool": {
"must_not": {
"exists": {
"field": "user"
}
}
}
}
Recipe for random sort query. Takes a query and returns the same wrapped in a random scoring query.
Can be accessed using esb.recipes.randomSortQuery
OR esb.cookRandomSortQuery
.
(number?)
A seed value for the random score function.
FunctionScoreQuery
:
A
function_score
query with random sort applied
query
is not an instance of
Query
.
const reqBody = esb.requestBodySearch()
.query(esb.cookRandomSortQuery(esb.rangeQuery('age').gte(10)))
.size(100);
reqBody.toJSON();
{
"query": {
"function_score": {
"query": {
"range": { "age": { "gte": 10 } }
},
"random_score": {}
}
},
"size": 100
}
Recipe for constructing a filter query using bool
query.
Optionally, scoring can be enabled.
Can be accessed using esb.recipes.filterQuery
OR esb.cookFilterQuery
.
(Query)
The query to fetch documents for.
(boolean?
= false
)
Optional flag for enabling/disabling scoring. Disabled by default.
If enabled, a score of
1.0
will be assigned to all documents.
BoolQuery
:
A
bool
query with a
filter
clause is returned.
query
is not an instance of
Query
.
const boolQry = esb.cookFilterQuery(esb.termQuery('status', 'active'), true);
boolQry.toJSON();
{
"bool": {
"must": { "match_all": {} },
"filter": {
"term": { "status": "active" }
}
}
}