弹性搜索嵌套函数得分和带有功能得分的脚本评分

我正在尝试根据字段的重要性来实现自定义分数.

但是,我需要比较不同文档类型的多个索引.这些文件有不同的字段,具有不同的重要性.
我需要从这些结果中得出的分数具有可比性,因此我们希望忽略TF / IDF和得分归一化.

因此,如果搜索查询匹配2个重要字段和1个不太重要的字段,则其分数应为重要分数的两倍加上不太重要的分数:

(8* (1+1)) + (3*(1)) = 19

我得到的结果是得分11.由于下面的查询似乎忽略内部函数得分和计算:

(8*1) + (3*1).

分数解释也在下面,这似乎表明它忽略了内部的function_score,只是给它一个1的常数分数(这就是我想要停止发生的事情).

我试过没有嵌套函数得分和使用简单的应该查询以及尝试使用boost_factor而不是’weight’并给匹配的字段一个恒定的分数,所有这些都有相同的结果.

而不是应用恒定的权重乘以我想使用script_score来计算外部结果.然而,传递的’_score’不是我刚刚计算的分数,而是原始搜索分数.
在script_score中我是否可以使用“_score”以外的字段来获取此字段?

提前致谢!

询问

"query": {
 "function_score": {
  "functions": [
    {
      "weight": 8.0,
      "filter": {
        "fquery": {
          "query": {
            "function_score": {
              "functions": [
                {
                  "weight": 1.0,
                  "filter": {
                    "fquery": {
                      "query": {
                        "query_string": {
                          "query": "match*",
                          "fields": [
                            "ImportantField1"
                          ],
                          "default_operator": "and",
                          "analyzer": "english",
                          "analyze_wildcard": true
                        }
                      }
                    }
                  }
                },
                {
                  "weight": 1.0,
                  "filter": {
                    "fquery": {
                      "query": {
                        "query_string": {
                          "query": "match*",
                          "fields": [
                            "ImportantField2"
                          ],
                          "default_operator": "and",
                          "analyzer": "english",
                          "analyze_wildcard": true
                        }
                      }
                    }
                  } // More field queries that don't match omitted for clarity
                }
              ],
              "score_mode": "sum",
              "boost_mode": "replace"
            }
          }
        }
      }
    },
    {
      "weight": 3.0,
      "filter": {
        "fquery": {
          "query": {
            "function_score": {
              "functions": [
                {
                  "weight": 1.0,
                  "filter": {
                    "fquery": {
                      "query": {
                        "query_string": {
                          "query": "match*",
                          "fields": [
                            "LessImportantField"
                          ],
                          "default_operator": "and",
                          "analyzer": "english",
                          "analyze_wildcard": true
                        }
                      }
                    }
                  }
                }// More field queries that don't match omitted for clarity

              ],
              "query": {
                "match_all": {}
              },
              "score_mode": "sum",
              "boost_mode": "replace"
            }
          }
        }
      }
    }
  ],
  "query": {
     "match_all": {} // Filtering done here, omitted for clarity
    }
  },
  "score_mode": "sum",
  "boost_mode": "replace"
 }
}

分数说明

"_explanation": {
           "value": 11,
           "description": "function score, product of:",
           "details": [
              {
                 "value": 11,
                 "description": "Math.min of",
                 "details": [
                    {
                       "value": 11,
                       "description": "function score, score mode [sum]",
                       "details": [
                          {
                             "value": 8,
                             "description": "function score, product of:",
                             "details": [
                                {
                                   "value": 1,
                                   "description": "match filter: QueryWrapperFilter(function score (ConstantScore(*:*), functions: [{filter(QueryWrapperFilter(ImportantField1:match*)), function [org.elasticsearch.common.lucene.search.function.WeightFactorFunction@64b3fd0e]}{filter(QueryWrapperFilter(ImportantField2:match*)), function [org.elasticsearch.common.lucene.search.function.WeightFactorFunction@38ed4b5c]}]))"
                                },
                                {
                                   "value": 8,
                                   "description": "product of:",
                                   "details": [
                                      {
                                         "value": 1,
                                         "description": "constant score 1.0 - no function provided"
                                      },
                                      {
                                         "value": 8,
                                         "description": "weight"
                                      }
                                   ]
                                }
                             ]
                          },
                          {
                             "value": 3,
                             "description": "function score, product of:",
                             "details": [
                                {
                                   "value": 1,
                                   "description": "match filter: QueryWrapperFilter(function score (ConstantScore(*:*), functions: [{filter(QueryWrapperFilter(LessImportantField:match*)), function [org.elasticsearch.common.lucene.search.function.WeightFactorFunction@3ce99ebf]}]))"
                                },
                                {
                                   "value": 3,
                                   "description": "product of:",
                                   "details": [
                                      {
                                         "value": 1,
                                         "description": "constant score 1.0 - no function provided"
                                      },
                                      {
                                         "value": 3,
                                         "description": "weight"
                                      }
                                   ]
                                }
                             ]
                          }
                       ]
                    },
                    {
                       "value": 3.4028235e+38,
                       "description": "maxBoost"
                    }
                 ]
              },
              {
                 "value": 1,
                 "description": "queryBoost"
              }
           ]
        }

最佳答案 所以这是不可能的. Function_score仅在其函数中使用过滤器来应用分数.这意味着它们匹配或不匹配,因此嵌套的function_score的分数无法传递.

我设法使用以下方法禁用查询规范化:

"similarity": {
           "default": {
              "queryNorm": "1",
              "type": //whatever type you want
              }
            }

然而这意味着TF / IDF对我来说成了问题,因为我的每个索引的这些值都不同,所以我最终使用编写自定义相似度类并将这些值设置为1的常量.

点赞