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

发布于 2025-01-13 08:18:08 字数 3032 浏览 3 评论 0原文

你能帮我吗?

我在 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
                }
              ]
            }
          }
        ]
      }
    ]
  }

Can you help me please?

I have a problem with Completion Suggester in ElasticSearch

Example: I have this mapping :

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

and index multiple suggestions for a document as follows:

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

Querying: you can do this request on kibana

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

and the result I retrieve only the first value but not both.

I did the test on kibana dev tool too and this is the result

    {
  "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
                }
              ]
            }
          }
        ]
      }
    ]
  }
}

expected result :

"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
                }
              ]
            }
          }
        ]
      }
    ]
  }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不打扰别人 2025-01-20 08:18:08

这是当前实现的默认行为。您可以查看 #31738。以下是解释为什么它只返回一份文档/建议的评论之一。

完成建议器在设计上是基于文档的,因此我们不能
每个匹配建议返回一个条目。据记载,它
返回文档而不是建议,并且可以对单个输入进行索引
多个建议(如果您的分析器中有同义词
实例),因此区分匹配和它的匹配并不简单
变化。此外,完成建议器不会访问所有
建议选择前N个,它有一个特殊的结构(a
加权 FST),可以按分数顺序访问建议
一旦找到足够的文档,就会提前终止查询。

This is the default behavior of current implementations. You can check #31738. Below is one of the comment for an explanation why it is returning only one 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文