Search code examples
elasticsearchelasticsearch-5elasticsearch-aggregationelasticsearch-dslresthighlevelclient

elasticsearch how to identify which fields have the common aggregated values


I have a scenario where for the aggregated values from the document i want for each aggregation response which id have the aggregated values for example in the document below if i do aggregate by item,destinationlocation,sourcelocation,transportmode,arrivaldate,shipdate i have 2 identical documents for the id 1 and another document with the same properties but id is 2 so i need these two unique id's in the response and also i have attached the query.

          { 
            "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"
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

Solution

  • You can use a top_hits aggregation, it will return all the documents involved in that aggregation

    {
    
        "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"
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }