Score_mode avg returns 1 for all documents

Hi Elasticians!

I'm using function score with score_mode avg and boost_mode replace.
And according to documentation I'm expecting the query function score to be overriden by the function score filters.

This works as expected for the sum and multiply, but not for avg (I'm aware that the average in function score is a weighted average)

When I apply this function_score all the documents get a score of 1.
How can this happen?

GET kibana_sample_data_ecommerce/_search
{
  "_source": {
    "includes": ["customer_last_name", "customer_first_name", "customer_gender"]
    
  }, 
  "size": 10, 
  "query": {
    "function_score": {
      "functions": [
        {
          "filter": { "match": { "customer_last_name": "Cook" } },
          "weight": 2
        },
        {
          "filter": { "match": { "customer_first_name": "Jackson" } },
          "weight": 2
        },
        {
          "filter": { "match": { "customer_gender" : "MALE"} },
          "weight": 2
        }
      ],
      "score_mode": "multiply", 
      "boost_mode": "replace"
    }
  }
}

UPDATED CODE SAMPLE (With avg instead of multiply)

GET kibana_sample_data_ecommerce/_search
{
  "_source": {
    "includes": ["customer_last_name", "customer_first_name", "customer_gender"]
    
  }, 
  "size": 10, 
  "query": {
    "function_score": {
      "functions": [
        {
          "filter": { "match": { "customer_last_name": "Cook" } },
          "weight": 2
        },
        {
          "filter": { "match": { "customer_first_name": "Jackson" } },
          "weight": 2
        },
        {
          "filter": { "match": { "customer_gender" : "MALE"} },
          "weight": 2
        }
      ],
      "score_mode": "avg", 
      "boost_mode": "replace"
    }
  }
}

Anyone can help with this one?

Thanks

Hi there can someone help with this one?

Is there someone who understands about scoring who can help?

Thanks

I have initiated a 50 points bounty in stackoverflow for this same question

In my question, even if the functions weights are diferent I Still get 1 as score

@jimczi do you know?

Hi @dadoonet Thank you for the try here for helping me out!

I think I figured it out. the whys I don't know exactly.

But when one of the filters don't match it is doesn't seem to be took into acount in the formula weighted average formula.

More over is that if no filter is matched the score is defined between the min value of 1, and a very large value.

As a result, all if an element match 2 filters it will get 1, because the other 1 filter does not enter in the formula, an element with 1 match filter gets 1 score because the other 2 unmatched filters are also not took into acount. and the 100% unmatched filter documents, get 1 because it is the min between 1, and a very large number. So it really looks like no matter the case we will get always 1 score, when we use score_mode avg and boost_mode replace. I don't know if it happens for boost_mode too but it's out of the scope of this question.

I obtain this information by exploring explain API.

Have a great day, and take care about the corona virus!

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.