我是 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"}
}
]
}
}
}
}
}
关于java - 如何在 Elasticsearch 上使用嵌套聚合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24320124/