【问题标题】:Elasticsearch, HOW to make phrase suggester return the exact suggestion?Elasticsearch,如何让短语建议器返回确切的建议?
【发布时间】:2018-04-10 09:39:27
【问题描述】:

我正在使用弹性搜索 5.5.2

我正在尝试短语建议器,但无法将其配置为返回索引中已经存在的确切建议。下面给出了我的索引设置、类型映射和短语建议查询。请帮忙。

我的索引设置和类型映射是

PUT test
{
  "settings": {
    "index": {
      "analysis": {
        "analyzer": {
          "trigram_analyzer": {
            "type": "custom",
            "tokenizer": "standard",
            "filter": ["shingle"]
          }
        },
        "filter": {
          "shingle": {
            "type": "shingle",
            "min_shingle_size": 2,
            "max_shingle_size": 3
          }
        }
      }
    }
  },
  "mappings": {
    "test": {
      "properties": {
        "title": {
          "type": "text",
          "fields": {
            "trigram": {
              "type": "text",
              "analyzer": "trigram_analyzer"
            }
          }
        }
      }
    }
  }
}

索引文档使用

POST test/test?refresh=true
{"title": "noble prize"}

我正在使用的短语建议器

POST test/_search
{
  "suggest": {
    "text": "nobe priz",
    "simple_phrase": {
      "phrase": {
        "field": "title.trigram",
        "size": 1,
        "gram_size": 3,
        "direct_generator": [ {
          "field": "title.trigram",
          "suggest_mode": "always"
        } ],
        "highlight": {
          "pre_tag": "<em>",
          "post_tag": "</em>"
        }
      }
    }
  }
}

我得到的结果是

"suggest": {
    "simple_phrase": [
      {
        "text": "nobe priz",
        "offset": 0,
        "length": 9,
        "options": [
          {
            "text": "noble priz",
            "highlighted": "<em>noble</em> priz",
            "score": 0.09049256
          }
        ]
      }
    ]
  }

我的问题是,对于搜索文本 - “nobe priz” - 为什么我没有获得“noble Prize”作为建议。相反,为什么我只是获得“贵族奖”?

如果我们看到,“贵族奖品”就是我保存的文件。

如果我将大小的值增加到“2”,那么我也不会获得“贵族奖”作为建议之一。

大小为 2,对于搜索文本“nobe priz”,我得到以下响应

"suggest": {
    "simple_phrase": [
      {
        "text": "nobe priz",
        "offset": 0,
        "length": 9,
        "options": [
          {
            "text": "noble priz",
            "highlighted": "<em>nobel</em> priz",
            "score": 0.09049256
          },
          {
            "text": "nobe prize",
            "highlighted": "nobe <em>prize</em>",
            "score": 0.09049256
          }
        ]
      }
    ]
  }

我应该怎么做才能获得“贵族奖”作为建议? 请帮忙。

【问题讨论】:

    标签: elasticsearch elasticsearch-5


    【解决方案1】:

    我自己找到了答案。需要使用参数 'max_errors' 告诉 ES 搜索文本中有多少项拼写错误。 'max_errors' 可以以浮点数或绝对数的形式作为百分比值给出。

    “点击下方查看带有 max_errors 参数的短语建议器的 ES 文档” https://www.elastic.co/guide/en/elasticsearch/reference/master/search-suggesters-phrase.html

    因此我将'max_errors'参数值添加为2,如下所示

    POST test/_search
    {
      "suggest": {
        "text": "nobe priz",
        "simple_phrase": {
          "phrase": {
            "field": "title.trigram",
            "size": 1,
            "gram_size": 3,
            "max_errors": 2,
            "direct_generator": [ {
              "field": "title.trigram",
              "suggest_mode": "always"
            } ],
            "highlight": {
              "pre_tag": "<em>",
              "post_tag": "</em>"
            }
          }
        }
      }
    }
    

    我得到了完全匹配的短语建议,如下所示

    "suggest": {
        "simple_phrase": [
          {
            "text": "nobe priz",
            "offset": 0,
            "length": 9,
            "options": [
              {
                "text": "noble prize",
                "highlighted": "<em>noble prize</em>",
                "score": 0.4833575
              }
            ]
          }
        ]
      }
    

    因此,max_errors 为 2 时,将返回建议“贵族奖”。

    干杯:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-21
      • 2017-10-23
      • 2013-09-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多