Iterate Over all Documents for a specific index and type

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;"
}
}
}
}

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"
}
}
}
}
}
}

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