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
我在这里找到了解决这个问题的方法。根据uboness和ComoWhat的提示,我编写了一个替代的Haystack引擎(我相信)使EdgeNGram fields像单词一样处理数字字符串。其他人可能会受益,所以我想分享一下。
如果您使用的是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”等代币。。。
相关问题 更多 >
编程相关推荐