java - 如何在 Elasticsearch 上使用嵌套聚合?

标签 java elasticsearch aggregation facet

我是 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/

相关文章:

java - 带有 spring 缓存和 aspectj 的 NoClassDefFoundError

java - A Switch Java 问题 : case expressions must be constant expressions

java - 如何在 Spring Boot 中使用特定日期范围和聚合从 MongoDB 数据库检索数据?

java - 定义没有名字的java类

elasticsearch - Elasticsearch在匹配查询中给出不必要的记录

node.js - Elasticsearch 查询未给出结果,并显示待处理请求

elasticsearch - Nest 1.0不会暴露碎片失败的原因

SQL Server float 总和应为 1,但过滤器另有说明

Elasticsearch 多术语聚合以检索重复项

java - Micronaut 安全失败 "secure"