2016-08-18 219 views
1

Elasticsearch版本:2.3.3Elasticsearch:在深度嵌套聚合下的reverse_nested聚合不起作用

基本上標題說的都是。如果在第二個嵌套聚合下使用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」不起作用。這是一個錯誤還是我做錯了什麼?

回答

1

您需要明確指定您要使用path選項「反向聚合」的嵌套對象,否則它會假定該字段位於根級別。

documentation

路徑 - 它定義什麼嵌套的對象字段應該加入 回來。默認值爲空,這意味着它會加入根文件夾 /主文檔級別。的路徑不能包含對嵌套聚合的嵌套 結構之外落入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" 
           } 
           } 
          } 
         ] 
        } 
        } 
       } 
      ] 
     } 
     } 
    } 
} 

注第二個「反向aggergation」中的path選項:

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