How can I do a fuzzy search using django-haystack and elasticsearch backend?

It seems like elasticsearch supports fuzzy queries ( http://www.elasticsearch.org/guide/reference/query-dsl/fuzzy-query/ ), but I can't figure out how to get through django-haystack in this option.

I burst into a search for django-haystack and looks as if it used a match_all request when using the elasticsearch backend. Is it possible to get fuzzy-match behavior without changing the django-haystack source code?

Haystack Source: https://github.com/toastdriven/django-haystack/blob/master/haystack/backends/elasticsearch_backend.py (the build_search_kwargs method is what I suspect needs to be changed)

+8
django elasticsearch django-haystack
source share
1 answer

There is no need for a Haystack fork, you can update this method in your own backend (see the Haystack ElasticSearch Backend Stretch for details ). The build_search_kwargs method returns a dictionary, so you can just change the original return value.

Disclaimer: This code is just an example of how you could update your own backend, not how to implement a fuzzy search.

 class FuzzyBackend(ElasticsearchSearchBackend): def build_search_kwargs(self, query_string, **kwargs): fuzzy = kwargs.pop('fuzzy', False) fuzzy_field = kwargs.pop('min_similarity', '') search_kwargs = super(FuzzyBackend, self).build_search_kwargs( query_string, kwargs) if fuzzy: search_kwargs = {'fuzzy': {fuzzy_field: query_string}} return search_kwargs 
+6
source share

All Articles