Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add operator __in to es.filter() for search in list. #53

Open
wants to merge 2 commits into
base: feature/fix-filters
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions django_elasticsearch/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,12 @@ def make_search_body(self):
if is_nested and isinstance(value, Model):
value = value.id

if operator == 'contains':
if operator == 'in':
nested_update(filters,
{'query': {'filtered': {"filter": {'terms': { field_name: value }}}}})
if len(filters['query']['filtered']["filter"]['terms'].items()) > 1:
raise NotImplementedError("multi_terms is not implemented.")
elif operator == 'contains':
nested_update(filters,
{'query': {'match': {field_name: {'query': value}}}})
if len(filters['query']['match'].items()) > 1:
Expand Down Expand Up @@ -321,7 +326,7 @@ def filter(self, **kwargs):
return clone

def sanitize_lookup(self, lookup):
valid_operators = ['exact', 'not', 'should', 'range', 'gt', 'lt', 'gte', 'lte', 'contains', 'isnull']
valid_operators = ['exact', 'not', 'should', 'range', 'gt', 'lt', 'gte', 'lte', 'contains', 'in', 'isnull']
words = lookup.split('__')
fields = [word for word in words if word not in valid_operators]
# this is also django's default lookup type
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ Note that es.search automatically add the default facets set on the model to the
* **es.queryset.order_by**(**kwargs)

* **es.queryset.filter**(**kwargs)
Accepted lookups are: __exact, __should, __contains, __gt, __gte, __lt, __lte, __range
Accepted lookups are: __exact, __should, __contains, __in, __gt, __gte, __lt, __lte, __range
Just like in django, the default lookup is __exact.
See the [bool query](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html) for a difference between __exact (which maps to 'must') and __should.

Expand Down