elasticsearch 如何识别哪些字段具有共同的聚合值

elasticsearch how to identify which fields have the common aggregated values

我有一个场景,对于文档中的聚合值,我希望每个聚合响应都有聚合值,例如在下面的文档中,如果我按项目、目的地位置、源位置、传输模式、到达日期聚合,发货日期我有 2 个相同的文档,id 为 1,另一个文档具有相同的属性,但 id 为 2,所以我在响应中需要这两个唯一的 id,并且我已经附加了查询。

          { 
            "id": "1",
            "exceptionId": "1",
            "shipmentId": "123",
            "primaryRecommendation": true,
            "priority": 1,
            "sourceLocation": "DC4",
            "transferQuantity": 40,
            "destinationLocation":"DC1",
            "shipDate": "2019-01-11T05:30:00.000+0530",
            "arrivalDate": "2019-01-12T05:30:00.000+0530",
            "transportMode": "Road",
            "transferCost": 200.0,
            "maxQtyAvailableForTransfer": 40,
            "totalQtyAtSource": 40,
            "operation": "Road-Item1-from-DC3-to-DC1",
            "peggedStockDemandIds": "",
            "revenueRecovered": 20000.0
          }
     { 
            "id": "1",
            "exceptionId": "1",
            "shipmentId": "123",
            "primaryRecommendation": true,
            "priority": 1,
            "sourceLocation": "DC4",
            "destinationLocation":"DC1",
            "transferQuantity": 40,
            "shipDate": "2019-01-11T05:30:00.000+0530",
            "arrivalDate": "2019-01-12T05:30:00.000+0530",
            "transportMode": "Road",
            "transferCost": 200.0,
            "maxQtyAvailableForTransfer": 40,
            "totalQtyAtSource": 40,
            "operation": "Road-Item1-from-DC3-to-DC1",
            "peggedStockDemandIds": "",
            "revenueRecovered": 20000.0
          }

          { 
            "id": "2",
            "exceptionId": "1",
            "shipmentId": "123",
            "primaryRecommendation": true,
            "priority": 1,
            "sourceLocation": "DC4",
            "destinationLocation":"DC1",
            "transferQuantity": 40,
            "shipDate": "2019-01-11T05:30:00.000+0530",
            "arrivalDate": "2019-01-12T05:30:00.000+0530",
            "transportMode": "Road",
            "transferCost": 200.0,
            "maxQtyAvailableForTransfer": 40,
            "totalQtyAtSource": 40,
            "operation": "Road-Item1-from-DC3-to-DC1",
            "peggedStockDemandIds": "",
            "revenueRecovered": 20000.0
          }  


{
    "query": {
        "bool": {
            "must": {
                "query_string": {
                    "default_field": "shipmentId",
                    "query": "\"123\""
                }
            },
            "filter": {
                "terms": {
                    "exceptionId": [
                        "2",
                        "1"
                    ]
                }
            },
            "must_not": {
                "terms": {
                    "id": [
                        ""
                    ]
                }
            }
        }
    },
    "aggregations": {
        "by_item": {
            "terms": {
                "field": "item.keyword",
                "size": 10,
                "min_doc_count": 1,
                "shard_min_doc_count": 0,
                "show_term_doc_count_error": false,
                "order": [
                    {
                        "_count": "desc"
                    },
                    {
                        "_key": "asc"
                    }
                ]
            },
            "aggregations": {
                "by_destination": {
                    "terms": {
                        "field": "destinationLocation.keyword",
                        "size": 10,
                        "min_doc_count": 1,
                        "shard_min_doc_count": 0,
                        "show_term_doc_count_error": false,
                        "order": [
                            {
                                "_count": "desc"
                            },
                            {
                                "_key": "asc"
                            }
                        ]
                    },
                    "aggregations": {
                        "by_trans": {
                            "terms": {
                                "field": "transportMode.keyword",
                                "size": 10,
                                "min_doc_count": 1,
                                "shard_min_doc_count": 0,
                                "show_term_doc_count_error": false,
                                "order": [
                                    {
                                        "_count": "desc"
                                    },
                                    {
                                        "_key": "asc"
                                    }
                                ]
                            },
                            "aggregations": {
                                "by_sourcelocation": {
                                    "terms": {
                                        "field": "sourceLocation.keyword",
                                        "size": 10,
                                        "min_doc_count": 1,
                                        "shard_min_doc_count": 0,
                                        "show_term_doc_count_error": false,
                                        "order": [
                                            {
                                                "_count": "desc"
                                            },
                                            {
                                                "_key": "asc"
                                            }
                                        ]
                                    },
                                    "aggregations": {
                                        "by_shipdate": {
                                            "terms": {
                                                "field": "shipDate",
                                                "size": 10,
                                                "min_doc_count": 1,
                                                "shard_min_doc_count": 0,
                                                "show_term_doc_count_error": false,
                                                "order": [
                                                    {
                                                        "_count": "desc"
                                                    },
                                                    {
                                                        "_key": "asc"
                                                    }
                                                ]
                                            },
                                            "aggregations": {
                                                "by_arrival": {
                                                    "terms": {
                                                        "field": "arrivalDate",
                                                        "size": 10,
                                                        "min_doc_count": 1,
                                                        "shard_min_doc_count": 0,
                                                        "show_term_doc_count_error": false,
                                                        "order": [
                                                            {
                                                                "_count": "desc"
                                                            },
                                                            {
                                                                "_key": "asc"
                                                            }
                                                        ]
                                                    },
                                                    "aggregations": {
                                                        "quantity": {
                                                            "sum": {
                                                                "field": "transferQuantity"
                                                            }
                                                        },
                                                        "transfercost": {
                                                            "sum": {
                                                                "field": "transferCost"
                                                            }
                                                        },
                                                        "revenueRecovered": {
                                                            "sum": {
                                                                "field": "revenueRecovered"
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

您可以使用 top_hits 聚合,它将 return 该聚合中涉及的所有文档

{

    "aggregations": {
                "by_destination": {
                    "terms": {
                        "field": "destinationLocation.keyword",
                        "size": 10,
                        "min_doc_count": 1,
                        "shard_min_doc_count": 0,
                        "show_term_doc_count_error": false,
                        "order": [
                            {
                                "_count": "desc"
                            },
                            {
                                "_key": "asc"
                            }
                        ]
                    },
                    "aggregations": {
                        "by_trans": {
                            "terms": {
                                "field": "transportMode.keyword",
                                "size": 10,
                                "min_doc_count": 1,
                                "shard_min_doc_count": 0,
                                "show_term_doc_count_error": false,
                                "order": [
                                    {
                                        "_count": "desc"
                                    },
                                    {
                                        "_key": "asc"
                                    }
                                ]
                            },
                            "aggregations": {
                                "by_sourcelocation": {
                                    "terms": {
                                        "field": "sourceLocation.keyword",
                                        "size": 10,
                                        "min_doc_count": 1,
                                        "shard_min_doc_count": 0,
                                        "show_term_doc_count_error": false,
                                        "order": [
                                            {
                                                "_count": "desc"
                                            },
                                            {
                                                "_key": "asc"
                                            }
                                        ]
                                    },
                                    "aggregations": {
                                        "by_shipdate": {
                                            "terms": {
                                                "field": "shipDate",
                                                "size": 10,
                                                "min_doc_count": 1,
                                                "shard_min_doc_count": 0,
                                                "show_term_doc_count_error": false,
                                                "order": [
                                                    {
                                                        "_count": "desc"
                                                    },
                                                    {
                                                        "_key": "asc"
                                                    }
                                                ]
                                            },
                                            "aggregations": {
                                                "by_arrival": {
                                                    "terms": {
                                                        "field": "arrivalDate",
                                                        "size": 10,
                                                        "min_doc_count": 1,
                                                        "shard_min_doc_count": 0,
                                                        "show_term_doc_count_error": false,
                                                        "order": [
                                                            {
                                                                "_count": "desc"
                                                            },
                                                            {
                                                                "_key": "asc"
                                                            }
                                                        ]
                                                    },
                                                    "aggregations": {
                                                        "docs":{
                                                          "top_hits": { 
                                                            "size": 10,
                                                             "_source": ["id"]
                                                          }
                                                        },
                                                        "quantity": {
                                                            "sum": {
                                                                "field": "transferQuantity"
                                                            }
                                                        },
                                                        "transfercost": {
                                                            "sum": {
                                                                "field": "transferCost"
                                                            }
                                                        },
                                                        "revenueRecovered": {
                                                            "sum": {
                                                                "field": "revenueRecovered"
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }