问题
I have an elasticsearch index containing documents that represent entities at a given point in time. When an entity changes state, a new document is created with a timestamp. When I need to get the current state of all entities, I can do the following:
GET https://127.0.0.1:9200/myindex/_search
{
"collapse": {
"field": "entity_id"
},
"sort" : [{
"timestamp": {
"order": "desc"
}
}]
}
However, I would like to further filter the result of the collapse. When entities are deleted I create a new document that includes an is_deleted flag along with the timestamp in a nested metadata field. I would like to extend the above query to entirely filter out those entities that have been deleted. Using a term filter on entity_metadata.is_deleted: true
obviously does not work, because then my result just includes the last document with that entity_id before it got marked as deleted. How can I filter my results after the collapse is done to exclude any tombstoned entites?
回答1:
What I would suggest is that instead of adding an is_deleted
flag to all entity_id
documents, you could add a date_deleted
field with the date of the deletion to all documents of that entity, and then when you view a document, given its date and the deleted_date
you'd know if the document was LIVE or deleted at that date.
In addition, it would allow you to consider:
- all documents that don't have a
deleted_date
field (i.e. not deleted) and - all documents that have a
deleted_date
before/after a given date.
来源:https://stackoverflow.com/questions/56710253/filtering-collapsed-results-in-elasticsearch