弹性搜索嵌套函数得分和脚本评分,函数得分为

时间:2015-09-16 11:43:50

标签: nest elasticsearch

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

但是我需要比较不同文档类型的多个索引。这些文件有不同的字段,具有不同的重要性。 我需要将这些结果的得分进行比较,因此我们希望忽略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"
              }
           ]
        }

1 个答案:

答案 0 :(得分:2)

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

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

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

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