2014-02-05 28 views
1

我正在使用elasticsearch db存儲基準數據。如何從ElasticSearch中的一組對象中獲取平均值,最大值,最小值等

我需要計算和檢索一組值的平均值,最大值,最小值等。

我有類似:

Index = "myindex" 

Type = "mytype" 

每個對象有3個字段:

"load_time", "execution_time", "log_time". 

我然後運行基準和db填充了數以百計的記錄。現在,我將檢索每個字段的平均值,最大值和最小值。

我應該運行什麼查詢? 我試圖與統計方面,但我總是得到一個「unreognized令牌例外」 ......

回答

3

您應該使用統計方面(doc

查詢應該是這樣的:

{ 
    "query" : { 
     "match_all" : {} 
    }, 
    "facets" : { 
     "statload_time" : { 
      "statistical" : { 
       "field" : "load_time" 
      } 
     }, 
     "statexec_time" : { 
      "statistical" : { 
       "field" : "execution_time" 
      } 
     }, 
     "statlog_time" : { 
      "statistical" : { 
       "field" : "log_time" 
      } 
     } 
    } 
} 

正如文檔所述:

統計數據包括計數,總數,平方和,平均值 (平均值),最小值,最大值,va riance和標準偏差

索引幾個文件後,查詢的響應是:

{ 
    "took" : 2, 
    "timed_out" : false, 
    "_shards" : { 
    "total" : 5, 
    "successful" : 5, 
    "failed" : 0 
    }, 
    "hits" : { 
    "total" : 2, 
    "max_score" : 1.0, 
    "hits" : [ ] 
    }, 
    "facets" : { 
    "statload_time" : { 
     "_type" : "statistical", 
     "count" : 2, 
     "total" : 7.0, 
     "min" : 2.0, 
     "max" : 5.0, 
     "mean" : 3.5, 
     "sum_of_squares" : 29.0, 
     "variance" : 2.25, 
     "std_deviation" : 1.5 
    }, 
    "statexec_time" : { 
     "_type" : "statistical", 
     "count" : 2, 
     "total" : 12.0, 
     "min" : 5.0, 
     "max" : 7.0, 
     "mean" : 6.0, 
     "sum_of_squares" : 74.0, 
     "variance" : 1.0, 
     "std_deviation" : 1.0 
    }, 
    "statlog_time" : { 
     "_type" : "statistical", 
     "count" : 2, 
     "total" : 16.0, 
     "min" : 7.0, 
     "max" : 9.0, 
     "mean" : 8.0, 
     "sum_of_squares" : 130.0, 
     "variance" : 1.0, 
     "std_deviation" : 1.0 
    } 
    } 
} 
+0

我想這一點,但我得到的錯誤「unreognized令牌例外」,他說, : {「error」:「MapperParsingException [未解析];嵌套:JsonParseException [無法識別的令牌'load_time':期待('真','假'或'空') –

+0

@CarmineGiangregorio你可以發佈你的映射? – moliware

相關問題