【问题标题】:can't match digits in haystack elastic search无法匹配 haystack 弹性搜索中的数字
【发布时间】:2015-02-01 21:54:50
【问题描述】:

我有一些产品要编入索引,例如“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 django-haystack


【解决方案1】:

终于在这里找到了答案: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

【讨论】:

    【解决方案2】:

    通过标准分析器运行字符串 99% chocolate 会得到正确的结果(99 是一个术语),所以如果你目前没有使用它,你应该切换到它。

    curl -XGET 'localhost:9200/myindex/_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
      } ]
    }
    

    【讨论】:

    • 抱歉,我已更新问题以反映正常搜索工作正常这一事实。但是,自动完成与数字不匹配。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-17
    • 2013-06-12
    • 2015-03-12
    • 2017-01-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多