【问题标题】:NgramField returning resutls based on substring of the query termgramField 根据查询词的子字符串返回结果
【发布时间】:2016-04-11 17:25:34
【问题描述】:

我有一个仓库模型,它正在获取如下索引

class WarehouseIndex(SearchIndex, Indexable):
    """
    SearchIndex Class that stored indexes for Model Warehouse
    """
    text = CharField(document=True, use_template=True)
    search_auto = NgramField()
    ....

   def get_model(self):
        return WareHouse

在我的 shell 中,我正在运行以下 sqs 查询。

>>> sqs = SearchQuerySet().models(WareHouse)
>>> sqs.filter(customers=3).filter(search_auto='pondicherry')

这返回的结果由不具有确切术语 pondicherry 的结果组成,它还为我提供了一些匹配诸如 ichchendi 等术语的结果。

我什至尝试过使用__exactExact,但都返回相同的结果?

编辑:Index mappingIndex Setting

我怎样才能避免这种情况并仅提供术语 pondicherry 的结果?

【问题讨论】:

  • 你能展示你的映射吗?
  • @Val : 添加了映射和设置
  • 这是因为您的 search_auto ngram 字段具有相同的索引和搜索分析器,因此您的搜索词 pondicherry 在搜索时也会被 ngramed。解决此问题的唯一方法是为您的search_auto 字段设置不同的search_analyzerstandard 将是一个不错的选择。似乎与this issue有关
  • @Val 如何设置 seach_analyzer?

标签: django elasticsearch django-haystack


【解决方案1】:

好像和这个open issue有关

这是因为您的 search_auto ngram 字段具有相同的索引和搜索分析器,因此您的搜索词 pondicherry 在搜索时也会被 ngramed。解决此问题的唯一方法是为您的 search_auto 字段设置不同的 search_analyzerstandard 将是一个不错的选择。

您可以通过以下方式更改您的 search_auto 字段映射:

curl -XPUT localhost:9200/haystack/_mapping/modelresult -d '{
   "properties": {
      "search_auto": {
         "type": "string",
         "analyzer": "ngram_analyzer",
         "search_analyzer": "standard"
      }
   }
}'

【讨论】:

    【解决方案2】:

    正如@Val 在上述答案中所说,错误是因为 search_analyzer 和 indexed_analyzer 是相同的,从而导致了问题,

    众所周知haystack 在设置基本的elasticsearch 配置时非常不灵活,我安装了elasticstack,并在我的setting.py 中按照建议将后端更改为elasticsearch_backend,并另外添加了以下2 个配置

    # elasticslack setting
    ELASTICSEARCH_DEFAULT_ANALYZER = 'snowball'
    ELASTICSEARCH_DEFAULT_NGRAM_SEARCH_ANALYZER = 'standard'   
    

    这似乎解决了我的问题。

    【讨论】:

    • 很高兴你知道了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-18
    • 2020-04-06
    相关资源
    最近更新 更多