# Script to iterate field by field in a doc

**URL:** <https://discuss.elastic.co/t/script-to-iterate-field-by-field-in-a-doc/8959>\
**Category:** Elasticsearch\
**Created:** [September 8, 2012, 4:40am UTC](https://discuss.elastic.co/t/script-to-iterate-field-by-field-in-a-doc/8959 "2012-09-08T04:40:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sarohan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sarohan/32/4575_2.png) [@sarohan](https://discuss.elastic.co/u/sarohan)\
**Post date:** [September 8, 2012, 4:40am UTC](https://discuss.elastic.co/t/script-to-iterate-field-by-field-in-a-doc/8959/1 "2012-09-08T04:40:29Z")

</div>

Hi all,

I am using custom score to obtain my expected result, such a way i need to iterate the fields in a document and have to do some score values for specified fields in the document, i have tried to get the fields of the doc but Oops!, cud any one help me?

i need the script as like this....

String[] fieldNames = {"field1","field2","field3"};  
for (String fieldName : fieldNames) {  
if(fieldName == "myExpectedField" && fieldName.value == "epectedValue"){  
\_score = \_score+1  
}  
}

and if is there any other way to achieve this please let me know? Thanks in advance

---

<div class="post-metadata">

**Author:** ![wanglifengwf](https://avatars.discourse-cdn.com/v4/letter/w/41988e/32.png) [@wanglifengwf](https://discuss.elastic.co/u/wanglifengwf)\
**Post date:** [November 4, 2016, 10:44am UTC](https://discuss.elastic.co/t/script-to-iterate-field-by-field-in-a-doc/8959/2 "2016-11-04T10:44:54Z")

</div>

you can use :

```auto
String str;for(e in _source.keySet()){ str +=_source[e]};return str;

```

---

<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:** [July 5, 2017, 10:06pm UTC](https://discuss.elastic.co/t/script-to-iterate-field-by-field-in-a-doc/8959/3 "2017-07-05T22:06:35Z")

</div>


