首页 > 解决方案 > Elasticsearch 前缀建议?

问题描述

如何通过前缀获得术语建议,我有名称字段。

我有三个记录:

坐在中间

完成最少的工作

运河 mib 不见了

当我输入min时,我希望能够将minimum一词作为建议的术语,但我确实得到了其他文档中的midmib 。

这是我的查询:

{
  "suggest": {
    "text": "min",
    "simple_phrase": {
      "term": {
        "analyzer": "standard",
        "field": "Name.raw",
        "min_word_length": 2,
        "prefix_length": 1,
        "suggest_mode": "always"
      }
    }
  }
}

映射

{
  "Name": {
    "type": "text",
    "index": "not_analyzed",
    "include_in_all": true,
    "fields": {
      "raw": {
        "type": "text",
        "index": "not_analyzed",
        "include_in_all": false
      },
      "trigram": {
        "type": "text",
        "analyzer": "trigram_analyzer"
      }
    }
  }
}

结果

{
  "took": 6,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 0,
    "max_score": 0,
    "hits": []
  },
  "suggest": {
    "simple_phrase": [
      {
        "text": "min",
        "offset": 0,
        "length": 3,
        "options": [
          {
            "text": "mib",
            "score": 0.6666666,
            "freq": 79
          },
          {
            "text": "mid",
            "score": 0.6666666,
            "freq": 59
          }
        ]
      }
    ]
  }
}

标签: elasticsearchsearch-suggestion

解决方案


正如它在文档中所说:

术语建议者根据编辑距离建议术语。在建议术语之前分析提供的建议文本。根据分析的建议文本标记提供建议的术语。术语建议者不考虑作为请求一部分的查询。

因此,术语建议者只是“编辑”您的文本。所以你可以尝试这样的事情,看看minimum建议的结果:

{
  "suggest": {
    "text" : "minimam",
    "simple_phrase" : {
      "term" : {
        "analyzer" : "standard",
        "field" :  "Name.raw",
        "min_word_length" : 1,
        "suggest_mode" : "always"
      }
    }
  }
}

我认为,为了您的目的,最好使用完成建议器。


推荐阅读