我正在使用术语聚合来计算字段值,但在聚合之前,我正在根据结果聚合进行过滤器搜索将起作用..最后我需要 id 和聚合计数 如果异常 id 为 1 并且它匹配,那么我需要输出为
1 -> "key": "转账" “doc_count”:2
2 -> “关键”:“股票” “doc_count”:4
我在下面突出显示了异常 ID,我希望将其作为指向每个存储桶的指针。
我如何在 Elasticsearch 中做到这一点,我已附上示例响应。
{
"took": 250,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 0.0,
"hits": [
{
"_index": "america",
"_type": "_doc",
"_id": "1",
"_score": 0.0,
"_source": {
"clusterId": "1",
"rank": 1,
"events": [
{
"eventId": "1",
"eventType": "Delayed",
"metaInfo": {
"batch_id": "batch_1"
},
"recommendationData": [
{
***"exceptionId": "1",***
"item": "Item1",
"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": "transfer",
"customerName": "Walmart",
"stockTransfer": {
"primaryRecommendation": true,
"priority": 1,
"sourceLocation": "DC3",
"metaInfo": 40,
"shipDate": "2019-01-09T05:30:00.000+0530",
"arrivalDate": "2019-01-10T05:30:00.000+0530",
"transportMode": "Road",
"transferCost": 200.0,
"maxQtyAvailableForTransfer": 40,
"totalQtyAtSource": 40
},
"expedite": null
},
{
"rank": 1,
"recommendationType": "transfer",
"customerName": "Walmart",
"stockTransfer": {
"primaryRecommendation": true,
"priority": 2,
"sourceLocation": "DC2",
"transferQuantity": 60,
"shipDate": "2019-01-09T05:30:00.000+0530",
"arrivalDate": "2019-01-10T05:30:00.000+0530",
"transportMode": "Road",
"transferCost": 600.0,
"maxQtyAvailableForTransfer": 100,
"totalQtyAtSource": 100
},
"expedite": null
}
]
}
]
}
]
}
}
]
},
"aggregations": {
"recommendationTypes": {
"doc_count": 2,
"recommendationTypes": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "transfer",
"doc_count": 2
}
]
}
}
}
}
最佳答案
如果您想对任何 exceptionId 或RecommendationType 进行聚合(两者都位于嵌套对象内),则需要使用嵌套聚合。
例如。如果您有一个文档,其中包含两个嵌套文档,其中 exceptionId 为 1 和 2。您想要聚合异常 ID 为 2 的嵌套文档,则即使您在“查询”部分中使用嵌套查询进行过滤,也需要使用嵌套聚合,因为整个文档即使嵌套对象匹配并且您必须在聚合中特别提及您想要聚合特定嵌套对象时,也会返回。 查询
{
"aggs": {
"recommendations": {
"nested": {
"path": "events.recommendationData"
},
"aggs": {
"exception": {
"filter": {
"terms": {
"events.recommendationData.exceptionId": [
"2"
]
}
},
"aggs": {
"exceptionIds": {
"terms": {
"field": "events.recommendationData.exceptionId.keyword",
"size": 10
},
"aggs": {
"recommendations": {
"nested": {
"path": "events.recommendationData.recommendations"
},
"aggs": {
"recommendationType": {
"terms": {
"field": "events.recommendationData.recommendations.recommendationType",
"size": 10
}
}
}
}
}
}
}
}
}
}
}
}
结果:
"aggregations" : {
"recommendations" : {
"doc_count" : 1,
"exception" : {
"doc_count" : 1,
"exceptionIds" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "2",
"doc_count" : 1,
"recommendations" : {
"doc_count" : 2,
"recommendationType" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "transfer",
"doc_count" : 2
}
]
}
}
}
]
}
}
}
}
关于java - 如何按字段类型对匹配的文档字段进行分组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61184303/