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 } } ] } } }