I've been implementing an event sourcing solution backed in elasticsearch. Documents represent state change events, linked by id field on the _source. There's a sequence field starting at 0, so that the highest sequence per id is the latest event for that id. In practice additional data will be available only on the first event and subsequent events will contain only the fields that have changed. The goal was to have an index I never have to send updates to, only inserts.
Trying to create a query that will returns the first and last events grouped by their id, if an only if their latest event's status matches READY.
Sample data:
[
{
"_index":"events",
"_type":"event",
"_id":"AWcFf2N-IqNGd75vWMgc",
"_score":1,
"_source":{
"id":"event_chain-1",
"status":"SENT",
"sequence":1,
"timestamp":"1541985493824",
"export_batch_id":"103709fe-959f-4b4e-8255-ef59f18a3cf6"
}
},
{
"_index":"events",
"_type":"event",
"_id":"AWbQomwoIqNGd75vWMf6",
"_score":1,
"_source":{
"id":"event_chain-1",
"status":"READY",
"sequence":"0",
"timestamp":"2018-10-31T00:00:00Z"
}
},
{
"_index":"events",
"_type":"event",
"_id":"AWbQomwoIqNGd75vWabc",
"_score":1,
"_source":{
"id":"event_chain-2",
"status":"READY",
"sequence":"0",
"timestamp":"2018-10-31T00:00:00Z"
}
}
]
I wrote a terms aggregation on the id.keyword field, and two top_hits sub aggregations to get the first and latest events by ordering on the sequence and grabbing the top and bottom result respectively.
Problem is any matching I do on the status happens before the aggregations, and I need a way to exclude from the terms aggregation results any hits where the latest_event's status is the one that doesn't match READY.
What I have so far:
POST /events/_search
{
"size": 0,
"query": {
"bool": {
"must": {
"match": {
"status": "READY"
}
}
}
},
"aggs": {
"group_by_id": {
"terms": {
"field": "id.keyword",
"order": {
"_term": "asc"
},
"size": 100
},
"aggs": {
"latest_event": {
"top_hits": {
"sort": [
{
"sequence": {
"order": "desc"
}
}
],
"from": 0,
"size": 1
}
},
"first_event": {
"top_hits": {
"sort": [
{
"sequence": {
"order": "asc"
}
}
],
"from": 0,
"size": 1
}
}
}
},
"num_ready": {
"cardinality": {
"field": "id.keyword"
}
}
}
}
This would return two terms, one for event_chain-1 and one for event_chain-2 when I only want the one for event_chain-2
Terms agg size is so this query can be run in scheduled batches, always scraping the top of the results and updating the chains so they don't come up in the next query.