gpt4 book ai didi

elasticsearch - 无法匹配 haystack Elasticsearch 中的数字

转载 作者:行者123 更新时间:2023-12-02 22:12:59 24 4
gpt4 key购买 nike

我有一些正在编入索引的产品类似于“99% 巧克力”。如果我搜索巧克力,它会匹配这个特定项目,但如果我搜索“99”,它就会不匹配。我遇到了这个 Using django haystack autocomplete with elasticsearch to search for digits/numbers?有同样的问题,但没有人回答他的问题。有人可以帮忙吗?

Edit2:很抱歉我忽略了一个重要的细节。数字搜索本身有效,但自动完成功能不起作用。我包括相关行:

#the relevant line in my index
name_auto = indexes.EdgeNgramField(model_attr='name')

#the relevant line in my view
prodSqs = SearchQuerySet().models(Product).autocomplete(name_auto=request.GET.get('q', ''))

编辑:以下是运行分析器的结果:

curl -XGET 'localhost:9200/haystack/_analyze?analyzer=standard&pretty' -d '99% chocolate'
{
"tokens" : [ {
"token" : "99",
"start_offset" : 0,
"end_offset" : 2,
"type" : "<NUM>",
"position" : 1
}, {
"token" : "chocolate",
"start_offset" : 4,
"end_offset" : 13,
"type" : "<ALPHANUM>",
"position" : 2
} ]
}

最佳答案

终于在这里找到了答案:ElasticSearch: EdgeNgrams and Numbers

添加以下类并更改设置文件中 Haystack_connections 下的引擎以使用下面的 CustomElasticsearchSearchEngine 而不是默认的 haystack 之一:

class CustomElasticsearchBackend(ElasticsearchSearchBackend):
"""
The default ElasticsearchSearchBackend settings don't tokenize strings of digits the same way as words, so they
get lost: the lowercase tokenizer is the culprit. Switching to the standard tokenizer and doing the case-
insensitivity in the filter seems to do the job.
"""
def __init__(self, connection_alias, **connection_options):
# see https://stackoverflow.com/questions/13636419/elasticsearch-edgengrams-and-numbers
self.DEFAULT_SETTINGS['settings']['analysis']['analyzer']['edgengram_analyzer']['tokenizer'] = 'standard'
self.DEFAULT_SETTINGS['settings']['analysis']['analyzer']['edgengram_analyzer']['filter'].append('lowercase')
super(CustomElasticsearchBackend, self).__init__(connection_alias, **connection_options)

class CustomElasticsearchSearchEngine(ElasticsearchSearchEngine):
backend = CustomElasticsearchBackend

关于elasticsearch - 无法匹配 haystack Elasticsearch 中的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27286918/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com