Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 弹性搜索:边图和数字_Python_<img Src="//i.stack.imgur.com/RUiNP.png" Height="16" Width="18" Alt="" Class="sponsor Tag Img">elasticsearch_Django Haystack - Fatal编程技术网 elasticsearch,django-haystack,Python,elasticsearch,Django Haystack" /> elasticsearch,django-haystack,Python,elasticsearch,Django Haystack" />

Python 弹性搜索:边图和数字

Python 弹性搜索:边图和数字,python,elasticsearch,django-haystack,Python,elasticsearch,Django Haystack,关于Edengram如何对待数字有什么想法吗 我正在用ElasticSearch后端运行haystack。我创建了一个EdgeNgram类型的索引字段。此字段将包含一个字符串,该字符串可能包含单词和数字 当我使用一个部分词对这个字段进行搜索时,它的工作原理是这样的。但是如果我输入一个部分数,我就不会得到我想要的结果 例如: 我通过键入“edgen”搜索索引字段“EdgeNgram 12323”,然后将索引返回给我。如果我通过键入“123”来搜索相同的索引,我将一无所获 想法?如果您正在使用edg

关于Edengram如何对待数字有什么想法吗

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

当我使用一个部分词对这个字段进行搜索时,它的工作原理是这样的。但是如果我输入一个部分数,我就不会得到我想要的结果

例如:

我通过键入“edgen”搜索索引字段“EdgeNgram 12323”,然后将索引返回给我。如果我通过键入“123”来搜索相同的索引,我将一无所获


想法?

如果您正在使用edgeNGram标记器,那么它会将“edgeNGram 12323”视为单个标记,然后对其应用edgeNGram标记过程。例如,如果minu_grams=1 max_grams=4,您将得到以下标记的索引:[“E”、“Ed”、“Edg”、“Edge”]。所以我想这不是你真正想要的——考虑使用EdGigGand令牌过滤器代替:

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


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

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

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

我使用的是haystack中elasticsearch后端附带的EdgeNGram分析器。它使用小写标记器和EdgeNgram过滤器。如果字段是“EdgeNgram 12323 myfield”,而我的搜索是myfi,我将得到正确的结果。它似乎对数字的处理方式不一样。没关系,我明白了。小写标记器使用带有小写筛选器的字母标记器。此字母标记器不会将分组在一起的数字标记为一个数字,而是作为单独的数字(例如:1453将标记为1、4、5、3,而不是1453)。我将其更改为使用标准标记器,并将小写筛选器添加到我的筛选器列表中,结果成功了!