A simple query builder for Elasticsearch. Install with pip install elasticquery
.
from elasticquery import ElasticQuery, Filter, Query, Aggregate
q = ElasticQuery()
# Filters & queries
q.must(
Filter.range('field', 0, 500),
Filter.terms(my_field=['list', 'of', 'terms']),
Filter.or_filter(
Query.string(field='matching string'),
Query.raw_string('field: another matching string')
)
)
# Nested filter/query
q.should(
Query.nested('path', must=[
Query.term(field='match')
])
)
# Aggregates
q.aggregate(Aggregate.sum('aggregate_name', 'field_name'))
# Sub aggregates
q.aggregate(
Aggregate.terms('aggregate_name', 'field_name') \
.sub(Aggregate.sum('sub_aggregate_name', 'field_name'))
)
# Print out JSON ready for ES
print q.json(indent=4)
q = ElasticQuery()
Return a json string ready to send to Elasticsearch.
fields: list of fields to return
Sort the result set by a field, order optional.
Search where the Query
/Filter
object matches.
Search where the Query
/Filter
object might matches.
Search where the Query
/Filter
object does not match.
Add some Aggregate
s to our search query.
Adds a nested query.
musts, shoulds & must_nots: all lists containing Query
or Filter
objects.
Prefix multiple keys.
Search multiple key=>value terms.
Search multiple key=>[values] terms.
Search multiple key=>[values].
Filter for missing/null fields.
Adds a raw query string to match against.
This builds a query string based on kwargs
. Values can be simple (ints/strings) or lists, in which case they are OR
'd together.
Or the arg filters/queries together.
The Query
class inherits from Filter
, see above for API details.
Searches objects where field is similar to the match.
Get sum of a field.
Get the average value across a field.
Get the lowest value of a field.
Get the highest value of a field.
Get stats on a field.
Get extended stats on a field.
Count how many documents are missing a given field.
Count how many documents contain a given field.
Generate multiple buckets based on the input ranges.
Generate a histogram.
Generate a date histogram.
Count number of terms on a field.
Create a nested aggregation (for use with sub aggregates, see Aggregate.sub).
Creates a filtered aggregate (for use with sub aggregates, see Aggregate.sub).