Skip to content

Instantly share code, notes, and snippets.

@MatthewWilkes
Created October 12, 2015 15:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MatthewWilkes/2de8aaf7b4deca31f952 to your computer and use it in GitHub Desktop.
Save MatthewWilkes/2de8aaf7b4deca31f952 to your computer and use it in GitHub Desktop.
Native ElasticSearch suggestion fields using Haystack
from haystack import indexes
from haystack.fields import SearchField
from haystack.backends import elasticsearch_backend
class CompletionField(SearchField):
field_type = 'completion'
# As suggested at https://www.elastic.co/guide/en/elasticsearch/reference/current/search-suggesters-completion.html
elasticsearch_backend.FIELD_MAPPINGS['completion'] = {
"type" : "completion",
"index_analyzer" : "simple",
"search_analyzer" : "simple",
"payloads" : True
}
class MyIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.EdgeNgramField(document=True, use_template=True)
suggest = CompletionField()
def prepare_suggest(self, obj):
return self.prepared_data['text'].split()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment