[英]How to use Nested Aggregation on Elasticsearch?
我是Elasticsearch的新手。 我正在使用聚合编写嵌套的dsl。
输入文档的结构是这样的:
{
"_source": {
"id": 1234,
"oid": 6,
"education": [
{
"school_name": "Harvard",
"city" : "Boston",
"year": 1965,
"degree": "Undergrad"
},
{
"school_name": "Harvard",
"city" : "Boston",
"year": 1975,
"degree": "Masters"
},
{
"school_name": "Harvard",
"city" : "Boston",
"year": 1958,
"degree": "BA"
}
],
}
},
----其他记录...等等
*以上显示的文件符合一项记录。
目标:我想找出所有在波士顿学习过的学生。 因此,理想情况下,如果我只有上述文档,那么我应该只会得到1条记录。
在下面编写的嵌套聚合查询中,我得到3作为波士顿的计数
GET cluster_test/index_test/_search
{
"query": {
"bool": {
"must": [
{
"term": {
"oid": {
"value": "6"
}
}
}
]
}
},
"aggs": {
"education": {
"nested": {
"path": "education"
},
"aggs": {
"edu": {
"terms": {
"field": "education.city",
"size": 0
}
}
}
}
}
}
如果有人可以指出我出了问题的地方,或者哪种类型的查询更好。 任何帮助表示赞赏。
您不应该使用汇总,因为您想过滤在所需城市学习的学生。 使用下面的过滤器应该会有所帮助。
GET cluster_test/index_test/students/_search
{
"filtered" : {
"query" : { "match_all" : {} },
"filter" : {
"nested" : {
"path" : "education",
"filter" : {
"bool" : {
"must" : [
{
"term" : {"education.city" : "Boston"}
}
]
}
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.