ES是個分佈式的文檔存儲系統。經過JSON序列化來存儲複雜的數據信息。在集羣環境中,你可以馬上從集羣的任意節點獲取到你想要的數據。html
當你去檢索一個被索引的document
,它能在1s內給你響應。數據庫
存儲類型 | |||
---|---|---|---|
ES | index | document | indices(index集合) |
MYSQL | DB | tables | 數據庫實例 |
經過ES提供的restful接口去請求獲取到相關的數據信息。也能夠經過DSL 查詢來作複雜的聚合查詢獲得結果。json
ES的聚合查詢不單單是幫助你大海撈針
,它還會告訴你其餘的信息api
這是怎麼工做的?實際上,Elasticsearch索引只是一個或多個物理分片(shard)的邏輯分組,其中每一個碎片其實是一個獨立的索引。經過將索引中的文檔分佈在多個碎片上,並將這些分片(shard)分佈在多個節點上,Elasticsearch能夠確保冗餘,這既能夠防止硬件故障,又能夠在節點添加到集羣時增長查詢容量。當集羣增加(或收縮)時,Elasticsearch會自動遷移分片(shard)以從新平衡集羣。restful
分片(shard)有兩種類型:原始分片(shard)和複製分片(shard)。索引中的每一個文檔都屬於一個主分片。副本碎片是主碎片的副本。副本提供數據的冗餘副本,以防止硬件故障,並增長服務讀請求(如搜索或檢索文檔)的容量。app
http://192.168.243.8:9200/sentinel/sentinel_metrics/_search?pretty { "took" : 2, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 167, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "sentinel", "_type" : "sentinel_metrics", "_id" : "1588222865023", "_score" : 1.0, "_source" : { "id" : 1588222865023, "gmtCreate" : "2020-04-30T05:01:04.889Z", "gmtModified" : "2020-04-30T05:01:04.889Z", "app" : "service-mall-user", "timestamp" : "2020-04-30T05:00:59.000Z", "resource" : "cn.com.service.emp.api.EmpDubboService", "passQps" : 1, "successQps" : 1, "blockQps" : 0, "exceptionQps" : 0, "rt" : 126.0, "count" : 1, "resourceCode" : -1860573111 } }, ... ]
返回信息的具體含義:elasticsearch
took
– 查詢話費的時間timed_out
– 查詢是否超時_shards
– 搜索了多少碎片,並對成功、失敗或跳過的碎片進行了細分。max_score
– 找到的最相關文檔的分數hits.total.value
- 匹配的document數hits.sort
- 文檔的排序位置(不按相關性分數排序時)hits._score
- 文檔的相關性分數(使用「所有匹配」時不適用)查詢10-19的數據信息分佈式
http://192.168.243.8:9200/sentinel/sentinel_metrics/_search?pretty { "query": { "match_all": {} }, "sort": [ { "gmtCreate": "asc" } ], "from": 10, "size": 10 }
查詢document = sentinel_metrics而且字段app=service-mall-user全部字段信息ide
http://192.168.243.8:9200/sentinel/sentinel_metrics/_search?pretty { "query": { "match": { "app":"service-mall-user" } }, "sort": [ { "gmtCreate": "asc" } ], "from": 10, "size": 10 }
查詢document = sentinel_metrics而且字段app like %service-mall-user%全部字段信息ui
http://192.168.243.8:9200/sentinel/sentinel_metrics/_search?pretty { "query": { "match_phrase": { "app":"service" } }, "sort": [ { "gmtCreate": "asc" } ], "from": 10, "size": 10 }
要構造更復雜的查詢,可使用
bool
查詢組合多個查詢條件。您能夠根據須要(must)、須要(must_not)或不須要(必須不匹配)指定條件。
匹配
"app":"service-mall-user"
而且不是rt < 0
的記錄信息
http://192.168.243.8:9200/sentinel/sentinel_metrics/_search?pretty { "bool": { "must":[ { "match":{ "app":"service-mall-user" } } ], "most_not":[ { "range":{ "rt":{ "lt": 0 } } } ] } }
http://192.168.243.8:9200/sentinel/sentinel_metrics/_search?pretty { "bool": { "must":[ { "match":{ "app":"service-mall-user" } } ], "filter":[ { "range":{ "passQps":{ "gte": 1, "lt": 10 } } } ] } "from": 10, "size": 10 }
根據字段
app
聚合查詢,返回十個結果信息 ;group_by_app
group_by_字段
http://192.168.240.10:9200/sentinel/sentinel_metric/_search { "size": 10, "aggs": { "group_by_app": { "terms": { "field": "app.keyword" } } } }
聚合結果再聚合
{ "size": 0, "aggs": { "group_by_app": { "terms": { "field": "app.keyword" }, "aggs": { "average_rt":{ "avg":{ "field":"rt" } } } } } } 結果 { "took": 922, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 33, "max_score": 0.0, "hits": [] }, "aggregations": { "group_by_app": { "doc_count_error_upper_bound": 0, "sum_other_doc_count": 0, "buckets": [ { "key": "service-mall-user", "doc_count": 33, "average_rt": { "value": 22.87878787878788 } } ] } } }