弹性搜索匹配的聚合字段如何获取特定字段

时间:2020-05-13 04:18:49

标签: elasticsearch elasticsearch-5 elasticsearch-aggregation resthighlevelclient

例如,如果我通过以下六个字段进行汇总,则我需要文档中的另一个字段,即“ id”,该字段应显示为“ Item1-DC3-DC1-2019-01-11T05:30:00.000 + 0530-” 2019 -01-12T05:30:00.000 + 0530“”:下面的[12345]是我的查询,该查询当前仅返回聚合值,如何增强现有查询以返回“ id”,并将列出匹配字段和id的对应值

1)项

2)sourceLocation

3)destinationLocation

4)运输方式

5)发货日期

6)到达日期

                  {
                    "id": "12345",
                    "clusterRank": null,
                    "recommendationRank": null,
                    "batchId": null,
                    "clusterId": "1",
                    "eventId": "1",
                    "exceptionId": "1",
                    "shipmentId": "123",
                    "eventType": "Delayed",
                    "item": "Item1",
                    "destinationLocation": "DC1",
                    "dueDate": "2019-01-12T05:30:00.000+0530",
                    "exceptionQuantity": 50,
                    "recommendationType": "stockTransfer",
                    "customerName": "Walmart",
                    "primaryRecommendation": true,
                    "priority": 1,
                    "sourceLocation": "DC3",
                    "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": null,
                    "revenueRecovered": 20000.0

            },
            {

                    "id": "12345",
                    "clusterRank": null,
                    "recommendationRank": null,
                    "batchId": null,
                    "clusterId": "1",
                    "eventId": "1",
                    "exceptionId": "1",
                    "shipmentId": "123",
                    "eventType": "Delayed",
                    "item": "Item1",
                    "destinationLocation": "DC1",
                    "dueDate": "2019-01-12T05:30:00.000+0530",
                    "exceptionQuantity": 50,
                    "recommendationType": "stockTransfer",
                    "customerName": "Walmart",
                    "primaryRecommendation": true,
                    "priority": 1,
                    "sourceLocation": "DC3",
                    "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": null,
                    "revenueRecovered": 20000.0
                }


{
    "size": 0,
    "query": {
        "bool": {
            "must_not": [
                {
                    "terms": {
                        "id.keyword": [
                            ""
                        ],
                        "boost": 1
                    }
                }
            ],
            "should": [
                {
                    "terms": {
                        "shipmentId.keyword": [
                            "123"
                        ],
                        "boost": 1
                    }
                }
            ],
            "adjust_pure_negative": true,
            "minimum_should_match": "1",
            "boost": 1
        }
    },
    "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"
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

0 个答案:

没有答案
相关问题