1、创建索引
1.1、直接塞入对象,会自动创建索引
PUT /vvvv/vurx/1{"name":"吴苏环","age":27,"desc":"一顿操作猛如虎,一看工资2500"}
2、插入数据
PUT /vvvv/vurx/1{"name":"吴苏环","age":27,"desc":"一顿操作猛如虎,一看工资2500"}
3、修改数据
//直接覆盖,不推荐!!!PUT /vvvv/vurx/1{"name":"吴苏环update","age":27,"desc":"一顿操作猛如虎,一看工资2500"}
//推荐使用!POST /vvvv/vurx/1/_update{"doc":{"name":"吴苏环学习ES"}}
4、删除数据
4.1、删除索引
4.2、删除doc(数据)
5、查询数据
5.1、结构查询
5.1.1、查询所有索引
GET /_cat/indicesgreen open .kibana_task_manager_1 NK7olWHFTkeLqncUdymgRw 1 0 2 2 20.9kb 20.9kbyellow open jd_goods YN_86pAsQF2vH4YYQYZZrw 1 1 60 0 68.8kb 68.8kbgreen open .apm-agent-configuration SPjU_VMGRhWrj7ZSPuX5qw 1 0 0 0 283b 283byellow open vvvv VwGr9u0dTqa5Fe4WGyiFUQ 1 1 3 0 13kb 13kbyellow open testdb -dun8ufgRGqaQSOhu4N9zA 1 1 2 0 15.6kb 15.6kbyellow open kisia 53Kg-xxnTg697VnSd36lZQ 1 1 8 0 7.5kb 7.5kbgreen open .kibana_1 R3D6HLFMShm5uWlHilFS4Q 1 0 40 9 58.3kb 58.3kbyellow open idea-create IP4ygmRbThOlxCueAo4aEw 1 1 0 0 283b 283b
5.2、简单数据查询
5.2.1、根据id查询数据
5.2.2、根据name查询数据
GET /vvvv/vurx/_search?q=name:吴苏环
5.3、复杂操作数据查询
5.3.1、根据某个字段查询
等同于(where xxx=’’)
GET /vvvv/vurx/_search{"query": {"match": {"name": "环"}}}
5.3.2、结果只展示某些字段
(select a,b from xxx)
GET /vvvv/vurx/_search{"query": {"match": {"name": "环"}},"_source": ["name","desc"]}
5.3.3、根据某个字段排序
(select * from order by age asc)
GET /vvvv/vurx/_search{"query": {"match": {"desc": "一顿"}},"sort": [{"age": {"order": "asc"}}]}
5.3.4、分页
GET /vvvv/vurx/_search{"query": {"match": {"desc": "一顿"}},"sort": [{"age": {"order": "asc"}}],"from": 0, //从第几个数据开始"size": 1 //返回几条数据}
5.3.5、根据多个字段查询
(where xxx=’’ and yyy=’’)
GET /vvvv/_search{"query": {"bool": {"must": [{"match": {"name": "吴苏环"}},{"match": {"age": "27"}}]}}}
5.3.6、满足其中一个字段查询
(where a=’’ or a=’’)
GET /vvvv/_search{"query": {"bool": {"should": [{"match": {"name": "吴苏环"}},{"match": {"desc": "xxx"}}]}}}
5.3.7、范围查询
(where a>’’ and a<’’)
GET /vvvv/_search{"query": {"bool": {"must": [{"match": {"name": "吴苏环"}}],"filter": {"range": {"age": {"gte": 10,"lte": 30}}}}}}
gt:大于 gte:大于等于 lt:小于 lte:小于等于
5.4、聚合查询
5.4.1、简单聚合
5.4.1.1、通过type字段聚合查询
GET /vvvv/_search{"aggs": {"aggByType": {"terms": {"field": "type.keyword","size": 10}}},"size": 0}
5.4.1.2、结果展示
{"took" : 1,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 4,"relation" : "eq"},"max_score" : null,"hits" : [ ]},"aggregations" : {"aggByType" : {"doc_count_error_upper_bound" : 0,"sum_other_doc_count" : 0,"buckets" : [{"key" : "android","doc_count" : 2},{"key" : "PC","doc_count" : 1},{"key" : "ios","doc_count" : 1}]}}}
5.4.2、复合聚合
5.4.2.1、先通过type聚合,再通过age聚合
GET /vvvv/_search{"aggs": {"aggByType": {"terms": {"field": "type.keyword","size": 10},"aggs": {"aggByAge": {"terms": {"field": "age","size": 10}}}}},"size": 0}
5.3.2.2、结果展示
{"took" : 1,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 4,"relation" : "eq"},"max_score" : null,"hits" : [ ]},"aggregations" : {"aggByType" : {"doc_count_error_upper_bound" : 0,"sum_other_doc_count" : 0,"buckets" : [{"key" : "android","doc_count" : 2,"aggByAge" : {"doc_count_error_upper_bound" : 0,"sum_other_doc_count" : 0,"buckets" : [{"key" : 24,"doc_count" : 1},{"key" : 27,"doc_count" : 1}]}},{"key" : "PC","doc_count" : 1,"aggByAge" : {"doc_count_error_upper_bound" : 0,"sum_other_doc_count" : 0,"buckets" : [{"key" : 27,"doc_count" : 1}]}},{"key" : "ios","doc_count" : 1,"aggByAge" : {"doc_count_error_upper_bound" : 0,"sum_other_doc_count" : 0,"buckets" : [{"key" : 27,"doc_count" : 1}]}}]}}}
5.4.3、聚合关键字
5.5、游标查询
在默认情况下,ES查询每次返回的数量最多只有1W条,且只能是前1W条。要想检索更多的数据,只能采用游标查询。
5.5.1、使用步骤
5.5.1.1、步骤一
查询:在原来查询的基础加上 ?scroll=1m
GET /vvvv/_search?scroll=1m{"query": {"match_all": {}},"size": 2}
5.5.1.2、步骤二
结果:
{"_scroll_id" : "DXF1ZXJ5QW5kRmV0Y2gBAAAAAAAkvRoWZjMxMC1uYXpRN3FkZTRLLUxRUWNMQQ==","took" : 0,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 4,"relation" : "eq"},"max_score" : 1.0,"hits" : [{"_index" : "vvvv","_type" : "vurx","_id" : "1","_score" : 1.0,"_source" : {"name" : "吴苏环","age" : 27,"desc" : "一顿操作猛如虎,一看工资2500","type" : "ios"}},{"_index" : "vvvv","_type" : "vurx","_id" : "2","_score" : 1.0,"_source" : {"name" : "吴苏环","age" : 27,"desc" : "一顿操作猛如虎,一看工资2500","type" : "PC"}}]}}
5.5.1.3、步骤三
其实索引里面一共命中了4个文档,但是由于size只返回2个。可以拿着【返回】回来的 scroll_id 查询剩下的数据。
/* 只需要传入返回的游标id即可,查询条件会保持不变 */GET /_search/scroll?{"scroll":"1m","scroll_id":"DXF1ZXJ5QW5kRmV0Y2gBAAAAAAAkwOgWZjMxMC1uYXpRN3FkZTRLLUxRUWNMQQ=="}
5.5.1.4、步骤四
此时会查出剩下的结果
{"_scroll_id" : "DXF1ZXJ5QW5kRmV0Y2gBAAAAAAAkwSMWZjMxMC1uYXpRN3FkZTRLLUxRUWNMQQ==","took" : 1,"timed_out" : false,"terminated_early" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 4,"relation" : "eq"},"max_score" : 1.0,"hits" : [{"_index" : "vvvv","_type" : "vurx","_id" : "4","_score" : 1.0,"_source" : {"name" : "吴苏环","age" : 27,"desc" : "一顿操作猛如虎,一看工资2500","type" : "android"}},{"_index" : "vvvv","_type" : "vurx","_id" : "3","_score" : 1.0,"_source" : {"name" : "吴苏环","age" : "24","desc" : "一顿操作猛如虎,一看工资2500","type" : "android"}}]}}
5.5.1.5、步骤五
重复步骤三四,一直到hits为空即可,如下方
{"_scroll_id" : "DXF1ZXJ5QW5kRmV0Y2gBAAAAAAAkwSMWZjMxMC1uYXpRN3FkZTRLLUxRUWNMQQ==","took" : 1,"timed_out" : false,"terminated_early" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 4,"relation" : "eq"},"max_score" : 1.0,"hits" : [ ]}}
5.5.2、注意事项
5.5.2.1、二次游标查询超过游标存过时间
假设第一次查询
GET /vvvv/_search?scroll=1m{......}
结果由于该语句耗时过长,查询超过1m
{"_scroll_id" : "xxxx==","took" : 1200,......}
再使用返回来的游标id进行二次查询
GET /_search/scroll?{"scroll":"1m","scroll_id":"xxxx=="}
返回如下结果 "status" : 404
{"error" : {"root_cause" : [{"type" : "search_context_missing_exception","reason" : "No search context found for id [2407706]"}],"type" : "search_phase_execution_exception","reason" : "all shards failed","phase" : "query","grouped" : true,"failed_shards" : [{"shard" : -1,"index" : null,"reason" : {"type" : "search_context_missing_exception","reason" : "No search context found for id [2407706]"}}],"caused_by" : {"type" : "search_context_missing_exception","reason" : "No search context found for id [2407706]"}},"status" : 404}
