弹性搜索-使用原始查询分数作为对function_score的二级排序

时间:2019-09-30 08:32:36

标签: javascript sorting elasticsearch elasticsearch-5

我正在使用function_score创建自定义相关性分数,并希望将原始查询分数用作辅助排序。这是查询的简单版本。

{
  "from": 0,
  "size": 20,
  "query": {
    "function_score": {
      "functions": [
        {
          "filter": [
            {
              "match": {
                "keywords": {
                  "query": "pop",
                  "operator": "or"
                }
              }
            }
          ],
          "weight": 1400
        },
        {
          "filter": [
            {
              "match": {
                "genre": {
                  "query": "pop",
                  "operator": "or"
                }
              }
            }
          ],
          "weight": 1300
        },
        {
          "filter": [
            {
              "match": {
                "trackDescription": {
                  "query": "pop",
                  "operator": "or"
                }
              }
            }
          ],
          "weight": 1200
        },
        {
          "filter": [
            {
              "match": {
                "album.albumDisplayTitle": {
                  "query": "pop",
                  "operator": "or"
                }
              }
            }
          ],
          "weight": 1100
        },
        {
          "filter": [
            {
              "term": {
                "library.libraryID": "202"
              }
            }
          ],
          "weight": 1050
        },
        {
          "filter": [
            {
              "range": {
                "releaseDate": {
                  "gte": "now-7d"
                }
              }
            }
          ],
          "weight": 2500
        }
      ],
      "score_mode": "sum",
      "boost_mode": "replace",
      "query": {
        "bool": {
          "filter": [
            {
              "term": {
                "isAlternate": "false"
              }
            }
          ],
          "must": [
            {
              "multi_match": {
                "query": "pop",
                "operator": "or",
                "fields": [
                  "trackDisplayTitle",
                  "trackDescription",
                  "genre",
                  "keywords",
                  "lyrics",
                  "album.albumCDCode",
                  "album.albumDisplayTitle",
                  "album.keywords"
                ]
              }
            }
          ]
        }
      }
    }
  },
  "sort": [
    {
      "_score": {
        "order": "desc"
      }
    },
    {
      "releaseDate": {
        "order": "desc"
      }
    }
  ]
}

我尝试做的第一件事是将“ boost_mode”更改为“ sum”,这会将原始查询分数添加到自定义分数,并提供了一定的深度。问题在于查询分数没有任何界限,这意味着它有可能使自定义评分蒙上阴影。我找不到标准化查询分数的方法。

我目前唯一可行的解​​决方案是进行第二次搜索,而不进行自定义排序,并使用其中的查询分数来增强第一次搜索的排序。这看起来很混乱。

有更好的方法吗?我本来以为会支持以下内容,但事实并非如此。

sort": [
    {
      "_score": {
        "order": "desc"
      }
    },
    { // only _score variable is supported
      "_queryscore": {
        "order": "desc"
    }
    },
    {
      "releaseDate": {
        "order": "desc"
      }
    }
  ]

或者有什么方法可以告诉ES使用查询分数作为决胜局?

任何提示表示赞赏。

0 个答案:

没有答案