我对弹性搜索很新。 我正在使用聚合编写嵌套的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
}
}
}
}
}
}
如果有人能够指出我出错的地方或者更好地处理这些类型的查询。 任何帮助表示赞赏。
答案 0 :(得分:0)
您不应该使用聚合,因为您希望过滤在所需城市上学习的学生。使用下面的过滤器应该会有所帮助。
GET cluster_test/index_test/students/_search
{
"filtered" : {
"query" : { "match_all" : {} },
"filter" : {
"nested" : {
"path" : "education",
"filter" : {
"bool" : {
"must" : [
{
"term" : {"education.city" : "Boston"}
}
]
}
}
}
}
}