Thanks for the explanations. but i still couldn’t find a correct query.
So,
POST orders/_doc
{
"order_id": "1",
"status": "started",
"index": 1
}
POST orders/_doc
{
"order_id": "1",
"status": "progress",
"index": 2
}
POST orders/_doc
{
"order_id": "1",
"status": "completed",
"index": 3
}
POST orders/_doc
{
"order_id": "2",
"status": "started",
"index": 1
}
POST orders/_doc
{
"order_id": "2",
"status": "progress",
"index": 2
}
POST orders/_doc
{
"order_id": "3",
"status": "started",
"index": 1
}
POST orders/_doc
{
"order_id": "3",
"status": "progress",
"index": 2
}
here i used index instead of timestamp for simplicity.
if i run your query
GET /orders/_search
{
"aggs": {
"status_breakdown": {
"terms": {
"field": "status.keyword",
"size": 5
}
},
"top_orders": {
"terms": {
"field": "order_id.keyword",
"size": 100
},
"aggs": {
"last_status": {
"top_hits": {
"sort": [
{
"index": {
"order": "desc"
}
}
],
"_source": {
"includes": [ "status" ]
},
"size": 1
}
}
}
}
}
}
My response is
{
"took": 5,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 7,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_index": "orders",
"_id": "PDaQuosBg8S_kochBrsW",
"_score": 1,
"_source": {
"order_id": "1",
"status": "started",
"index": 1
}
},
{
"_index": "orders",
"_id": "PjaQuosBg8S_kochBrte",
"_score": 1,
"_source": {
"order_id": "1",
"status": "progress",
"index": 2
}
},
{
"_index": "orders",
"_id": "PzaQuosBg8S_kochBrt-",
"_score": 1,
"_source": {
"order_id": "1",
"status": "completed",
"index": 3
}
},
{
"_index": "orders",
"_id": "QDaQuosBg8S_kochBru-",
"_score": 1,
"_source": {
"order_id": "2",
"status": "started",
"index": 1
}
},
{
"_index": "orders",
"_id": "QTaQuosBg8S_kochBrvb",
"_score": 1,
"_source": {
"order_id": "2",
"status": "progress",
"index": 2
}
},
{
"_index": "orders",
"_id": "QjaQuosBg8S_kochBrv3",
"_score": 1,
"_source": {
"order_id": "3",
"status": "started",
"index": 1
}
},
{
"_index": "orders",
"_id": "QzaQuosBg8S_kochB7sX",
"_score": 1,
"_source": {
"order_id": "3",
"status": "progress",
"index": 2
}
}
]
},
"aggregations": {
"top_orders": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "1",
"doc_count": 3,
"last_status": {
"hits": {
"total": {
"value": 3,
"relation": "eq"
},
"max_score": null,
"hits": [
{
"_index": "orders",
"_id": "PzaQuosBg8S_kochBrt-",
"_score": null,
"_source": {
"status": "completed"
},
"sort": [
3
]
}
]
}
}
},
{
"key": "2",
"doc_count": 2,
"last_status": {
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": null,
"hits": [
{
"_index": "orders",
"_id": "QTaQuosBg8S_kochBrvb",
"_score": null,
"_source": {
"status": "progress"
},
"sort": [
2
]
}
]
}
}
},
{
"key": "3",
"doc_count": 2,
"last_status": {
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": null,
"hits": [
{
"_index": "orders",
"_id": "QzaQuosBg8S_kochB7sX",
"_score": null,
"_source": {
"status": "progress"
},
"sort": [
2
]
}
]
}
}
}
]
},
"status_breakdown": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "progress",
"doc_count": 3
},
{
"key": "started",
"doc_count": 3
},
{
"key": "completed",
"doc_count": 1
}
]
}
}
}
Status breakdown buckets are not correct.
I need completed 1, progress 2
Can you help me correct the query?