Elastic search for nested functional indicators and script with function evaluation

I am trying to implement a user rating based on how important the field is.

However, I need to compare several indexes of different types of documents. These documents have different fields with different values. I need the estimates from these results to be comparable and therefore would like to ignore TF / IDF and normalize normalization.

Thus, if the search query corresponds to two important fields and 1 less important field, it should be twice as high as in the case with a more important account:

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

As a result, I get a rating of 11 . Since the query below seems to ignore the evaluation of the internal function and computes:

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

Below is also an explanation of the evaluation, which seems to show that it ignores the internal function_score and just gives it a constant score of 1 (this is what I want to terminate).

I tried not to calculate function scores and use simple queries, and also tried boost_factor instead of "weight" and give the mapped fields to a constant count, all of which have the same result.

Instead of applying constant weight for multiplication, I would like to use script_score to calculate the external result. However, the "_score" that gets skipped is not the score I just calculated, but the original search count. Is there a field that I can use besides "_score" inside script_score to get this?

Thanks in advance!

Query

"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"
 }
}

Explanatory Score

"_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"
              }
           ]
        }
+4
1

. Function_score . , , function_score.

, :

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

, TF/IDF , , 1.

+2

Source: https://habr.com/ru/post/1607634/


All Articles