【问题标题】:Elasticsearch: reverse_nested aggregation under deep nested aggregation doesn't workElasticsearch:深度嵌套聚合下的reverse_nested聚合不起作用
【发布时间】:2019-05-29 21:30:59
【问题描述】:

Elasticsearch 版本:2.3.3

基本上,标题说明了一切。如果在第二个嵌套聚合下使用reverse_nested,尽管文档似乎是通过reverse_nested 限定范围的(请参阅结果中的最后一个"doc_count" 字段),但它后面的聚合不会以某种方式工作。

这里我准备了一个例子——一个文档是一个学生,上面有注册日期和考试历史。

映射:

{
    "mappings": {
        "students": {
            "properties": {
                "name": {
                    "type": "string"},
                "enrollment": {
                    "type": "date"},
                "exam_histories": {
                    "type": "nested",
                    "properties": {
                        "date": {
                            "type": "date"},
                        "subjects": {
                            "type": "nested",
                            "properties": {
                                "name": {
                                    "type": "string"},
                                "score": {
                                    "type": "short"}}}}}}}}}

测试文档:

{
    "name": "John",
    "enrollment": "2012-09-01T00:00:00+00:00",
    "exam_histories": [
        {
            "date": "2016-05-05T00:00:00+00:00",
            "subjects": [
                {
                    "name": "math",
                    "score": 90}]}]}

聚合查询(没有实际意义):

{
    "aggs": {
        "nested_exam_histories": {
            "nested": {
                "path": "exam_histories"},
            "aggs": {
                "date_buckets": {
                    "date_histogram": {
                        "field": "exam_histories.date",
                        "interval": "day"},
                    "aggs": {
                        "this_reverse_nested_does_work": {
                            "reverse_nested": {},
                            "aggs": {
                                "newest_enrollment": {
                                    "max": {
                                        "field": "enrollment"}}}},
                        "deep_nested_subjects": {
                            "nested": {
                                "path": "exam_histories.subjects"},
                            "aggs": {
                                "score_buckets": {
                                    "terms": {
                                        "field": "exam_histories.subjects.score"},
                                    "aggs": {
                                        "this_reverse_nested_doesnt_work": {
                                            "reverse_nested": {},
                                            "aggs": {
                                                "newest_exam_date": {
                                                    "max": {
                                                        "field": "exam_histories.date"}}}}}}}}}}}}}}

结果:

...
"aggregations" : {
    "nested_exam_histories" : {
      "doc_count" : 1,
      "date_buckets" : {
        "buckets" : [ {
          "key_as_string" : "2016-05-05T00:00:00.000Z",
          "key" : 1462406400000,
          "doc_count" : 1,
          "this_reverse_nested_does_work" : {
            "doc_count" : 1,
            "newest_enrollment" : {
              "value" : 1.3464576E12,
              "value_as_string" : "2012-09-01T00:00:00.000Z"
            }
          },
          "deep_nested_subjects" : {
            "doc_count" : 1,
            "score_buckets" : {
              "doc_count_error_upper_bound" : 0,
              "sum_other_doc_count" : 0,
              "buckets" : [ {
                "key" : 90,
                "doc_count" : 1,
                "this_reverse_nested_doesnt_work" : {
                  "doc_count" : 1,
                  "newest_exam_date" : {
                    "value" : null
                  }
...

...您可以在其中看到聚合“newest_exam_date”不起作用。是错误还是我做错了什么?

【问题讨论】:

    标签: elasticsearch elasticsearch-aggregation


    【解决方案1】:

    您需要使用path 选项明确指定要“反向聚合”的嵌套对象,否则它假定该字段位于根级别。

    来自documentation

    path - 定义了应该加入的嵌套对象字段 背部。默认为空,表示加入回根 /主文件级别。路径不能包含对嵌套的引用 位于嵌套聚合的嵌套之外的对象字段 结构中有一个 reverse_nested。 示例:

    {
         "size":0,
       "aggs": {
          "nested_exam_histories": {
             "nested": {
                "path": "exam_histories"
             },
             "aggs": {
                "date_buckets": {
                   "date_histogram": {
                      "field": "exam_histories.date",
                      "interval": "day"
                   },
                   "aggs": {
                      "this_reverse_nested_does_work": {
                         "reverse_nested": {},
                         "aggs": {
                            "newest_enrollment": {
                               "max": {
                                  "field": "enrollment"
                               }
                            }
                         }
                      },
                      "deep_nested_subjects": {
                         "nested": {
                            "path": "exam_histories.subjects"
                         },
                         "aggs": {
                            "score_buckets": {
                               "terms": {
                                  "field": "exam_histories.subjects.score"
                               },
                               "aggs": {
                                  "this_reverse_nested_doesnt_work": {
                                     "reverse_nested": {
                                        "path": "exam_histories"
                                     },
                                     "aggs": {
                                        "newest_exam_date": {
                                           "max": {
                                              "field": "exam_histories.date"
                                           }
                                        }
                                     }
                                  }
                               }
                            }
                         }
                      }
                   }
                }
             }
          }
       }
    }
    

    结果:

     {
       "took": 5,
       "timed_out": false,
       "_shards": {
          "total": 5,
          "successful": 5,
          "failed": 0
       },
       "hits": {
          "total": 2,
          "max_score": 0,
          "hits": []
       },
       "aggregations": {
          "nested_exam_histories": {
             "doc_count": 2,
             "date_buckets": {
                "buckets": [
                   {
                      "key_as_string": "2016-05-05T00:00:00.000Z",
                      "key": 1462406400000,
                      "doc_count": 2,
                      "this_reverse_nested_does_work": {
                         "doc_count": 2,
                         "newest_enrollment": {
                            "value": 1377993600000,
                            "value_as_string": "2013-09-01T00:00:00.000Z"
                         }
                      },
                      "deep_nested_subjects": {
                         "doc_count": 2,
                         "score_buckets": {
                            "doc_count_error_upper_bound": 0,
                            "sum_other_doc_count": 0,
                            "buckets": [
                               {
                                  "key": 90,
                                  "doc_count": 2,
                                  "this_reverse_nested_doesnt_work": {
                                     "doc_count": 2,
                                     "newest_exam_date": {
                                        "value": 1462406400000,
                                        "value_as_string": "2016-05-05T00:00:00.000Z"
                                     }
                                  }
                               }
                            ]
                         }
                      }
                   }
                ]
             }
          }
       }
    }
    

    注意第二个“反向聚合”中的path 选项:

    reverse_nested": {
        "path": "exam_histories"
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-23
      • 2021-12-14
      • 2015-11-18
      • 1970-01-01
      • 2018-11-17
      • 1970-01-01
      • 1970-01-01
      • 2019-08-22
      相关资源
      最近更新 更多