Skip to content

Commit

Permalink
simplified Payload import
Browse files Browse the repository at this point in the history
  • Loading branch information
rochacbruno committed Jan 3, 2016
1 parent a84471a commit 69c767e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -522,11 +522,11 @@ Person.search(query=query, size=10)
## Same example using payload utils

```python
from esengine.utils.payload import Payload, Query, Filter
from esengine import Payload, Query, Filter
payload = Payload(
query=Query.filtered(query=Query.match_all(), filter=Filter.ids([1, 2]))
)
Person.search(query=payload.dict, size=10)
Person.search(payload, size=10)
```

> Payload utils exposes Payload, Query, Filter, Aggregate, Suggesters
Expand All @@ -536,7 +536,9 @@ Person.search(query=payload.dict, size=10)
Payload object is chainable so you can do:
```python
payload = Payload(query=query).size(10).sort("field", order="desc")
Document.search(payload)
Document.search(payload)
# or the equivalent
payload.search(Document)
```

# Contribute
Expand Down
1 change: 1 addition & 0 deletions esengine/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
from esengine.document import Document # noqa
from esengine.mapping import Mapping # noqa
from esengine.fields import * # noqa
from esengine.utils.payload import Payload, Query, Filter, Aggregate, Suggester # noqa
10 changes: 7 additions & 3 deletions esengine/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from esengine.bases.result import ResultSet
from esengine.mapping import Mapping
from esengine.utils import validate_client
from esengine.utils.payload import Payload


class Document(BaseDocument):
Expand Down Expand Up @@ -305,16 +306,19 @@ def search(cls, query, es=None, perform_count=False, **kwargs):
...}
>>> results = Document.search(query, size=10)
:param query: raw query or Payload instance
:param query: raw query or Query or Payload instance
:param es: ES client or None (if implemented a default in Model)
:param perform_count: If True, dont return objects, only count
:param kwargs: extra key=value to be passed to es client
:return: Iterator of Doc objets
"""

if not isinstance(query, dict):
# if not a dict, must be a Payload instance
query = query.dict
# if not a raw dict query
if isinstance(query, Payload): # must be a Payload instance
query = query.dict
else: # must be a Query to wrap
query = Payload(query=query).dict

es = cls.get_es(es)
search_args = dict(
Expand Down

0 comments on commit 69c767e

Please sign in to comment.