ElasticSearch: Ranking with multiple weighted functions when using random_score, other functions are ignored

I want to have a complex rating made of several functions that I want to weigh and multiply with the _score search. I understand that this is possible with the function_score -> functions parameter. Here's what I have (note, this is Python):

        "function_score": {
            "query": ...,
            "functions": [
                {
                    "random_score" : {
                        "seed":     seed
                    },
                    "weight": 0.1
                },
                {
                    "field_value_factor": {
                        "field":    "score"
                    },
                    "weight": 1
                }
            ],
            "score_mode": "multiply"
        }

      

Notes:

  • Each document has a "score" field that contains a number from 0 to 1
  • "seed" is generated based on user id and current date

Observed behavior:

  • If I comment out the field_value_factor function, the results are randomly ranked.
  • If I comment out the random_score function, the results are ordered by their score field.
  • If I don't comment on anything, the result is the same as with random: the second function seems to be ignored
  • Even changing the weights to sharp values ​​does not affect the rating
  • Also, using a "factor" inside the field_value_factor function does nothing.
  • Reordering doesn't change behavior ...

What am I doing wrong? Any other ways to debug this?

EDIT: Explain the output

Just found out about the explain command! Here is the result for the result with the highest score. Trying to wrap your head around me ...

  "_explanation": {
      "value": 0,
      "description": "function score, product of:",
      "details": [
        {
          "value": 1,
          "description": "ConstantScore(*:*), product of:",
          "details": [
            {
              "value": 1,
              "description": "boost"
            },
            {
              "value": 1,
              "description": "queryNorm"
            }
          ]
        },
        {
          "value": 0,
          "description": "Math.min of",
          "details": [
            {
              "value": 0,
              "description": "function score, score mode [multiply]",
              "details": [
                {
                  "value": 90500,
                  "description": "function score, product of:",
                  "details": [
                    {
                      "value": 1,
                      "description": "match filter: *:*"
                    },
                    {
                      "value": 90500,
                      "description": "product of:",
                      "details": [
                        {
                          "value": 9.05,
                          "description": "field value function: (doc['score'].value * factor=10.0)"
                        },
                        {
                          "value": 10000,
                          "description": "weight"
                        }
                      ]
                    }
                  ]
                },
                {
                  "value": 0,
                  "description": "function score, product of:",
                  "details": [
                    {
                      "value": 1,
                      "description": "match filter: *:*"
                    },
                    {
                      "value": 0,
                      "description": "product of:",
                      "details": [
                        {
                          "value": 0,
                          "description": "random score function (seed: 16121)"
                        },
                        {
                          "value": 0.01,
                          "description": "weight"
                        }
                      ]
                    }
                  ]
                }
              ]
            },
            {
              "value": 3.4028235e+38,
              "description": "maxBoost"
            }
          ]
        },
        {
          "value": 1,
          "description": "queryBoost"
        }
      ]
    }

      

EDIT 2:

So it seems that the random function always returns 0 and which is multiplied by other factors is of course 0 ... Why is that?

+3


source to share


1 answer


I feel this is a problem with the costly value you provide. The initial value is used to compute a random score. The same seed will always give the same random number.

Hence, if you remove the seed from your query, it should work fine. You can refer to this sample -



"function_score": {
    "query": ...,
    "functions": [
        {
            "random_score" : {
            },
            "weight": 0.1
        },
        {
            "field_value_factor": {
                "field":    "score"
            },
            "weight": 1
        }
    ],
    "score_mode": "multiply"
}

      

If you want to use an initial value, try using a very large number.

+1


source







All Articles