2016-09-07 85 views
0
{ 
"aggs":{ 
    "nest_exams":{ 
     "nested":{ 
      "path": "exams" 
     }, 
     "aggs":{ 
      "exams":{ 
       "filter" : { 
        "term": { 
         "exams.exam_id": 96690 
        } 
       }, 
       "aggs":{ 
        "nested_attempts":{ 
         "nested":{ 
          "path": "exams.attempts" 
         }, 
         "aggs":{ 
          "user_attempts":{ 
           "terms":{ 
            "field": "exams.attempts.user_id", 
            "size": 0 
           }, 
           "aggs":{ 
            "max_score":{ 
             "max":{ 
              "field": "exams.attempts.order_score" 
             } 
            } 
           } 
          } 
         } 
        } 
       } 
      } 
     } 
    }   
} 

你好,我有這個聚合查詢。問題是,即使我可以找到每個用戶的max_score,我也無法將聚合分給最大聚合器以查找此最佳分數的日期。Elasticsearch Aggregation Max Value

的嘗試已經user_id說明,order_score,DATE_START

回答

1

另一種方法是不運行max指標子聚集,但top_hits而不是由降max_score排序,所以你可以檢索文檔的date_start

{ 
    "aggs": { 
    "nest_exams": { 
     "nested": { 
     "path": "exams" 
     }, 
     "aggs": { 
     "exams": { 
      "filter": { 
      "term": { 
       "exams.exam_id": 96690 
      } 
      }, 
      "aggs": { 
      "nested_attempts": { 
       "nested": { 
       "path": "exams.attempts" 
       }, 
       "aggs": { 
       "user_attempts": { 
        "terms": { 
        "field": "exams.attempts.user_id", 
        "size": 0 
        }, 
        "aggs": { 
        "max_score": { 
         "top_hits": { 
         "sort": { 
          "exams.attempts.order_score": "desc" 
         }, 
         "size": 1, 
         "_source": [ 
          "date_start" 
         ] 
         } 
        } 
        } 
       } 
       } 
      } 
      } 
     } 
     } 
    } 
    } 
} 
+0

我可以根據date_start對整個結果進行排序嗎? – Michalis