如何对具有相同分数的 OpenSearch 结果进行排序?

huangapple go评论42阅读模式
英文:

How to sort OpenSearch results that have the same score?

问题

我想设置第二排序准则,而不停用默认行为,即按相关性分数排序。在文档中,所有示例似乎都会停用默认行为,然后按所选字段排序。

文档还提供了设置多个排序准则的示例(查询的sort属性是排序准则数组),但我没有看到如何设置第一个排序准则为按相关性分数排序的提及。

track_score选项允许我查看每个命中的相关性分数,但我希望实际将其用作第一个排序规则,仅在具有相同相关性分数的结果上使用另一个排序规则。

英文:

I want to set secondary sorting criteria, without deactivating the default behavior, which is sorting by relevance score. In the documentation, all the examples seem to deactivate the default behavior and then sort by the chosen field.

Documentation also gives examples of setting several sorting criteria (the sort attribute of the query is an array of sorting criteria), but I don't see a mention of how to set the first one as sorting by relevance score.

The track_score option allows me to see the relevance score of each hit, but I would like to actually use it as the first ordering rule, and use the other one only for results that have the same relevance score.

答案1

得分: 2

以下是您要翻译的内容:

You can sort by more than one criteria. The second sort will work whenever the first sort score is the same.

Here is an example:

POST test_stackoverflow_us/_bulk?refresh=true&pretty
{ "index": {}}
{"name":"obama a", "countryCode":"us", "rating":5}
{ "index": {}}
{"name":"obama b", "countryCode":"us", "rating":4}
{ "index": {}}
{"name":"obama ac", "countryCode":"ar", "rating":3}
{ "index": {}}
{"name":"obama ess", "countryCode":"es", "rating":3.5}

GET test_stackoverflow_us/_search
{
"query": {
"bool": {
"must": [
{
"bool": {
"must": [
{
"match_phrase_prefix": {
"name": {
"query": "obama"
}
}
}
],
"boost": 2
}
}
],
"should": [
{
"term": {
"countryCode": {
"value": "US",
"boost": 4
}
}
},
{
"term": {
"countryCode": {
"value": "AR",
"boost": 3
}
}
},
{
"term": {
"countryCode": {
"value": "ES",
"boost": 2
}
}
}
]
}
},
"size": 50,
"sort": [
{
"_score": {
"order": "desc"
}
},
{
"rating": {
"order": "desc"
}
}
]
}

英文:

You can sort by more than one criteria. The second sort will work whenever the first sort score is the same.

Here is an example:

POST test_stackoverflow_us/_bulk?refresh=true&pretty
{ "index": {}}
{"name":"obama a", "countryCode":"us", "rating":5}
{ "index": {}}
{"name":"obama b", "countryCode":"us", "rating":4}
{ "index": {}}
{"name":"obama ac", "countryCode":"ar", "rating":3}
{ "index": {}}
{"name":"obama ess", "countryCode":"es", "rating":3.5}


GET test_stackoverflow_us/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "must": [
              {
                "match_phrase_prefix": {
                  "name": {
                    "query": "obama"
                  }
                }
              }
            ],
            "boost": 2
          }
        }
      ],
      "should": [
        {
          "term": {
            "countryCode": {
              "value": "US",
              "boost": 4
            }
          }
        },
        {
          "term": {
            "countryCode": {
              "value": "AR",
              "boost": 3
            }
          }
        },
        {
          "term": {
            "countryCode": {
              "value": "ES",
              "boost": 2
            }
          }
        }
      ]
    }
  },
  "size": 50,
  "sort": [
    {
      "_score": {
        "order": "desc"
      }
    },
    {
      "rating": {
        "order": "desc"
      }
    }
  ]
}

如何对具有相同分数的 OpenSearch 结果进行排序?

huangapple
  • 本文由 发表于 2023年2月16日 05:28:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75465604.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定