ElasticSearch 无法从同一文档中获取多个建议值

ElasticSearch can't get multiple suggestor values from the same document

你能帮帮我吗?

我对 ElasticSearch 中的 Completion Suggester 有疑问

示例:我有这个映射:

PUT music
{
  "mappings": {
    "properties": {
      "suggest": {
        "type": "completion"
      },
      "title": {
        "type": "keyword"
      }
    }
  }
}

并按如下方式为文档索引多个建议:

PUT music/_doc/1?refresh
{
  "suggest": [
    {
      "input": "Nirva test",
      "weight": 10
    },
    {
      "input": "Nirva hola",
      "weight": 3
    }
  ]
}

查询:你可以在kibana上做这个请求

POST music/_search?pretty
{
  "suggest": {
    "song-suggest": {
      "prefix": "nirv",        
      "completion": {         
          "field": "suggest"  
      }
    }
  }
}

结果我只检索第一个值而不是两个。

我也在 kibana 开发工具上做了测试,结果是这样

    {
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 0,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "suggest" : {
    "song-suggest" : [
      {
        "text" : "nir",
        "offset" : 0,
        "length" : 3,
        "options" : [
          {
            "text" : "Nirvana test",
            "_index" : "music",
            "_type" : "_doc",
            "_id" : "1",
            "_score" : 10.0,
            "_source" : {
              "suggest" : [
                {
                  "input" : "Nirvana test",
                  "weight" : 10
                },
                {
                  "input" : "Nirvana best",
                  "weight" : 3
                }
              ]
            }
          }
        ]
      }
    ]
  }
}

预期结果:

"suggest" : {
    "song-suggest" : [
          {
        "text" : "nirvana",
        "offset" : 0,
        "length" : 7,
        "options" : [
          {
            "text" : "Nirvana test",
            "_index" : "music",
            "_type" : "_doc",
            "_id" : "1",
            "_score" : 10.0,
            "_source" : {
              "suggest" : [
                {
                  "input" : "Nirvana test",
                  "weight" : 10
                },
                {
                  "input" : "Nirvana best",
                  "weight" : 3
                }
              ]
            }
          }
        ]
      },
      {
        "text" : "nirvana b",
        "offset" : 0,
        "length" : 9,
        "options" : [
          {
            "text" : "Nirvana best",
            "_index" : "music",
            "_type" : "_doc",
            "_id" : "1",
            "_score" : 3.0,
            "_source" : {
              "suggest" : [
                {
                  "input" : "Nirvana test",
                  "weight" : 10
                },
                {
                  "input" : "Nirvana best",
                  "weight" : 3
                }
              ]
            }
          }
        ]
      }
    ]
  }

这是当前实现的默认行为。您可以查看 #31738。以下是解释为什么它只返回一个 document/suggestion.

的评论之一

The completion suggester is document-based by design so we cannot return one entry per matching suggestion. It is documented that it returns documents not suggestions and a single input can be indexed in multiple suggestions (if you have synonyms in your analyzer for instance) so it is not trivial to differentiate a match from its variations. Also the completion suggester does not visit all suggestions to select the top N, it has a special structure (a weighted FST) that can visit suggestions in the order of their scores and early terminates the query once enough documents have been found.