Skip to content

Instantly share code, notes, and snippets.

@simonw
Created November 13, 2016 20:19
Show Gist options
  • Save simonw/f0fe4cd921964821f8b15afe351195bc to your computer and use it in GitHub Desktop.
Save simonw/f0fe4cd921964821f8b15afe351195bc to your computer and use it in GitHub Desktop.
Over-riding elasticsearch-dsl FacetedSearch to use .filter, not .post_filter for selected facets - see https://twitter.com/simonw/status/797894804700966912
class EmailSearch(FacetedSearch):
doc_types = [Email]
# fields that should be searched
fields = ['subject', 'body']
facets = {
'inbox': TermsFacet(field='inbox'),
}
def filter(self, search):
"""
Over-ride default behaviour (which uses post_filter) to use filter instead.
"""
filters = Q('match_all')
for f in itervalues(self._filters):
filters &= f
return search.filter(filters)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment