https://github.com/danpaz/bodybuilder
Bodybuilder is a small library that makes elasticsearch queries easier to write, read, and maintain 💪. The whole public api is documented here, but how about a simple example to get started:
bodybuilder()
.query('match', 'message', 'this is a test')
.build()
// results in:
{
query: {
match: {
message: 'this is a test'
}
}
}
You can chain multiple methods together to build up a more complex query.
bodybuilder()
.query('match', 'message', 'this is a test')
.filter('term', 'user', 'kimchy')
.notFilter('term', 'user', 'cassie')
.aggregation('terms', 'user')
.build()
For nested sub-queries or sub-aggregations, pass a function as the last
argument and build the nested clause in the body of that function. Note that
you must return
the builder object in the nested function. For example:
bodybuilder()
.query('nested', 'path', 'obj1', (q) => {
return q.query('match', 'obj1.color', 'blue')
})
.build()
The entire elasticsearch query DSL is available using the bodybuilder api. There are many more examples in the docs as well as in the tests.
(Object)
Body to initialise with
(Object)
Queries to initialise with
(Object)
Filters to initialise with
(Object)
Aggregations to initialise with
(Object)
Suggestions to initialise with
bodybuilder
:
Builder.
Set a sort direction on a given field.
bodybuilder()
.sort('timestamp', 'desc')
.build()
You can sort multiple fields at once
bodybuilder()
.sort([
{"categories": "desc"},
{"content": "asc"}
])
.build()
Geo Distance sorting is also supported & it's the only sort type that allows for duplicates
bodyBuilder().sort([
{
_geo_distance: {
'a.pin.location': [-70, 40],
order: 'asc',
unit: 'km',
mode: 'min',
distance_type: 'sloppy_arc'
}
},
{
_geo_distance: {
'b.pin.location': [-140, 80],
order: 'asc',
unit: 'km',
mode: 'min',
distance_type: 'sloppy_arc'
}
}
])
.sort([
{ timestamp: 'desc' },
{ content: 'desc' },
{ content: 'asc' },
{"price" : {"order" : "asc", "mode" : "avg"}}
])
.build()
bodybuilder
:
Builder.
Set a from offset value, for paginating a query.
(Number)
The offset from the first result you want to
fetch.
bodybuilder
:
Builder.
Set a size value for maximum results to return.
(Number)
Maximum number of results to return.
bodybuilder
:
Builder.
Set any key-value on the elasticsearch body.
(String)
Key.
(any)
Value.
bodybuilder
:
Builder.
Collect all queries, filters, and aggregations and build the entire elasticsearch query.
Object
:
Elasticsearch query body.
Returns a cloned instance of bodybuilder
const bodyA = bodybuilder().size(3);
const bodyB = bodyA.clone().from(2); // Doesn't affect bodyA
// bodyA: { size: 3 }
// bodyB: { size: 3, from: 2 }
bodybuilder
:
Newly cloned bodybuilder instance
Add a query clause to the query body.
(...any)
(string)
Query type.
(Object)
(optional) Additional options for the
query clause.
(Function?)
(optional) A function used to define
sub-filters as children. This
must
be
the last argument.
bodybuilder
:
Builder.
bodybuilder()
.query('match_all')
.build()
bodybuilder()
.query('match_all', { boost: 1.2 })
.build()
bodybuilder()
.query('match', 'message', 'this is a test')
.build()
bodybuilder()
.query('terms', 'user', ['kimchy', 'elastic'])
.build()
bodybuilder()
.query('nested', { path: 'obj1', score_mode: 'avg' }, (q) => {
return q
.query('match', 'obj1.name', 'blue')
.query('range', 'obj1.count', {gt: 5})
})
.build()
Alias for query
.
(...any)
bodybuilder
:
Builder.
Alias for query
.
(...any)
bodybuilder
:
Builder.
Add a "should" query to the query body.
Same arguments as query
.
(...any)
bodybuilder
:
Builder.
Add a "must_not" query to the query body.
Same arguments as query
.
(...any)
bodybuilder
:
Builder.
Set the minimum_should_match
property on a bool query with more than
one should
clause.
(any)
minimum_should_match parameter. For possible values
see
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-minimum-should-match.html
(boolean)
allows minimum_should_match to be overridden, ignoring internal constraints
bodybuilder
:
Builder.
Add a filter clause to the query body.
(...any)
(string)
Filter type.
(Object)
(optional) Additional options for the
filter clause.
(Function?)
(optional) A function used to define
sub-filters as children. This
must
be
the last argument.
bodybuilder
:
Builder.
bodybuilder()
.filter('term', 'user', 'kimchy')
.build()
Alias for filter
.
(...any)
bodybuilder
:
Builder.
Alias for filter
.
(...any)
bodybuilder
:
Builder.
Add a "should" filter to the query body.
Same arguments as filter
.
(...any)
bodybuilder
:
Builder.
Add a "must_not" filter to the query body.
Same arguments as filter
.
(...any)
bodybuilder
:
Builder.
Set the minimum_should_match
property on a bool filter with more than
one should
clause.
(any)
minimum_should_match parameter. For possible values
see
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-minimum-should-match.html
(boolean)
allows minimum_should_match to be overridden, ignoring internal constraints
bodybuilder
:
Builder.
Add an aggregation clause to the query body.
(...any)
(string)
Name of the field to aggregate over.
(Object?)
(optional) Additional options to
include in the aggregation.
[options._meta] associate a piece of metadata with individual aggregations
[options._name ] another way to pass a custom name to the aggregation.
(Function?)
(optional) A function used to define
sub-aggregations as children. This
must
be the last argument.
bodybuilder
:
Builder.
bodybuilder()
.aggregation('max', 'price')
.build()
bodybuilder()
.aggregation('percentiles', 'load_time', {
percents: [95, 99, 99.9]
})
.build()
bodybuilder()
.aggregation('date_range', 'date', {
format: 'MM-yyy',
ranges: [{ to: 'now-10M/M' }, { from: 'now-10M/M' }]
})
.build()
bodybuilder()
.aggregation('diversified_sampler', 'user.id', { shard_size: 200 }, (a) => {
return a.aggregation('significant_terms', 'text', 'keywords')
})
.build()
bodybuilder()
.aggregation('terms', 'title', {
_meta: { color: 'blue' }
}, 'titles')
.build()
Alias for aggregation
.
(...any)
bodybuilder
:
Builder.
Add a suggestion clause to the query body.
(...any)
(string)
Name of the field to suggest on.
(Object?)
(optional) Additional options to
include in the suggestion clause.
[options.text ] text query to run on suggest
[options.name ] pass a custom name to the function
[options.analyzer ] name of predefined analyzer to use on suggest
bodybuilder
:
Builder.
bodybuilder()
.suggest('term', price', { text: 'test' })
.build()
bodybuilder()
.suggest('phrase', 'price', { text: 'test', name: 'custom name' })
.build()