ElasticSearch:Edgegrams和Numbers

2024-04-28 08:38:21 发布

您现在位置:Python中文网/ 问答频道 /正文

关于埃德根格拉姆如何对待数字有什么想法吗?在

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

当我用一个部分词对这个字段进行搜索时,它的工作方式是应该的。但是如果我输入一个偏数,我就得不到我想要的结果。在

示例:

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

有什么想法?在


Tags: 字符串示例类型键入方式数字elasticsearch单词
2条回答

我在这里找到了解决这个问题的方法。根据uboness和ComoWhat的提示,我编写了一个替代的Haystack引擎(我相信)使EdgeNGram fields像单词一样处理数字字符串。其他人可能会受益,所以我想分享一下。

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

如果您使用的是edgeNGram标记器,那么它将把“edgeNGram 12323”视为单个令牌,然后对其应用edgeNGram'ing过程。例如,如果min_grams=1 max_grams=4,您将得到以下标记的索引:[“E”,“Ed”,“Edg”,“Edg”]。所以我想这不是您真正想要的-考虑使用edgeNGram令牌筛选器:

如果您使用的是edgeNGram令牌筛选器,请确保您使用的是一个标记化文本“edgeNGram 12323”的标记器,以便从中生成两个标记:[“edgeNGram”,“12323”](标准或空白标记器将完成此操作)。然后在其旁边应用edgeNGram过滤器。

一般来说,edgeNGram将使用“12323”并生成诸如“1”、“12”、“123”等代币。。。

相关问题 更多 >