elasticsearch简单使用
一、索引与文档
//查看索引相关信息 GET movies //查看索引文档总数 GET movies/_count
二、集群与分片
1,查看集群的健康状况 GET _cluster/health
green - 主分片副本都正常分配
yellow - 主分片全部正常分配,有副本分片未能正常分配
red - 有主分片未能分配(例如,当服务器的磁盘容量超过85%时。去创建了一个新的索引)
2,查询节点: GET _cat/nodes
3,查询分片: GET _cat/shards
三、CRUD与批量操作
1,crud
index: 针对整个文档,既可以新增又可以更新;
create:只是新增操作,已有报错,可以用PUT指定ID,或POST不指定ID;
update:指的是部分更新,官方只是说用POST,请求body里用script或 doc里包含文档要更新的部分;
delete和read:就是delete和get请求了,比较简单。
//创建文档,自动生成 _id POST user/_doc { "user":"hunter", "post_date":"2020-04-06T20:35:20" } //创建文档,指定id,如果已存在报错,不存在修改 PUT user/_doc/2?op_type=create { "user":"hunter", "post_date":"2020-04-06T20:35:20" } PUT user/_create/3 { "user":"hunter", "post_date":"2020-04-06T20:35:20" } //查询_id=1的文档 GET user/_doc/3 //删除指定的文档 DELETE user/_doc/3
//删除
POST employees/_delete_by_query
{
"track_total_hits": true,
"query": {
"bool": {
"filter": [
{"terms": {
"name.keyword": [
"李四"
]
}}
]
}
}
}
//批量修改
POST project_demo/_update_by_query
{
"script":{
"source":"ctx._source['is_deleted']=2"
},
"query": {
"bool": {
"filter": {
"term": {
"system_tag": "2"
}
}
}
}
}
//在原文档上增加字段 POST user/_update/3 { "doc":{ "age":"20", "post_date":"2020-04-06T20:35:21" } }
POST employees/_update/DOBxx3MBH3d_nFQ4zJ-J
{
"doc":{
"name":"张飞"
}
}
2,批量操作
①一次api调用,对不同的索引进行操作
//新增文档、修改文档值、删除文档 POST _bulk { "index":{"_index":"test","_id":"1"}} {"field1":"value1"} { "update":{"_index":"test","_id":"1"}} {"doc":{ "field1":"value2"}} {"delete":{"_index":"test","_id":"1"}}
post _bulk
{"create":{"_index":"employees"}}
{"name":"李四","age":20,"mobile":"1300000000"}
POST employees/_bulk
{"index":{}}
{"name":"王五","age":20,"mobile":"1300000000"}
②批量读取
GET _mget { "docs":[ { "_index":"user", "_id":"3" }, { "_index":"movies", "_id":"3" } ] }
③批量查询
GET movies/_msearch {} {"query":{"match_all":{}},"size":1}} {"index":"user"} {"query":{"match_all":{}},"size":1}}
四、常见错误返回