Here's what I got so far assuming this data lives in an index called "example" with the following mappings:
PUT /example
{
"mappings": {
"properties": {
"order_id": { "type": "integer" },
"error_message": { "type": "keyword" },
"status": { "type": "keyword" }
}
}
}
And the data added with this:
POST example/_doc
{
"order_id": 1,
"status": "completed"
}
POST example/_doc
{
"order_id": 1,
"status": "inprogress"
}
POST example/_doc
{
"order_id": 1,
"error_message": "Not approved",
"status": "inprogress"
}
POST example/_doc
{
"order_id": 1,
"status": "inprogress"
}
POST example/_doc
{
"order_id": 2,
"error_message": "Service NA",
"status": "inprogress"
}
POST example/_doc
{
"order_id": 2,
"status": "inprogress"
}
Running this:
GET example/_search
{
"size": 0,
"aggs": {
"agg1": {
"terms": {
"field": "order_id"
},
"aggs": {
"agg2": {
"terms": {
"field": "status"
},
"aggs": {
"agg3": {
"terms": {
"field": "error_message"
}
}
}
}
}
}
}
}
Would give you this output:
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 8,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"agg1" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : 1,
"doc_count" : 6,
"agg2" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "inprogress",
"doc_count" : 5,
"agg3" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "Not approved",
"doc_count" : 1
}
]
}
},
{
"key" : "completed",
"doc_count" : 1,
"agg3" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [ ]
}
}
]
}
},
{
"key" : 2,
"doc_count" : 2,
"agg2" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "inprogress",
"doc_count" : 2,
"agg3" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "Service NA",
"doc_count" : 1
}
]
}
}
]
}
}
]
}
}
}
Don't worry if the doc count is not the same. I was adding extra docs, but that shouldn't matter overall. As you can see the last key, "2" in this case shows the "inprogress" status and the error message, where as "1" shows that a status of "completed" is present. You'd have to parse that to see the completed and then to ignore it, if that makes sense.