首页 > 解决方案 > Get all documents from elastic search with a field having same value

问题描述

Say I have documents of type Order and they have a field bulkOrderId. Bulkorderid represents a group or bulk of orders issued at once. They all have the same Id like this :

Order {

   bulkOrderId": "bulkOrder:12345678";

}

The id is unique and is generated using UUID.

How do I find groups of orders with the same bulkOrderId from elasticsearch when the bulkOrderId is not known? Is it possible?

标签: elasticsearch

解决方案


您可以使用terms聚合top_hits子聚合来实现,如下所示:

{
  "query": {
    "match_all": {}
  },
  "aggs": {
    "bulks": {
      "terms": {
        "field": "bulkOrderId",
        "size": 10
      },
      "aggs": {
        "orders": {
          "top_hits": {
            "size": 10
          }
        }
      }
    }
  }
}

推荐阅读