问题描述
看起来弹性搜索支持模糊查询(),但是我无法找出一个方法来让django-haystack通过该选项。
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 pass in that option.
我挖到django-haystack搜索,看起来好像在使用弹性搜索后端时使用'match_all'查询。是否可以获得模糊匹配行为,而无需修改django-haystack源代码?
I dug into the django-haystack search and it looks as if it's using the 'match_all' query when using the elasticsearch backend. Is it possible to get the fuzzy match behavior without having to modify the django-haystack source code?
干草堆资料来源:( build_search_kwargs方法是我怀疑我需要改变的)
Haystack Source: https://github.com/toastdriven/django-haystack/blob/master/haystack/backends/elasticsearch_backend.py (the build_search_kwargs method is what I suspect I need to change)
推荐答案
不需要fork Haystack,你可以更新你的自己的后端(有关更多详细信息,请参阅)。 build_search_kwargs
方法返回一个字典,以便您可以修改原始的返回值。
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
这篇关于如何使用django-haystack和弹性搜索后端进行模糊搜索?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!