Created
October 12, 2015 15:26
-
-
Save MatthewWilkes/2de8aaf7b4deca31f952 to your computer and use it in GitHub Desktop.
Native ElasticSearch suggestion fields using Haystack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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