考虑以下映射:
{
"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"
}
...
}
}
}
}
}
这是我目前的弹性搜索查询
{
"aggs": {
"trainning_agg": {
"terms": {
"field": "trainings.trainingId"
},
"aggs": {
"provider_agg": {
"terms": {
"field": "trainings.providerId"
}
}
}
}
}
}
不幸的是,弹性搜索会通过包含所有聚合提供程序的存储列表的训练(忽略训练ID)返回给我一个存储桶列表。
而不是包含训练的存储桶列表,其中包含由父级培训过滤的所有提供者的存储桶列表。
任何帮助都会感激不尽,让我知道我可以提供哪些细节来帮助您理解我的问题!
谢谢!
编辑:
让我确切地说,目前每个培训目前只有一个,而且只有提供者,所以每个人都有#34;目前将有一个"培训"它将包含单个元素。 (情况并非总是这样,但为了简化示例,我们假设这样做)
目前有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
}
]
}
}
]
}
}
}
文档示例(必须删除一些信息,保密协议:O) N.B。:我在插入数据时,提供者Id和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"
}
]
}
}
]
}
答案 0 :(得分:1)
好的,那么您需要的只是在映射中使用trainings
声明type: nested
属性,如下所示:
{
"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
} ]
}
} ]
}
}
}
}