# Foreach - Painless

**URL:** <https://discuss.elastic.co/t/foreach-painless/86164>\
**Category:** Elasticsearch\
**Created:** [May 17, 2017, 6:54pm UTC](https://discuss.elastic.co/t/foreach-painless/86164 "2017-05-17T18:54:11Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![thiago.eti](https://avatars.discourse-cdn.com/v4/letter/t/41988e/32.png) [@thiago.eti](https://discuss.elastic.co/u/thiago.eti)\
**Post date:** [May 17, 2017, 6:54pm UTC](https://discuss.elastic.co/t/foreach-painless/86164/1 "2017-05-17T18:54:11Z")

</div>

Oi não estou conseguindo executar LOOP em subdocumentos no Painless.  
Este é apenas um exemplo um cenário criado para executar LOOP o cenário é outro (este caso poderia ser executado filtro mas o problema é o Loop em subdocumentos).

Hi, I am not able to execute LOOP on subdocuments in Painless.  
This is just one example scenario created to run LOOP the scenario is another (this case could be run filter but the problem is the loop in subdocuments).

```
GET /_stats

GET /product/_mapping
GET /product/_settings

GET /product/table/_search
{"size":99}

```

CREATEs

```
DELETE /product

GET /product/table/_search
{"size":99}
PUT /product
{
"mappings":{
	"table":{
		"properties":{
			"name":{"type":"string","index":"not_analyzed"},
			"price":{"type":"float"},
			"subproduct":{
				"type":"nested",
				"properties":{
					"category":{"type":"long"},
					"subname":{"type":"string","index":"not_analyzed"},
					"subprice":{"type":"float"}
				}
			}
		}
	}
}
}

```

DATAs

```
PUT /product/table/1
{"name":"Product 1",
	"price":123,
	"subproduct":[
		{"category":1,
			"subname":"SubProduct 1.1",
			"subprice":1.1},
		{"category":2,
			"subname":"SubProduct 1.2",
			"subprice":1.2},
		{"category":3,
			"subname":"SubProduct 1.2",
			"subprice":1.3}
	]
}

PUT /product/table/2
{"name":"Product 2",
	"price":234,
	"subproduct":[
		{"category":1,
			"subname":"SubProduct 2.1",
			"subprice":2.1},
		{"category":2,
			"subname":"SubProduct 2.2",
			"subprice":2.2},
		{"category":3,
			"subname":"SubProduct 2.2",
			"subprice":2.3}
	]
}

PUT /product/table/3
{"name":"Product 1",
	"price":345,
	"subproduct":[
		{"category":1,
			"subname":"SubProduct 3.1",
			"subprice":3.1},
		{"category":2,
			"subname":"SubProduct 3.2",
			"subprice":3.2},
		{"category":3,
			"subname":"SubProduct 3.2",
			"subprice":3.3}
	]
}

```

QUERYs

```
GET /product/table/_search
{
	"_source":true,
	"size":9,
	"script_fields":{
		"sub":{
			"script":{
				"lang":"groovy",
				"inline":"price=0; for(s in _source.subproduct){if(s.category==2) price=s.subprice;}; return price;"
			}
		}
	}
}

GET /product/table/_search
{
	"_source":true,

	"size":9,
	"script_fields":{
		"sub":{
			"script":{
				"lang":"painless",
				"inline":"def price=0; for(s in _source.subproduct){if(s.category==2) price=s.subprice;} return price;"
			}
		}
	}
}

GET /product/table/_search
{
	"_source":true,
	"size":9,
	"script_fields":{
		"sub":{
			"script":{
				"lang":"painless",
				"inline":"def price=0; for(s in doc['subproduct']){if(s.category==2) price=s.subprice;} return price;"
			}
		}
	}
}

GET /product/table/_search
{
	"_source":true,
	"size":9,
	"script_fields":{
		"sub":{
			"script":{
				"lang":"painless",
				"inline":"def price=0; for (int i=0; i<doc['subproduct'].length; ++i){if(doc['subproduct'][i].category==2) price=doc['subproduct'][i].subprice;} return price;"
			}
		}
	}
}
```

---

<div class="post-metadata">

**Author:** ![thiago](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thiago/32/32096_2.png) [@thiago](https://discuss.elastic.co/u/thiago)\
**Post date:** [May 19, 2017, 4:44pm UTC](https://discuss.elastic.co/t/foreach-painless/86164/2 "2017-05-19T16:44:02Z")

</div>

Hello Chará 😉

What is the result/error that you are getting?

---

<div class="post-metadata">

**Author:** ![thiago](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thiago/32/32096_2.png) [@thiago](https://discuss.elastic.co/u/thiago)\
**Post date:** [May 19, 2017, 6:19pm UTC](https://discuss.elastic.co/t/foreach-painless/86164/3 "2017-05-19T18:19:04Z")

</div>

Nevermind, I've found the issue.

Because it is a nested type, you can not access through `doc` and you need to access it using `_source`. According to [script\_field](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-script-fields.html) docs, you need to use `params['_source']`.

So here is a modified version that will wok:

```auto
GET /product/table/_search
{
	"size":9,
	"script_fields":{
		"sub":{
			"script":{
				"lang":"painless",
				"inline":"def price=0; for (p in params['_source'].subproduct) { if (p.category==2) price=p.subprice; } return price;"
			}
		}
	}
}

```

I hope this helps!

---

<div class="post-metadata">

**Author:** ![thiago.eti](https://avatars.discourse-cdn.com/v4/letter/t/41988e/32.png) [@thiago.eti](https://discuss.elastic.co/u/thiago.eti)\
**Post date:** [May 19, 2017, 6:53pm UTC](https://discuss.elastic.co/t/foreach-painless/86164/4 "2017-05-19T18:53:36Z")

</div>

Brother, funcionou, muito obrigado por tudo.  
A próxima vez que tu vir ao sul, vamos tomar uma cerveja no FreeRiders, onde foi o Meetup de Elastic em Porto Alegre, fica a 200 metros da nossa empresa.

Brother, it worked, thank you so much for everything.  
The next time you come south, let's have a beer at FreeRiders, where it was the Elastic Meetup in Porto Alegre, it's 200 meters from our company.

---

<div class="post-metadata">

**Author:** ![thiago](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thiago/32/32096_2.png) [@thiago](https://discuss.elastic.co/u/thiago)\
**Post date:** [May 19, 2017, 7:06pm UTC](https://discuss.elastic.co/t/foreach-painless/86164/5 "2017-05-19T19:06:14Z")

</div>

Sure! I am glad I could help! I accept the beer invitation 🙂

Also, keep in mind that the recommended way to use script is using script in a file or stored instead of inline scripts. See [https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting-using.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting-using.html) for more information!

Cheers

---

<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:** [June 16, 2017, 7:06pm UTC](https://discuss.elastic.co/t/foreach-painless/86164/6 "2017-06-16T19:06:21Z")

</div>

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