gpt4 book ai didi

elasticsearch - Elasticsearch 总和仅根据匹配的文档ID汇总

转载 作者:行者123 更新时间:2023-12-03 01:18:41 25 4
gpt4 key购买 nike

我是Elasticsearch的新手,我的文档中有两个具有唯一ID的文档,当exceptionId为1时为exceptionId,然后正确返回文档1的总和,但是如果我想输入两个文档的总和,而我的输入为exceptionId 1和2,然后Elasticsearch将对两个文档进行求和,并在我的情况下为匹配的输入返回结果,相应的文档总和应返回,当输入为多个时,聚合应为多个。

这是映射。

{
"mappings": {
"recommendations": {
"properties": {
"events": {
"type": "nested",
"properties": {
"recommendationData": {
"type": "nested",
"properties": {
"recommendations": {
"type": "nested",
"properties": {
"recommendationType": {
"type": "keyword"
}
}
}
}
}
}
}
}
}
}
}

这是具有exceptionId为“1”的文档
{
"clusterId": "1",
"rank": 1,
"events": [
{
"eventId": "1",
"eventType": "Delayed",
"metaInfo": {
"batch_id": "batch_1"
},
"recommendationData": [
{
"exceptionId": "1",
"item": "KitKat",
"location": "DC1",
"dueDate": "2019-01-10T05:30:00.000+0530",
"quantity": 100,
"metaInfo": {
"batch_id": "batch_1",
"dummy_id": "dummy_1"
},
"rank": 1,
"recommendations": [
{
"rank": 1,
"recommendationType": "Out Of Stock",
"customerName": "Walmart",
"stockTransfer": {
"primaryRecommendation": true,
"priority": 1,
"sourceLocation": "DC1",
"transferQuantity": 100,
"metaInfo": 40,
"shipDate": "01/01/2020",
"arrivalDate": "10/01/2020",
"transportMode": "Air",
"transferCost": 1000,
"maxQtyAvailableForTransfer": 40,
"totalQtyAtSource": 1,
"revenueRecovered": 12000
},
"expedite": null
}
]
}
]
}
]
}

这是具有exceptionId为“2”的文档
{
"clusterId": "2",
"rank": 2,
"events": [
{
"eventId": "2",
"eventType": "Delayed",
"metaInfo": {
"batch_id": "batch_1"
},
"recommendationData": [
{
"exceptionId": "2",
"item": "KitKat",
"location": "DC1",
"dueDate": "2019-01-10T05:30:00.000+0530",
"quantity": 100,
"metaInfo": {
"batch_id": "batch_1",
"dummy_id": "dummy_1"
},
"rank": 1,
"recommendations": [
{
"rank": 1,
"recommendationType": "Out Of Stock",
"customerName": "Walmart",
"stockTransfer": {
"primaryRecommendation": true,
"priority": 1,
"sourceLocation": "DC1",
"transferQuantity": 100,
"metaInfo": 40,
"shipDate": "01/01/2020",
"arrivalDate": "10/01/2020",
"transportMode": "Air",
"transferCost": 1000,
"maxQtyAvailableForTransfer": 40,
"totalQtyAtSource": 1,
"revenueRecovered": 12000
},
"expedite": null
}
]
}
]
}
]
}

这是我尝试过的查询
{
"aggregations": {
"recommendations": {
"nested": {
"path": "events.recommendationData"
},
"aggregations": {
"exceptionId": {
"filter": {
"terms": {
"events.recommendationData.exceptionId": [
"1", "2"
],
"boost": 1
}
},
"aggregations": {
"by_item": {
"terms": {
"field": "events.recommendationData.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": "events.recommendationData.location.keyword",
"size": 10,
"min_doc_count": 1,
"shard_min_doc_count": 0,
"show_term_doc_count_error": false,
"order": [
{
"_count": "desc"
},
{
"_key": "asc"
}
]
},
"aggregations": {
"recommendations": {
"nested": {
"path": "events.recommendationData.recommendations"
},
"aggregations": {
"by_trans": {
"terms": {
"field": "events.recommendationData.recommendations.stockTransfer.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": "events.recommendationData.recommendations.stockTransfer.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": "events.recommendationData.recommendations.stockTransfer.shipDate.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_arrival": {
"terms": {
"field": "events.recommendationData.recommendations.stockTransfer.arrivalDate.keyword",
"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": "events.recommendationData.recommendations.stockTransfer.transferQuantity"
}
},
"transfercost": {
"sum": {
"field": "events.recommendationData.recommendations.stockTransfer.transferCost"
}
},
"revenueRecovered": {
"sum": {
"field": "events.recommendationData.recommendations.stockTransfer.revenueRecovered"
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}

这是这里的输出,它正在对文档1和文档2进行所有求和
       {
"took": 3,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0.0,
"hits": []
},
"aggregations": {
"recommendations": {
"doc_count": 2,
"exceptionId": {
"doc_count": 2,
"by_item": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "KitKat",
"doc_count": 2,
"by_destination": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "DC1",
"doc_count": 2,
"recommendations": {
"doc_count": 2,
"by_trans": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "Air",
"doc_count": 2,
"by_sourcelocation": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "DC1",
"doc_count": 2,
"by_shipdate": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "01/01/2020",
"doc_count": 2,
"by_arrival": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "10/01/2020",
"doc_count": 2,
"quantity": {
"value": 200.0
},
"transfercost": {
"value": 2000.0
},
"revenueRecovered": {
"value": 24000.0
}
}
]
}
}
]
}
}
]
}
}
]
}
}
}
]
}
}
]
}
}
}
}
}

最佳答案

据我对您的要求的理解,您需要在“exceptionId”上进行术语汇总
让我知道这是否适合您
查询:

{
"aggregations": {
"recommendations": {
"nested": {
"path": "events.recommendationData"
},
"aggregations": {
"exceptionId": {
"filter": {
"terms": {
"events.recommendationData.exceptionId": [
"1",
"2"
],
"boost": 1
}
},
"aggregations": {
"exceptionId": {
"terms": {
"field": "events.recommendationData.exceptionId.keyword",
"size": 10
},
"aggregations": {
"by_item": {
"terms": {
"field": "events.recommendationData.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": "events.recommendationData.location.keyword",
"size": 10,
"min_doc_count": 1,
"shard_min_doc_count": 0,
"show_term_doc_count_error": false,
"order": [
{
"_count": "desc"
},
{
"_key": "asc"
}
]
},
"aggregations": {
"recommendations": {
"nested": {
"path": "events.recommendationData.recommendations"
},
"aggregations": {
"by_trans": {
"terms": {
"field": "events.recommendationData.recommendations.stockTransfer.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": "events.recommendationData.recommendations.stockTransfer.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": "events.recommendationData.recommendations.stockTransfer.shipDate.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_arrival": {
"terms": {
"field": "events.recommendationData.recommendations.stockTransfer.arrivalDate.keyword",
"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": "events.recommendationData.recommendations.stockTransfer.transferQuantity"
}
},
"transfercost": {
"sum": {
"field": "events.recommendationData.recommendations.stockTransfer.transferCost"
}
},
"revenueRecovered": {
"sum": {
"field": "events.recommendationData.recommendations.stockTransfer.revenueRecovered"
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}

结果:
"aggregations" : {
"recommendations" : {
"doc_count" : 1,
"exceptionId" : {
"doc_count" : 1,
"exceptionId" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "1",
"doc_count" : 1,
"by_item" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "KitKat",
"doc_count" : 1,
"by_destination" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "DC1",
"doc_count" : 1,
"recommendations" : {
"doc_count" : 1,
"by_trans" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "Air",
"doc_count" : 1,
"by_sourcelocation" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "DC1",
"doc_count" : 1,
"by_shipdate" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "01/01/2020",
"doc_count" : 1,
"by_arrival" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "10/01/2020",
"doc_count" : 1,
"quantity" : {
"value" : 100.0
},
"transfercost" : {
"value" : 1000.0
},
"revenueRecovered" : {
"value" : 12000.0
}
}
]
}
}
]
}
}
]
}
}
]
}
}
}
]
}
}
]
}
}
]
}
}
}
}

关于elasticsearch - Elasticsearch 总和仅根据匹配的文档ID汇总,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61377814/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com