# Iterate Over all Documents for a specific index and type

**URL:** <https://discuss.elastic.co/t/iterate-over-all-documents-for-a-specific-index-and-type/119335>\
**Category:** Elasticsearch\
**Created:** [February 10, 2018, 10:04am UTC](https://discuss.elastic.co/t/iterate-over-all-documents-for-a-specific-index-and-type/119335 "2018-02-10T10:04:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tameem\_Ahmed](https://avatars.discourse-cdn.com/v4/letter/t/7ea924/32.png) [@Tameem\_Ahmed](https://discuss.elastic.co/u/Tameem_Ahmed)\
**Post date:** [February 10, 2018, 10:04am UTC](https://discuss.elastic.co/t/iterate-over-all-documents-for-a-specific-index-and-type/119335/1 "2018-02-10T10:04:15Z")

</div>

I am newbie and this is my first attempt. I have few documents with this json

{  
"\_index": "msm",  
"\_type": "bat",  
"\_id": "AWBP0tthO9VGNRQOwYSB",  
"\_version": 1,  
"\_score": 1,  
"\_source": {  
"testMethodsSummary": {  
"passed": 5,  
"failed": 1,  
"skipped": 0  
}  
}  
}

These documents are dynamic and keep coming every min. I have to iterate over all documents and do the sum of passed, failed and skipped - calculate the total and do the % of pass.

I tried this and the total is calculated for each document - but I want the total for all documents for different types and index's. Any help would really appreciate.

GET msm/\_search  
{  
"query": {  
"match": {"\_type":"bat"}  
},  
"script\_fields": {  
"total": {  
"script": {  
"lang": "painless",  
"source": "int total = 0; for (int i = 0; i \< doc['testMethodsSummary.passed'].length; ++i) { total += doc['testMethodsSummary.passed'][i]+doc['testMethodsSummary.failed'][i]+doc['testMethodsSummary.skipped'][i]; } return total;"  
}  
}  
}  
}

---

<div class="post-metadata">

**Author:** ![Tameem\_Ahmed](https://avatars.discourse-cdn.com/v4/letter/t/7ea924/32.png) [@Tameem\_Ahmed](https://discuss.elastic.co/u/Tameem_Ahmed)\
**Post date:** [February 13, 2018, 4:15pm UTC](https://discuss.elastic.co/t/iterate-over-all-documents-for-a-specific-index-and-type/119335/2 "2018-02-13T16:15:02Z")

</div>

One of my good friend helped me with understanding bucket script and here is the working one

{  
"size": 0,  
"aggs": {  
"all": {  
"filters": {  
"filters": {  
"all": {  
"range": {  
"testMethodsSummary.passed": {  
"gte": 0  
}  
}  
}  
}  
},  
"aggs": {  
"passed": {  
"sum": {  
"field": "testMethodsSummary.passed"  
}  
},  
"failed": {  
"sum": {  
"field": "testMethodsSummary.failed"  
}  
},  
"skipped": {  
"sum": {  
"field": "testMethodsSummary.skipped"  
}  
},  
"passed\_percent": {  
"bucket\_script": {  
"buckets\_path": {  
"totalPassed": "passed",  
"totalFailed": "failed",  
"totalSkipped": "skipped"  
},  
"script": " params.totalPassed / (params.totalPassed + params.totalFailed + params.totalSkipped) \* 100"  
}  
}  
}  
}  
}  
}

---

<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:** [March 13, 2018, 4:15pm UTC](https://discuss.elastic.co/t/iterate-over-all-documents-for-a-specific-index-and-type/119335/3 "2018-03-13T16:15:09Z")

</div>

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