如何根据 should 子句的匹配次数对 elasticsearch 查询结果进行排名?

How to rank elasticsearch query results by number of matches on the should clause?

我有以下查询:

{
   "query":{
      "bool":{
         "should":[
            {
               "match_phrase":{
                  "related":"robotic"
               }
            },
            {
               "match_phrase":{
                  "related":"robotic arm"
               }
            },
            {
               "match_phrase":{
                  "related":"robot kit"
               }
            },
            {
               "match_phrase":{
                  "related":"robot technology"
               }
            },
            {
               "match_phrase":{
                  "related":"build a robot"
               }
            },
            {
               "match_phrase":{
                  "related":"robot for kids"
               }
            }
         ],
         "minimum_should_match":"1"
      }
   }
}

我想按匹配次数对结果进行排名。也就是说,匹配所有 6 个 should 子句的结果应该在顶部,而只匹配一个的结果应该在底部附近。

我该怎么做?

使用查询时间boosting,您还可以根据每个术语子句中不同的提升值更改几个字段的顺序。

{
    "query": {
        "bool": {
            "should": [{
                    "match_phrase": {
                        "related": {
                            "query": "robotic",
                            "boost": 5
                        }
                    }
                },
                {
                    "match_phrase": {
                        "related": {
                            "query": "robotic arm",
                            "boost": 5
                        }
                    }
                },
                {
                    "match_phrase": {
                        "related": {
                            "query": "robotic kit",
                            "boost": 5
                        }
                    }
                },
                {
                    "match_phrase": {
                        "related": {
                            "query": "robotic technology",
                            "boost": 5
                        }
                    }
                }
            ],
            "minimum_should_match": "1"
        }
    }
}