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

后端 未结 1 468
终归单人心
终归单人心 2021-02-15 03:57

It looks as if elasticsearch supports fuzzy queries (http://www.elasticsearch.org/guide/reference/query-dsl/fuzzy-query/) but I can\'t figure out a way to have django-haystack p

1条回答
  •  南方客
    南方客 (楼主)
    2021-02-15 04:55

    No need to fork Haystack, you can update that method in your own backend (for more details, see Stretching Haystack's ElasticSearch Backend). The build_search_kwargs method returns a dictionary so you can just modify the original return value.

    Disclaimer: this code is just an example of how you could update your own backend, not how to implement 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
    

    0 讨论(0)
提交回复
热议问题