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. 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.
bodybuilder
:
Builder.
Set a sort direction on a given field.
(String)
Field name.
([String]
(default 'asc'
)
)
A valid direction: 'asc' or 'desc'.
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.
Add a query clause to the query body.
(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.
(...any)
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.
Add a filter clause to the query body.
(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.
(...any)
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.
Add an aggregation clause to the query body.
(string)
Name of the field to aggregate over.
([Object])
(optional) Additional options to
include in the aggregation.
([Function])
(optional) A function used to define
sub-aggregations as children. This
must
be the last argument.
(...any)
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()
Alias for aggregation
.
(...any)
bodybuilder
:
Builder.