Elasticsearch函数评分排序

Elasticsearch funtion score sorting

我如何使用 function_score 查询进行排序,现在如果我删除 sort 然后我的查询 returns 正确的结果,它将 boost 公司companyId 为 158。

但是我添加了排序然后它忽略了提升。

为什么会发生这种情况是有道理的,只是不知道如何解决。我尝试查看 script_score 但无法在脚本

中获取过滤器术语
"script_score": {
            "script": "_score * how to get 158?"
          }


...,
  "query": {
    "function_score": {
      "functions": [
        {
          "filter": {
            "terms": {
              "companyId": [
                158
              ]
            }
          },
          "weight": 2
        }
      ],
      "query": {
        "bool": {
          "filter": [
            {
              "terms": {
                "companyGroupId": [
                  1595
                ]
              }
            }
          ]
        }
      }
    }
  },
  "size": 20,
  "sort": [
    {
      "lastName.keyword": {
        "order": "asc"
      }
    }
  ]
}

更新 像下面这样的可能吗?

         "query": {
            "function_score": {
              "functions": [
                {
                  "filter": {
                    "term": {
                      "companyId": 158
                    }
                  },
                  "script_score" : {
                       "script" : "doc['companyId'].value == 158 ? 4 : 0",
                   }
                }
              ],
-- Combining it with an sort--
    "sort": [
        {
          "lastName.keyword": {
            "order": "asc"
          }
        }
      ]

为了排序和计算分数,您需要启用 "Track Scores",如图所示 here.

默认情况下,elasticsearch 在排序时不会计算分数。