gpt4 book ai didi

ElasticSearch (Nest) 条款的条款子聚合 - 未按预期工作

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

考虑到以下映射:

{
"person": {
"properties": {
"id": {
"type": "string"
},
"name": {
"type": "string"
},
...
"trainings": {
"properties": {
"attendanceDate": {
"type": "date",
"format": "dateOptionalTime"
},
"providerId": {
"type": "string",
"index": "not_analyzed"
},
"trainingId": {
"type": "string",
"index": "not_analyzed"
}
...
}
}
}
}
}

这是目前我的 Elasticsearch 查询
{
"aggs": {
"trainning_agg": {
"terms": {
"field": "trainings.trainingId"
},
"aggs": {
"provider_agg": {
"terms": {
"field": "trainings.providerId"
}
}
}
}
}
}

不幸的是, Elasticsearch 将通过包含所有聚合提供者的存储桶列表的培训返回给我一个存储桶列表(忽略培训 ID)。

而不是按训练的存储桶列表,其中包含由父培训过滤的所有提供者的存储桶列表。

任何帮助将不胜感激,并让我知道我可以提供哪些详细信息来帮助您理解我的问题!

谢谢!

编辑:

让我准确地说,目前每个培训目前只有一个且唯一的提供者,因此每个“人”当前将有一个“培训”,其中包含一个元素。 (情况并非总是如此,但为了示例的简单性,我们假设是这样)

目前有 12 个不同的培训,每个培训作为一个提供者。

请注意,当前提供者 ID 与培训 ID 相同(并非总是如此,但为了示例的简单性,我们假设是这样)
这是目前我的回应:
{
...
"aggregations": {
"training_agg": {
...
"buckets": [
{
"key": "17effec3-1284-4609-af41-2252b4dde8b5",
"doc_count": 8808,
"provider_agg": {
...
"buckets": [
{
"key": "17effec3-1284-4609-af41-2252b4dde8b5",
"doc_count": 8808
},
{
"key": "560be929-134b-4b67-befd-f38fdbb2f7ad",
"doc_count": 8808
},
{
"key": "96b16133-51ef-413c-afa5-24f74263126b",
"doc_count": 8808
},
{
"key": "96c36f93-90ce-4378-a9f2-8a8aaa780fd2",
"doc_count": 8808
},
{
"key": "e9467886-42e0-467c-a338-a7fcbc66125f",
"doc_count": 8808
},
{
"key": "f05554c2-778b-44b6-972e-c2fea01d924f",
"doc_count": 8808
},
{
"key": "ab40dcda-e253-4ff3-95e9-314e4ce5ba39",
"doc_count": 8807
},
{
"key": "5aaa2f31-6aa6-4d5a-86ea-8a25cb11247e",
"doc_count": 8081
},
{
"key": "d1c45ad7-046b-43de-b65e-012185c17f6a",
"doc_count": 4686
},
{
"key": "c06e0cee-1050-41b6-ac83-ea3a00f0abb7",
"doc_count": 3135
}
]
}
}
]
}
}
}

这就是我想要得到的:
{
...
"aggregations": {
"training_agg": {
...
"buckets": [
{
"key": "17effec3-1284-4609-af41-2252b4dde8b5",
"doc_count": 8808,
"provider_agg": {
...
"buckets": [
{
"key": "17effec3-1284-4609-af41-2252b4dde8b5",
"doc_count": 8808
}
]
}
}
]
}
}
}

文件示例(必须删除一些信息,保密协议(protocol):O)
注意:providerId 和 trainingId 相同的事实是我插入数据时的纯粹懒惰,它可能是两个完全不同的 UUID。
    {
"hits": [
{
...
"_source": {
"id": "d9753b0fd674f9d55b8cd125258b2713b191499828dc5301162dffc17f3ddf4b",
"name": "Do Van Tien",
...
"trainings": [
{
"trainingName": "Certification - 4C",
"providerName": "Hidden Provider",
"trainingId": "c06e0cee-1050-41b6-ac83-ea3a00f0abb7",
"providerId": "c06e0cee-1050-41b6-ac83-ea3a00f0abb7",
"attendanceDate": "2012-07-07T00:00:00"
},
{
"trainingName": "GAP - Safety & Hygiene",
"providerName": "Hidden Provider",
"trainingId": "96c36f93-90ce-4378-a9f2-8a8aaa780fd2",
"providerId": "96c36f93-90ce-4378-a9f2-8a8aaa780fd2",
"attendanceDate": "2014-04-16T00:00:00"
},
{
"trainingName": "GAP - Crop protection",
"providerName": "Hidden Provider",
"trainingId": "ab40dcda-e253-4ff3-95e9-314e4ce5ba39",
"providerId": "ab40dcda-e253-4ff3-95e9-314e4ce5ba39",
"attendanceDate": "2014-04-16T00:00:00"
},
{
"trainingName": "GAP - Post Harvest",
"providerName": "Hidden Provider",
"trainingId": "d1c45ad7-046b-43de-b65e-012185c17f6a",
"providerId": "d1c45ad7-046b-43de-b65e-012185c17f6a",
"attendanceDate": "2012-07-07T00:00:00"
},
{
"trainingName": "GAP - Soil Management & Fertilizer Application",
"providerName": "Hidden Provider",
"trainingId": "17effec3-1284-4609-af41-2252b4dde8b5",
"providerId": "17effec3-1284-4609-af41-2252b4dde8b5",
"attendanceDate": "2012-08-16T00:00:00"
},
{
"trainingName": "GAP - Irrigation & Water Management",
"providerName": "Hidden Provider",
"trainingId": "560be929-134b-4b67-befd-f38fdbb2f7ad",
"providerId": "560be929-134b-4b67-befd-f38fdbb2f7ad",
"attendanceDate": "2012-07-07T00:00:00"
},
{
"trainingName": "GAP - Composting",
"providerName": "Hidden Provider",
"trainingId": "96b16133-51ef-413c-afa5-24f74263126b",
"providerId": "96b16133-51ef-413c-afa5-24f74263126b",
"attendanceDate": "2013-08-22T00:00:00"
},
{
"trainingName": "Certification - UTZ - Code of Conduct",
"providerName": "Hidden Provider",
"trainingId": "5aaa2f31-6aa6-4d5a-86ea-8a25cb11247e",
"providerId": "5aaa2f31-6aa6-4d5a-86ea-8a25cb11247e",
"attendanceDate": "2013-08-22T00:00:00"
},
{
"trainingName": "Economic - Business skills",
"providerName": "Hidden Provider",
"trainingId": "e9467886-42e0-467c-a338-a7fcbc66125f",
"providerId": "e9467886-42e0-467c-a338-a7fcbc66125f",
"attendanceDate": "2013-08-22T00:00:00"
},
{
"trainingName": "GAP - Pruning",
"providerName": "Hidden Provider",
"trainingId": "f05554c2-778b-44b6-972e-c2fea01d924f",
"providerId": "f05554c2-778b-44b6-972e-c2fea01d924f",
"attendanceDate": "2013-05-09T00:00:00"
}
]
}
},
{
...
"_source": {
"id": "dca2d4b86bf1bea73492bde614e0bb3d7cadf2f4f44e9fe3c5b4cf930d2cca00",
"name": "Le Thi Than",
...
"trainings": [
{
"trainingName": "Certification - UTZ - Code of Conduct",
"providerName": "Hidden Provider",
"trainingId": "5aaa2f31-6aa6-4d5a-86ea-8a25cb11247e",
"providerId": "5aaa2f31-6aa6-4d5a-86ea-8a25cb11247e",
"attendanceDate": "2012-06-25T00:00:00"
},
{
"trainingName": "GAP - Crop protection",
"providerName": "Hidden Provider",
"trainingId": "ab40dcda-e253-4ff3-95e9-314e4ce5ba39",
"providerId": "ab40dcda-e253-4ff3-95e9-314e4ce5ba39",
"attendanceDate": "2014-04-23T00:00:00"
},
{
"trainingName": "Economic - Business skills",
"providerName": "Hidden Provider",
"trainingId": "e9467886-42e0-467c-a338-a7fcbc66125f",
"providerId": "e9467886-42e0-467c-a338-a7fcbc66125f",
"attendanceDate": "2012-06-25T00:00:00"
},
{
"trainingName": "GAP - Soil Management & Fertilizer Application",
"providerName": "Hidden Provider",
"trainingId": "17effec3-1284-4609-af41-2252b4dde8b5",
"providerId": "17effec3-1284-4609-af41-2252b4dde8b5",
"attendanceDate": "2012-07-25T00:00:00"
},
{
"trainingName": "GAP - Composting",
"providerName": "Hidden Provider",
"trainingId": "96b16133-51ef-413c-afa5-24f74263126b",
"providerId": "96b16133-51ef-413c-afa5-24f74263126b",
"attendanceDate": "2012-06-25T00:00:00"
},
{
"trainingName": "GAP - Pruning",
"providerName": "Hidden Provider",
"trainingId": "f05554c2-778b-44b6-972e-c2fea01d924f",
"providerId": "f05554c2-778b-44b6-972e-c2fea01d924f",
"attendanceDate": "2013-05-21T00:00:00"
},
{
"trainingName": "GAP - Irrigation & Water Management",
"providerName": "Hidden Provider",
"trainingId": "560be929-134b-4b67-befd-f38fdbb2f7ad",
"providerId": "560be929-134b-4b67-befd-f38fdbb2f7ad",
"attendanceDate": "2012-06-25T00:00:00"
},
{
"trainingName": "GAP - Safety & Hygiene",
"providerName": "Hidden Provider",
"trainingId": "96c36f93-90ce-4378-a9f2-8a8aaa780fd2",
"providerId": "96c36f93-90ce-4378-a9f2-8a8aaa780fd2",
"attendanceDate": "2013-05-21T00:00:00"
},
{
"trainingName": "GAP - Post Harvest",
"providerName": "Hidden Provider",
"trainingId": "d1c45ad7-046b-43de-b65e-012185c17f6a",
"providerId": "d1c45ad7-046b-43de-b65e-012185c17f6a",
"attendanceDate": "2012-06-25T00:00:00"
}
]
}
}
]
}

最佳答案

好的,那么您只需声明您的trainings type: nested 的属性(property)在您的映射中,如下所示:

{
"mappings": {
"person": {
"properties": {
"id": {
"type": "string"
},
"name": {
"type": "string"
},
"trainings": {
"type": "nested", <----- add "nested" here
"properties": {
"attendanceDate": {
"type": "date",
"format": "dateOptionalTime"
},
"providerId": {
"type": "string",
"index": "not_analyzed"
},
"trainingId": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}
}
}

然后您需要稍微更改聚合以使用新的 nested输入,像这样:
{
"size": 0,
"aggs": {
"trainning_agg": {
"nested": { <----- use nested aggregation
"path": "trainings"
},
"aggs": {
"trainings": {
"terms": {
"field": "trainings.trainingId"
},
"aggs": {
"provider_agg": {
"terms": {
"field": "trainings.providerId"
}
}
}
}
}
}
}
}

响应将如您所料:
{
...
"aggregations" : {
"trainning_agg" : {
"doc_count" : 19,
"trainings" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [ {
"key" : "17effec3-1284-4609-af41-2252b4dde8b5",
"doc_count" : 2,
"provider_agg" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [ {
"key" : "17effec3-1284-4609-af41-2252b4dde8b5",
"doc_count" : 2
} ]
}
}, {
"key" : "560be929-134b-4b67-befd-f38fdbb2f7ad",
"doc_count" : 2,
"provider_agg" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [ {
"key" : "560be929-134b-4b67-befd-f38fdbb2f7ad",
"doc_count" : 2
} ]
}
}, {
"key" : "5aaa2f31-6aa6-4d5a-86ea-8a25cb11247e",
"doc_count" : 2,
"provider_agg" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [ {
"key" : "5aaa2f31-6aa6-4d5a-86ea-8a25cb11247e",
"doc_count" : 2
} ]
}
}, {
"key" : "96b16133-51ef-413c-afa5-24f74263126b",
"doc_count" : 2,
"provider_agg" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [ {
"key" : "96b16133-51ef-413c-afa5-24f74263126b",
"doc_count" : 2
} ]
}
}, {
"key" : "96c36f93-90ce-4378-a9f2-8a8aaa780fd2",
"doc_count" : 2,
"provider_agg" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [ {
"key" : "96c36f93-90ce-4378-a9f2-8a8aaa780fd2",
"doc_count" : 2
} ]
}
}, {
"key" : "ab40dcda-e253-4ff3-95e9-314e4ce5ba39",
"doc_count" : 2,
"provider_agg" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [ {
"key" : "ab40dcda-e253-4ff3-95e9-314e4ce5ba39",
"doc_count" : 2
} ]
}
}, {
"key" : "d1c45ad7-046b-43de-b65e-012185c17f6a",
"doc_count" : 2,
"provider_agg" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [ {
"key" : "d1c45ad7-046b-43de-b65e-012185c17f6a",
"doc_count" : 2
} ]
}
}, {
"key" : "e9467886-42e0-467c-a338-a7fcbc66125f",
"doc_count" : 2,
"provider_agg" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [ {
"key" : "e9467886-42e0-467c-a338-a7fcbc66125f",
"doc_count" : 2
} ]
}
}, {
"key" : "f05554c2-778b-44b6-972e-c2fea01d924f",
"doc_count" : 2,
"provider_agg" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [ {
"key" : "f05554c2-778b-44b6-972e-c2fea01d924f",
"doc_count" : 2
} ]
}
}, {
"key" : "c06e0cee-1050-41b6-ac83-ea3a00f0abb7",
"doc_count" : 1,
"provider_agg" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [ {
"key" : "c06e0cee-1050-41b6-ac83-ea3a00f0abb7",
"doc_count" : 1
} ]
}
} ]
}
}
}
}

关于ElasticSearch (Nest) 条款的条款子聚合 - 未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30906080/

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