# Painless iterate over nested data in an array to calculate custom score

**URL:** <https://discuss.elastic.co/t/painless-iterate-over-nested-data-in-an-array-to-calculate-custom-score/104711>\
**Category:** Elasticsearch\
**Created:** [October 20, 2017, 1:56pm UTC](https://discuss.elastic.co/t/painless-iterate-over-nested-data-in-an-array-to-calculate-custom-score/104711 "2017-10-20T13:56:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![el-n00b](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/el-n00b/32/23237_2.png) [@el-n00b](https://discuss.elastic.co/u/el-n00b)\
**Post date:** [October 20, 2017, 1:56pm UTC](https://discuss.elastic.co/t/painless-iterate-over-nested-data-in-an-array-to-calculate-custom-score/104711/1 "2017-10-20T13:56:54Z")

</div>

I'd like to iterate over a set of child elements on a search in order to calculate a value using Painless and then return that information with the rest of the search result. I decided to hijack the score calculation.

Basic index:

```
{
  "mappings": {
    "book": {
      "properties": {
        "title": { "type": "string", "index": "not_analyzed" },
        "topics": {
          "type": "nested",
          "properties": {
            "weight": { "type": "int" },
            "title": { "type": "string" }
          }
        }
      }
    }
  }
}

```

Assuming a sample document like this in `/books/book/`:

```
{
  "title": "Title 1",
  "topics": [
    { "weight": 123, "title": "foo" },
    { "weight": 4, "title": "bar" },
    { "weight": 72, "title": "baz" }
  ]
}

```

I formatted my query with a script like this:

```
{
  "query": {
  	"function_score": {
  	  "query": {
  	    "term": { "title": "QT-0003" }
  	  },
  	  "script_score": {
  	  	"script": {
  	  		"lang": "painless",
  	  		"inline": "int score = 0; for(int i = 0; i < doc['topics'].values.length; i++) { score += doc['topics'][i].weight; } return score;",
  	  		"params": {
  	  		  "default_return_value": 100
  	  		}
  	  	}
  	  }
  	}
  }
}

```

My problem is this:

> No field found for [topics] in mapping with types [book]

---

<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:** [November 17, 2017, 1:57pm UTC](https://discuss.elastic.co/t/painless-iterate-over-nested-data-in-an-array-to-calculate-custom-score/104711/2 "2017-11-17T13:57:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pickypg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pickypg/32/62409_2.png) [@pickypg](https://discuss.elastic.co/u/pickypg)\
**Post date:** [March 8, 2018, 4:33pm UTC](https://discuss.elastic.co/t/painless-iterate-over-nested-data-in-an-array-to-calculate-custom-score/104711/3 "2018-03-08T16:33:32Z")

</div>

Hi @el-n00b,

I realize that this response is a bit late, but in case you or anyone else is still wondering:

To use `nested` fields, you must enter the `nested` context. To do so, you simply need to wrap whatever query / filter you want in a `nested` query:

```auto
{
  "query": {
    "nested": {
      "path": "title",
      "query": { ... }
    }
  }
}

```

Unless you're in the right path of a `nested` context, then you cannot access the given field.

Hope that helps,  
Chris

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [March 8, 2018, 4:44pm UTC](https://discuss.elastic.co/t/painless-iterate-over-nested-data-in-an-array-to-calculate-custom-score/104711/4 "2018-03-08T16:44:40Z")

</div>



---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [April 1, 2018, 10:00pm UTC](https://discuss.elastic.co/t/painless-iterate-over-nested-data-in-an-array-to-calculate-custom-score/104711/5 "2018-04-01T22:00:10Z")

</div>

This topic was automatically closed after 24 days. New replies are no longer allowed.
