3

I want to have a complex ranking made out of several functions that I want to weight and multiply with the search _score. I understand 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 which contains a number between 0 and 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 ranked randomly.
  • If I comment out the random_score function, the results are ordered by their score field.
  • If I don't comment out anything, the result is the same as with only random: The second function seems to be ignored
  • Even changing the weights to drastic values do not make any difference in the ranking
  • Also, using a "factor" inside the field_value_factor function does not do anything
  • Swapping the order does not change behavior either...

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

EDIT: Explain output

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

  "_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 the random function always returns 0, and that multiplied with the other factors of course totals 0... Why is that?

cpury
  • 1,003
  • 10
  • 18

1 Answers1

1

I feel this is an issue with the seed value you are providing. The seed value is used to compute the random score. Same seed value always give the same random number.

Hence if you remove the seed value 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 the seed value , then try using a very big number.

Vineeth Mohan
  • 18,633
  • 8
  • 63
  • 77
  • Thanks, but I have tried different seed values before, and also no seed at all, but the result is always the same :( – cpury Jan 10 '15 at 20:12