gpt4 book ai didi

python - ElasticSearch:EdgeNgrams 和数字

转载 作者:太空狗 更新时间:2023-10-29 23:55:16 25 4
gpt4 key购买 nike

关于 EdgeNgram 如何处理数字有什么想法吗?

我正在使用 ElasticSearch 后端运行 haystack。我创建了一个 EdgeNgram 类型的索引字段。该字段将包含一个字符串,该字符串可能包含单词和数字。

当我使用部分词对该字段运行搜索时,它按预期运行。但是,如果我输入部分数字,我得不到我想要的结果。

例子:

我通过键入“edgen”搜索索引字段“EdgeNgram 12323”,然后我会得到返回给我的索引。如果我通过键入“123”来搜索相同的索引,我什么也得不到。

想法?

最佳答案

我在这里找到了解决 Haystack + Elasticsearch 中同样问题的方法。根据 uboness 和 ComoWhat 的提示,我编写了一个备用的 Haystack 引擎(我相信)它使 EdgeNGram 字段像对待单词一样对待数字字符串。其他人可能会受益,所以我想我会分享它。

from haystack.backends.elasticsearch_backend import ElasticsearchSearchEngine, ElasticsearchSearchBackend

class CustomElasticsearchBackend(ElasticsearchSearchBackend):
"""
The default ElasticsearchSearchBackend settings don't tokenize strings of digits the same way as words, so emplids
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 http://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

关于python - ElasticSearch:EdgeNgrams 和数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13636419/

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