ElasticSearch 分組查詢的幾個例子


facets接口可以根據query返回統計數據,其中的 terms_stats 是分組統計,根據key的情況返回value的統計數據,類似group by的意思。

"terms_stats" : { "key_field" : "", "value_field" : "" }

 

例子:查詢每個ip的請求執行時間

查詢語句:

   1: {
   2:     "size": 0,
   3:     "facets": {
   4:         "ips_stats": {
   5:             "terms_stats": {
   6:                 "key_field": "nginx_log.@fields.ip",
   7:                 "value_field": "nginx_log.@fields.request_time",
   8:                 "size": 5
   9:             }
  10:         }
  11:     }
  12: }

說明:

  • 第2行的 size 表示 hits 命中的返回0條;
  • 第3行的facets,第5行的terms_stats 是做分組查詢的必要關鍵字。
  • 第4行的 ips_stats 是對這個分組查詢的命名,可以自己隨便起。
  • 第6行 key_field 表示對 nginx_log.@fields.ip 字段進行分組。
  • 第7行 value_field 表示 對 nginx_log.@fields.request_time 的值進行分組后的運算。
  • 第8行的 size 表示分組運算,最多返回多少行。

這個例子的查詢結果如下,這里簡單起見,只返回了2條。:

   1: {
   2:     "took": 35641,
   3:     "timed_out": false,
   4:     "_shards": {
   5:         "total": 5,
   6:         "successful": 5,
   7:         "failed": 0
   8:     },
   9:     "hits": {
  10:         "total": 193109307,
  11:         "max_score": 1,
  12:         "hits": []
  13:     },
  14:     "facets": {
  15:         "ips_stats": {
  16:             "_type": "terms_stats",
  17:             "missing": 0,
  18:             "terms": [
  19:                 {
  20:                     "term": "180.149.157.110",
  21:                     "count": 1871112,
  22:                     "total_count": 1871112,
  23:                     "min": 0.016,
  24:                     "max": 80.306,
  25:                     "total": 545853.1529999943,
  26:                     "mean": 0.2917266058899704
  27:                 },
  28:                 {
  29:                     "term": "59.49.225.22",
  30:                     "count": 515179,
  31:                     "total_count": 515179,
  32:                     "min": 0,
  33:                     "max": 600.004,
  34:                     "total": 27793.9160000002,
  35:                     "mean": 0.053950017372602924
  36:                 }
  37:             ]
  38:         }
  39:     }
  40: }

從結果我們可以看到,我們對 每個ip的執行時間計算了 個數、最大最小值,平均值,合計的計算。

相關技術參考:http://stackoverflow.com/questions/16549001/elasticsearch-order-responses-and-then-facet/16568770

例子:按照每個ip的請求數排序

查詢JSON

   1: {
   2:     "size": 0,
   3:     "facets": {
   4:         "time_stats": {
   5:             "terms_stats": {
   6:                 "key_field": "nginx_log.@fields.ip",
   7:                 "value_field": "nginx_log.@fields.request_time",
   8:                 "size": 2,
   9:                 "order": "total"
  10:             }
  11:         }
  12:     }
  13: }

只比上述查詢多了一個 order 屬性.輸出結果格式跟上面一樣,只不過是排好序的,就不羅列了.

 

例子:查詢整個網站的執行時間

查詢json:

   1: {
   2:     "query": {
   3:         "match_all": {}
   4:     },
   5:     "size": 0,
   6:     "facets": {
   7:         "stat1": {
   8:             "statistical": {
   9:                 "field": "nginx_log.@fields.request_time"
  10:             }
  11:         }
  12:     }
  13: }

說明:

  • 最初的查詢條件我們沒有寫,意味着取全部,如果你想查詢指定范圍,可以在這里書寫。第2-4行。第5行的size表示查詢條件顯示的數據條數。
  • statistical 是對一個數字字段做統計的facet。

結果:

   1: {
   2:     "took": 4824,
   3:     "timed_out": false,
   4:     "_shards": {
   5:         "total": 5,
   6:         "successful": 5,
   7:         "failed": 0
   8:     },
   9:     "hits": {
  10:         "total": 193109307,
  11:         "max_score": 1,
  12:         "hits": []
  13:     },
  14:     "facets": {
  15:         "stat1": {
  16:             "_type": "statistical",
  17:             "count": 142590544,
  18:             "total": 59320216.00531181,
  19:             "min": 0,
  20:             "max": 5347.085,
  21:             "mean": 0.4160178812790826,
  22:             "sum_of_squares": 14578358539.95768,
  23:             "variance": 102.06623708075713,
  24:             "std_deviation": 10.102783630304923
  25:         }
  26:     }
  27: }

這個方法的更多參考:

http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-facets-statistical-facet.html

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM