Java 如何按字段类型对匹配的文档字段进行分组?
我正在使用术语聚合来计算字段值,但在进行聚合之前,我正在基于结果聚合进行筛选搜索。。最后,我需要id和聚合计数作为示例 若异常id为1,若它得到匹配项,那个么我需要输出为 1->“键”:“转移” “单据计数”:2 2->“键”:“库存” “单据计数”:4 我在下面突出显示了异常id,我希望它作为指向每个bucket的指针 我如何在弹性搜索中做到这一点?我已经附上了示例响应Java 如何按字段类型对匹配的文档字段进行分组?,java,
elasticsearch,elastic-stack,resthighlevelclient,Java,
elasticsearch,Elastic Stack,Resthighlevelclient,我正在使用术语聚合来计算字段值,但在进行聚合之前,我正在基于结果聚合进行筛选搜索。。最后,我需要id和聚合计数作为示例 若异常id为1,若它得到匹配项,那个么我需要输出为 1->“键”:“转移” “单据计数”:2 2->“键”:“库存” “单据计数”:4 我在下面突出显示了异常id,我希望它作为指向每个bucket的指针 我如何在弹性搜索中做到这一点?我已经附上了示例响应 { "took": 250, "timed_out": false, "_shards": { "tot
{
"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上进行聚合(两者都位于嵌套对象内),则需要使用嵌套聚合 例如,如果您有一个包含两个嵌套文档的文档,例外ID为1和2。如果要在exceptionId为2的嵌套文档上进行聚合,则即使在“查询”部分使用嵌套查询进行筛选,也需要使用嵌套聚合,因为即使嵌套对象匹配,也会返回整个文档,并且必须在聚合中明确指出要在特定嵌套对象上进行聚合。 质疑 结果:
"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代码用于ealstic search dsl?是否有任何方法可以在弹性搜索中替代java?请告诉我是否正确理解您的问题。您需要匹配嵌套对象的计数,但取而代之的是所有嵌套对象的计数?否。除了术语bucket外,我还需要匹配的id(异常id),它应该指向bucket。。。好的,例如,如果id是1,并且它在文档中匹配,那么连同术语bucket[key and count]我需要这个1来指向bucket。。希望你基本上了解我需要什么。。对于匹配的id,我需要匹配id以及术语桶。。
"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
}
]
}
}
}
]
}
}
}
}