A python library to simplify building complex Elasticsearch JSON queries. Based on the Django QuerySet API, backed by the official python elasticsearch library. Supports Elasticsearch 1.0+
.
Currently pre v1.0
, so the API is not locked in. This project aims to follow semantic versioning once it reaches a stable API. The only issues may arise as the backend elasticsearch-py
library locks its versions to Elasticsearch releases.
pip install pyeqs
from pyeqs import QuerySet
qs = QuerySet("127.0.0.1", index="my_index")
print qs._query
"""
{
'query': {
'match_all': {}
}
}
"""
from pyeqs import QuerySet
qs = QuerySet("127.0.0.1", query="cheese", index="my_index")
print qs._query
"""
{
'query': {
'query_string': {
'query': 'cheese'
}
}
}
"""
from pyeqs import QuerySet
from pyeqs.dsl import Term, Type
qs = QuerySet("127.0.0.1", index="my_index")
qs.filter(Term("foo", "bar"), operator="or").filter(Type("baz"))
print qs._query
"""
{
'query': {
'filtered': {
'filter': {
'or': [
{
'term': {
'foo': 'bar'
}
},
{
'type': {
'value': 'baz'
}
}
]
},
'query': {
'match_all': {}
}
}
}
}
"""
from pyeqs import QuerySet, Bool
from pyeqs.dsl import Sort
qs = QuerySet("127.0.0.1", index="my_index")
b = Bool()
b.must(Term("foo", "bar"))
qs.filter(b)
from pyeqs import QuerySet
from pyeqs.dsl import Sort
qs = QuerySet("127.0.0.1", index="my_index")
qs.order_by(Sort("_id", order="desc"))
from pyeqs import QuerySet
from pyeqs.dsl import ScriptScore
qs = QuerySet("127.0.0.1", index="my_index")
qs.score(ScriptScore("score = foo + bar;", lang="mvel", params={"bar": 1}))
from pyeqs import QuerySet
from pyeqs.dsl import Term
qs = QuerySet("127.0.0.1", index="my_index")
qs.filter(Term("foo", 1)
qs.wrapper(lambda x: x['_id'])
from pyeqs import QuerySet
qs = QuerySet("127.0.0.1", index="my_index")
qs.only('_id')
from pyeqs import QuerySet
from pyeqs.dsl import Terms, Term
qs = QuerySet("127.0.0.1", index="my_index")
qs.filter(Terms("foo", ["bar", "baz"]))
# Duplicate the Queryset and do more filters
only_bar = qs.objects.filter(Term("foo", "bar"))
only_baz = qs.objects.filter(Term("foo", "baz"))
from pyeqs import QuerySet
from pyeqs.dsl import Term
qs = QuerySet("127.0.0.1", index="my_index")
qs.filter(Term("foo", "bar"))
results = qs[0:10] # Uses from/size in the background
from pyeqs import QuerySet
from pyeqs.dsl import Term
qs = QuerySet("127.0.0.1", index="my_index")
qs.filter(Term("foo", "bar"))
for result in qs:
print result['_source']
# Builds a cache of 10 results at a time and iterates
from pyeqs import QuerySet
from pyeqs.dsl import Term
qs = QuerySet("127.0.0.1", index="my_index")
qs.filter(Term("foo", "bar"))
qs.count() # None, since we haven't queried
qs[0:10]
qs.count() # Returns number of hits