Filtering collapsed results in Elasticsearch

Deadly 提交于 2021-02-10 14:36:32

问题


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:

  1. all documents that don't have a deleted_date field (i.e. not deleted) and
  2. all documents that have a deleted_date before/after a given date.


来源:https://stackoverflow.com/questions/56710253/filtering-collapsed-results-in-elasticsearch

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!