# Find the lowest value from nested document for score calculation using script

**URL:** <https://discuss.elastic.co/t/find-the-lowest-value-from-nested-document-for-score-calculation-using-script/65157>\
**Category:** Elasticsearch\
**Created:** [November 7, 2016, 3:40am UTC](https://discuss.elastic.co/t/find-the-lowest-value-from-nested-document-for-score-calculation-using-script/65157 "2016-11-07T03:40:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pungent](https://avatars.discourse-cdn.com/v4/letter/p/a698b9/32.png) [@pungent](https://discuss.elastic.co/u/pungent)\
**Post date:** [November 7, 2016, 3:40am UTC](https://discuss.elastic.co/t/find-the-lowest-value-from-nested-document-for-score-calculation-using-script/65157/1 "2016-11-07T03:40:50Z")

</div>

I am using ElasticSearch 2.4. I have the following mapping with many more properties (simplified here intentionally)

```
{
  "name": {"type": "string"}
  "rents": {
    "type": "nested",
    "dynamic": "strict",
    "properties": {
      "start": { "type": "integer"},
      "end": { "type": "integer"},
      "min": { "type": "integer"}
    }                     
  }
}

```

I have to calculate the search score for each documents using the script. In that score calculation, one of the calculation  
is to find the nested `rents` documents those match the given conditions and in those matched nested documents  
get the LOWEST value of `min` and return that as the score of that script. So I have written the following  
query but it is not working.

```
"query": {
  "bool": {
    "must": [{
      "nested": {
        "path": "rents",
        "query": {
          "function_score": {
            "functions": [{
              "aggs" : {
                "min_price" : {
                  "min" : {
                    "script" : "if(doc['rents.start'].value > 200 && doc['rents.end'].value < 400) {return doc['rents.min'].value;} else {return 0;}"
                  }
                }
              }
            }],
            "score_mode": "sum",
            "boost_mode": "replace"
          }
        }
      }
    }]
  }
}

```

Any idea what is going wrong here?

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [November 7, 2016, 9:23am UTC](https://discuss.elastic.co/t/find-the-lowest-value-from-nested-document-for-score-calculation-using-script/65157/2 "2016-11-07T09:23:26Z")

</div>

What exactly does `not working` mean here?

Also, with such use-cases it would be incredibly helpful to have a reproducible example, including some documents to test with.

---

<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:** [December 5, 2016, 9:23am UTC](https://discuss.elastic.co/t/find-the-lowest-value-from-nested-document-for-score-calculation-using-script/65157/3 "2016-12-05T09:23:40Z")

</div>

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