1. 文档的CRUD操作
1.1 Create
- 支持自动生成文档ID和指定文档ID两种方式
- 通过调用“post /users/_doc”,系统自动生成document ID
//create document,自动生成_ID
POST users/_doc
{
"user": "Mike",
"post_date": "2021-04-8",
"message": "trying out kibana"
}
- 使用“PUT user/_create/1”创建时,URI中显示指定_create,此时如果该id文档已经存在,则操作失败
//create document,指定ID,如果ID存在,报错
PUT users/_doc/1?op_type-create
{
"user": "Jack",
"post_date": "2021-04-8",
"message": "trying out ElasticSearch"
}
1.2 Get
“GET movies”
- 找到文档,返回200
文档元信息
- _index/_type/
- 版本信息,同一个ID文档,即使被删除,version也会不断增加
- _source中默认包含了文档的所有原始信息
- 找不到文档,返回400
- index和Create不一样的地方
- 如果文档不存在,就索引新的文档,否则现有文档会被删除,新的文档被索引,version+1
1.3 Update
- Update不会删除原来文档,而是实现真正的数据更新
2. 批量操作
2.1 Bulk API
- 支持在一次API调用中,对不同对索引进行操作
- 支持四种类型操作
- Index
- Create
- Update
- Delete - 可以在URI中指定Index,也可以在请求对PayLoad中进行
- 操作中单挑操作失败,不会影响其他操作
- 返回结果包括了每一条操作执行对结果
// Bulk操作第一次执行
POST _bulk
{"index":{"_index":"test","_id": "1"}}
{"field1":"value3"}
{"delete":{"_index":"test","_id":2}}
{"create":{"_index":"test2","_id":"3"}}
{"field1":"value3"}
{"update":{"_id":"1","_index":"test"}}
{"doc":{"field2":"value2"}}
2.2 mget(批量读取)
- 批量操作,可以减少网络连接所产生的开销,提高性能
GET _mget
{
"docs":[
{
"_index" : "user",
"_id" : 1
},
{
"_index" : "comment",
"_id" : 1
}
]
}
2.3 msearch(批量查询)
POST kibana_sample_data_ecommerce/_msearch
{}
{"query" : {"match_all" : {}}, "size":1}
{"index" : "kibana_sample_data_flights"}
{"query" : {"match_all": {}}, "size" : 2}