0

I am trying to modify scores from normal query with different functions defined in function_score.

To find out what scores calculated by my functions are, I specify "boost_mode" to "replace". However, this makes all scores constant: all equal to 1.

Consider following query:

{
  "query": {
    "function_score": {
      "query": {
        "terms": {
          "name": ["men", "women"]
        }
      },
      "score_mode": "avg",
      "functions": [
        {
          "filter": {
            "terms": {
              "name": ["men","man"]
            }
          },
          "weight": 2
        }
      ],
      "boost_mode": "replace"
    }
  },
  "explain": true,
  "from": 0
}

I am expecting to get different scores here, depending on whether "name" field contain "men" or "man". Such documents are present in index for sure.

Moreover, if I am specifying "explain": true, I am getting score shown in explaination different to one shown in _score field of hit:

{  
    "_shard":0,
    "_node":"ro26nlDuTfiTaIlIgHqg4g",
    "_index":"products10",
    "_type":"product_basic",
    "_id":"0c25fc90433481aac0cce62dd1a21e06",
    "_score":1,
    "_source":{  
        "category":[  
            "Chicago Blues",
            "Blues",
            "Styles",
            "Digital Music"
        ],
        "site_name":"www.amazon.com",
        "name":"Who's That Women?",
        "url":"http://www.amazon.com/dp/B001125F8I/",
        "price":0.99,
        "reviews":[  

        ],
        "breadcrumb":"Digital Music",
        "in_stock":true,
        "features":[  

        ],
        "pic_urls":[  
            "http://ecx.images-amazon.com/images/I/51CvgPMwtsL.jpg",
            "http://ecx.images-amazon.com/images/I/51CvgPMwtsL.jpg"
        ],
        "name_semantic_core":[  
            "Women ?",
            "?"
        ],
        "category_path":"/Chicago Blues/Blues/Styles/",
        "visit_datetime":"2014-11-04T11:50:34.169779",
        "detected_category":"Digital Music"
    },
    "_explanation":{  
        "value":1.2249949,
        "description":"function score, no filter match, product of:",
        "details":[  
            {  
                "value":1.2249949,
                "description":"product of:",
                "details":[  
                    {  
                        "value":2.4499898,
                        "description":"sum of:",
                        "details":[  
                            {  
                                "value":2.4499898,
                                "description":"weight(name:women in 6181332) [PerFieldSimilarity], result of:",
                                "details":[  
                                    {  
                                        "value":2.4499898,
                                        "description":"score(doc=6181332,freq=1.0), product of:",
                                        "details":[  
                                            {  
                                                "value":0.67790973,
                                                "description":"queryWeight, product of:",
                                                "details":[  
                                                    {  
                                                        "value":7.228071,
                                                        "description":"idf(docFreq=238699, maxDocs=120967660)"
                                                    },
                                                    {  
                                                        "value":0.09378847,
                                                        "description":"queryNorm"
                                                    }
                                                ]
                                            },
                                            {  
                                                "value":3.6140356,
                                                "description":"fieldWeight in 6181332, product of:",
                                                "details":[  
                                                    {  
                                                        "value":1,
                                                        "description":"tf(freq=1.0), with freq of:",
                                                        "details":[  
                                                            {  
                                                                "value":1,
                                                                "description":"termFreq=1.0"
                                                            }
                                                        ]
                                                    },
                                                    {  
                                                        "value":7.228071,
                                                        "description":"idf(docFreq=238699, maxDocs=120967660)"
                                                    },
                                                    {  
                                                        "value":0.5,
                                                        "description":"fieldNorm(doc=6181332)"
                                                    }
                                                ]
                                            }
                                        ]
                                    }
                                ]
                            }
                        ]
                    },
                    {  
                        "value":0.5,
                        "description":"coord(1/2)"
                    }
                ]
            },
            {  
                "value":1,
                "description":"queryBoost"
            }
        ]
    }
}

Here explanation shows "value":1.2249949, while "_score" is 1.

What am I doing wrong? How can I get actual scores calculated using functinon_score functions [before combining with original query scores]?

Update: Here's what I get if matching product is found. For some reason, score is 1 while it should be 2: explanation for a matching product

Alexey Tigarev
  • 1,915
  • 1
  • 24
  • 31
  • In your sample, the function doesn't match any docs: `function score, no filter match,`. Also, from the [documentation](https://www.elastic.co/guide/en/elasticsearch/reference/1.7/query-dsl-function-score-query.html) when `replace` is being used, the following happens: `only function score is used, the query score is ignored`. So, the situation is like this: the filter doesn't match - so no scoring is computed - and `replace` will make the query score to be ignored and to use the score from the filter (which doesn't exist as it didn't match). – Andrei Stefan Sep 23 '15 at 07:22
  • And when the function doesn't match, the default value of the function is `1`. You can check this with `"boost_mode": "sum"`. My opinion is that this is the reason why you see a score of `1`. – Andrei Stefan Sep 23 '15 at 07:23
  • @AndreiStefan That's true for sample provided. That product did not match. However for matching product, it still returns 1 by some reason (see update in question). – Alexey Tigarev Sep 23 '15 at 07:37
  • @AndreiStefan Hmm, then when I change "score_mode" from "avg" to "multiply", I am getting meaningful results: first ones scored "2", then ones scored "1" – Alexey Tigarev Sep 23 '15 at 07:40
  • 1
    Yeah, the same goes for `sum`. I wonder how the average is calculated... doesn't look right. – Andrei Stefan Sep 23 '15 at 08:23

1 Answers1

3

In your sample, the function doesn't match any docs: function score, no filter match,. Also, from the documentation when replace is being used, the following happens: only function score is used, the query score is ignored. So, the situation is like this: the filter doesn't match - so no scoring is computed - and replace will make the query score to be ignored and to use the score from the filter (which doesn't exist as it didn't match).

And when the function doesn't match, the default value of the function is 1. You can check this with "boost_mode": "sum". My opinion is that this is the reason why you see a score of 1.

Regarding the avg behavior, this doesn't look ok to me and, most likely, it's a bug. I reported it here: https://github.com/elastic/elasticsearch/issues/13732

Andrei Stefan
  • 51,654
  • 6
  • 98
  • 89
  • "And when the function doesn't match, the default value of the function is 1" I cannot find such statement in es document, could you give me a web link about this? – user8510613 Sep 20 '19 at 02:33
  • I also can't find such reference – Altiano Gerung Aug 17 '20 at 16:59
  • "And when the function doesn't match, the default value of the function is 1" This is quite confusing, especially for "score_mode": "sum". The neutral element should be 0. – Markus Barthlen Apr 07 '21 at 15:34