gpt4 book ai didi

Elasticsearch,按精确字符串匹配排序

转载 作者:行者123 更新时间:2023-12-02 23:00:40 24 4
gpt4 key购买 nike

我想对结果进行排序,如果一个特定字段(比如“first_name”)等于一个精确值(比如“Bob”),那么首先返回这些文档。

这将导致所有 first_name 恰好为 'Bob' 的文档将首先返回,然后是所有其他文档。请注意,我不打算排除 first_name 不是“Bob”的文档,只是对它们进行排序,以便在所有 Bob 之后返回它们。

我了解 Elasticsearch 中数字或字母排序的工作原理,但我找不到涵盖此类排序的文档的任何部分。

这是否可能,如果可能,如何实现?

最佳答案

一种解决方案是操纵名字字段中包含 Bob 的结果的分数。

例如:

POST /test/users
{
"name": "Bob"
}

POST /test/users
{
"name": "Alice"
}

GET /test/users/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"name": {
"query": "Bob",
"boost" : 2
}
}
},
{
"match_all": {}
}
]
}
}
}

将按顺序返回 Bob 和 Alice(分别大约为 1 和 0.2)。

来自 the book :

Query-time boosting is the main tool that you can use to tune relevance. Any type of query accepts a boost parameter. Setting a boost of 2 doesn’t simply double the final _score; the actual boost value that is applied goes through normalization and some internal optimization. However, it does imply that a clause with a boost of 2 is twice as important as a clause with a boost of 1.

这意味着如果您还希望“Fred”排在 Bob 之前,您可以在上面的示例中将其增加 3 个因子。

关于Elasticsearch,按精确字符串匹配排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34943940/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com