# Is it possible getting parent doc's score in nested query?

**URL:** <https://discuss.elastic.co/t/is-it-possible-getting-parent-docs-score-in-nested-query/69727>\
**Category:** Elasticsearch\
**Created:** [December 22, 2016, 2:58am UTC](https://discuss.elastic.co/t/is-it-possible-getting-parent-docs-score-in-nested-query/69727 "2016-12-22T02:58:27Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Youxu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/youxu/32/117375_2.png) [@Youxu](https://discuss.elastic.co/u/Youxu)\
**Post date:** [December 22, 2016, 2:58am UTC](https://discuss.elastic.co/t/is-it-possible-getting-parent-docs-score-in-nested-query/69727/1 "2016-12-22T02:58:27Z")

</div>

For example, I have an index with following mapping:

```auto
{
    "mappings": {
      "docs": {
        "properties": {
          "QueryClicks": {
            "type": "nested",
            "properties": {
              "Count": {
                "type": "long"
              },
              "Term": {
                "type": "string",
                "fields": {
                  "lowercaseraw": {
                    "type": "string",
                    "analyzer": "lowercase_keyword"
                  }
                }
              }
            }
          },
          "Title": {
            "type": "string"
          }
        }
      }
    }
}

```

And example document:

```auto
{
  "Title": "The Heart of the Elastic Stack"
  "QueryClicks": [
      { "Term": "elastic stack", "Count": 100},
      { "Term": "elastic", "Count": 50},
      { "Term": "hard of the elastic", "Count": 200},
  ]
}

```

Now I want to get all documents which match query terms in Title field, and boost the score by QueryClicks.Count if the query term exactly match the corresponding QueryClicks.Term field.

Following is my initial query DSL:

```auto
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {"Title": "elastic stack"}
        }
      ],
     "should": [
        {
          "nested": {
            "path": "QueryClicks",
            "query": {
              "function_score": {
                "query": {
                  "match": {"QueryClicks.Term.lowercaseraw": "elastic stack"}
                },
                "functions": [{
                  "script_score": {
                    "script": "log(doc['QueryClicks.Count'].value*4)"
                  }
                }],
                "boost_mode": "replace"
              }
            }
          }
        }
     ]
    }
  }
}

```

It somehow works, for certain queryterm, if it match one of the QueryClicks.Term, extra scores will be added to the whole socre of the document.

But not perfect. What I want is to **multiply** the nested function score (that is, log(doc['QueryClicks.Count'].value\*4) ) with the parent document's score which calculated in must clause.

If I can get the parent doc's score, then I can do something like this:

`"script": "log(doc['QueryClicks.Count'].value*4) * _parent_score"`

Is it possible?

Or any other approach to solve my requirement?

---

<div class="post-metadata">

**Author:** ![Youxu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/youxu/32/117375_2.png) [@Youxu](https://discuss.elastic.co/u/Youxu)\
**Post date:** [December 23, 2016, 1:42am UTC](https://discuss.elastic.co/t/is-it-possible-getting-parent-docs-score-in-nested-query/69727/2 "2016-12-23T01:42:21Z")

</div>

Anyone has suggestion?

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [December 26, 2016, 4:36pm UTC](https://discuss.elastic.co/t/is-it-possible-getting-parent-docs-score-in-nested-query/69727/3 "2016-12-26T16:36:34Z")

</div>

I would try wraping the portion under `must` into a function score\_query and convert the `should` part into a function of that function score query. So, you would end up with two function\_score queries, one inside another.

---

<div class="post-metadata">

**Author:** ![Youxu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/youxu/32/117375_2.png) [@Youxu](https://discuss.elastic.co/u/Youxu)\
**Post date:** [December 26, 2016, 9:49pm UTC](https://discuss.elastic.co/t/is-it-possible-getting-parent-docs-score-in-nested-query/69727/4 "2016-12-26T21:49:09Z")

</div>

Thanks!  
I tried to build query with function\_score inside another function\_score, but could not work out a workable query.

Could you give me an simple example of function\_score inside another function\_score?

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [December 26, 2016, 9:50pm UTC](https://discuss.elastic.co/t/is-it-possible-getting-parent-docs-score-in-nested-query/69727/5 "2016-12-26T21:50:26Z")

</div>

Could you show me the query that you tried and that didn't work?

---

<div class="post-metadata">

**Author:** ![Youxu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/youxu/32/117375_2.png) [@Youxu](https://discuss.elastic.co/u/Youxu)\
**Post date:** [December 26, 2016, 10:18pm UTC](https://discuss.elastic.co/t/is-it-possible-getting-parent-docs-score-in-nested-query/69727/6 "2016-12-26T22:18:42Z")

</div>

```auto
{
  "query" : {
    "function_score" : {
      "query" : {
        "match" : {
          "Title" : "elastic stack"
        }
      },
      "functions" : [{
          "filter" : {
            "nested" : {
              "path" : "QueryClicks",
              "query" : {
                "function_score" : {
                  "query" : {
                    "match" : {
                      "QueryClicks.Term.lowercaseraw" : "elastic stack"
                    }
                  },
                  "functions" : [{
                      "script_score" : {
                        "script" : "10000"
                      }
                    }
                  ],
                  "boost_mode" : "replace"
                }
              }
            }
          },
          "weight" : 1
        }
      ],
      "boost_mode" : "multiply"
    }
  }
}

```

To verify if it works, in the inside function\_score, I simply set the score to 10000, and expect the final score will be around 10000, but it is actually around 3.

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [December 26, 2016, 10:26pm UTC](https://discuss.elastic.co/t/is-it-possible-getting-parent-docs-score-in-nested-query/69727/7 "2016-12-26T22:26:21Z")

</div>

Could you execute it with the [explain](https://www.elastic.co/guide/en/elasticsearch/reference/5.1/search-request-explain.html) flag to see why?

---

<div class="post-metadata">

**Author:** ![Youxu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/youxu/32/117375_2.png) [@Youxu](https://discuss.elastic.co/u/Youxu)\
**Post date:** [December 26, 2016, 10:43pm UTC](https://discuss.elastic.co/t/is-it-possible-getting-parent-docs-score-in-nested-query/69727/8 "2016-12-26T22:43:27Z")

</div>

Here is the explain output of first matched doc:

```auto
"_score" : 3.2159193,
"_explanation" : {
  "value" : 3.2159193,
  "description" : "sum of:",
  "details" : [{
      "value" : 3.2159193,
      "description" : "weight(Title:elastic stack in 104357) [PerFieldSimilarity], result of:",
      "details" : [{
          "value" : 3.2159193,
          "description" : "fieldWeight in 104357, product of:",
          "details" : [{
              "value" : 1,
              "description" : "tf(freq=1.0), with freq of:",
              "details" : [{
                  "value" : 1,
                  "description" : "termFreq=1.0",
                  "details" : []
                }
              ]
            }, {
              "value" : 3.2159193,
              "description" : "idf(docFreq=15520, maxDocs=142325)",
              "details" : []
            }, {
              "value" : 1,
              "description" : "fieldNorm(doc=104357)",
              "details" : []
            }
          ]
        }
      ]
    }, {
      "value" : 0,
      "description" : "match on required clause, product of:",
      "details" : [{
          "value" : 0,
          "description" : "# clause",
          "details" : []
        }, {
          "value" : 0.31095308,
          "description" : "_type:docs, product of:",
          "details" : [{
              "value" : 1,
              "description" : "boost",
              "details" : []
            }, {
              "value" : 0.31095308,
              "description" : "queryNorm",
              "details" : []
            }
          ]
        }
      ]
    }
  ]
}

```

It missed following like output from my original must + should query:

```auto
{
  "value": 10000,
  "description": "Score based on child doc range from 138429 to 138546",
  "details": []
}

```

Seems the nested function score is not executed?

---

<div class="post-metadata">

**Author:** ![Youxu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/youxu/32/117375_2.png) [@Youxu](https://discuss.elastic.co/u/Youxu)\
**Post date:** [December 26, 2016, 10:48pm UTC](https://discuss.elastic.co/t/is-it-possible-getting-parent-docs-score-in-nested-query/69727/9 "2016-12-26T22:48:57Z")

</div>

I think I know why.

Coz the nested function\_score is wrapped in "filter", so no score...

But if I changed "filter" to "query", I got parse error.

I read the ES reference, seems no way to wrap a query in functions clause of a function\_score query.

Any suggestion?

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [December 26, 2016, 11:34pm UTC](https://discuss.elastic.co/t/is-it-possible-getting-parent-docs-score-in-nested-query/69727/10 "2016-12-26T23:34:17Z")

</div>

Yep, you are right, that wouldn't really work. Sorry about that. I played with it a bit more and I don't see any reasonable solution here. The only crazy solution that comes to mind at the moment is to go back to the `bool` query wrap the must part into a `function_score` and calculate `log(_score)` in the script, then calculate `log(log(doc['QueryClicks.Count'].value*4))` in `should`. Considering that `log(xz)=log(x) + log(y)` you can replace multiplication with addition that takes place in `bool` and since log is a strictly increasing function for positive x you can, theoretically, use log of the score instead of score for comparison. Practically speaking, I am not sure if this solution would work giving relatively low resolution of float score.

---

<div class="post-metadata">

**Author:** ![mvg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mvg/32/98890_2.png) [@mvg](https://discuss.elastic.co/u/mvg)\
**Post date:** [December 27, 2016, 9:23am UTC](https://discuss.elastic.co/t/is-it-possible-getting-parent-docs-score-in-nested-query/69727/11 "2016-12-27T09:23:46Z")

</div>

Queries wrapped by the `nested` query can't access the score of the parent  
document, because at the time of scoring the parent document hasn't been  
matched yet and therefor its score hasn't been computed yet.

---

<div class="post-metadata">

**Author:** ![Youxu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/youxu/32/117375_2.png) [@Youxu](https://discuss.elastic.co/u/Youxu)\
**Post date:** [December 27, 2016, 1:02pm UTC](https://discuss.elastic.co/t/is-it-possible-getting-parent-docs-score-in-nested-query/69727/12 "2016-12-27T13:02:15Z")

</div>

Thanks you all.  
So any other workaround except Igor's amazing solution of using math?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [January 24, 2017, 1:02pm UTC](https://discuss.elastic.co/t/is-it-possible-getting-parent-docs-score-in-nested-query/69727/13 "2017-01-24T13:02:39Z")

</div>

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