elasticsearch 实战三部曲之二:文档操作
读者您看到上述内容,就可以在 postman 中发起 PUT 请求,地址是"test001/article/1"前面加上您的服务器地址,内容是上面的 JSON;
新建文档
在索引 test001 下创建一个文档,类型是 article,id 为 1:
PUT test001/article/1
{
"id":1,
"title":"标题 a",
"posttime":"2019-01-12",
"star":100,
"content":"一起来熟悉文档相关的操作"
}
收到返回码 201,body 内容如下,可见 version 为 1:
{
"_index": "test001",
"_type": "article",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 2,
"failed": 0
},
"_seq_no": 2,
"_primary_term": 3
}
查找文档
根据 id 查找刚刚创建的那一条文档:
GET test001/article/1
收到返回码 200,body 内容如下,索引、类型、id、版本号等全部返回了:
{
"_index": "test001",
"_type": "article",
"_id": "1",
"_version": 1,
"found": true,
"_source": {
"id": 1,
"title": "标题 a",
"posttime": "2019-01-12",
"star": 100,
"content": "一起来熟悉文档相关的操作"
}
}
如果查找的文档不存在,返回码为 400,返回内容如下:
{
"_index": "test001",
"_type": "article",
"_id": "11",
"found": false
}
检查文档是否存在
HEAD test001/article/1
该请求的响应没有 body,只有返回码,存在时返回 200,不存在返回 404
根据 id 一次获取多个文档(_mget 命令)
一次查询三条记录,id 为 1 和 2 的记录真实存在,id 为 999 的记录不存在,请求报文如下:
GET test001/_mget
{
"docs":[
{
"_id":"1"
},
{
"_id":"2"
},
{
"_id":"999"
}
]
}
返回内容如下所示,可见 id 为 999 的记录,found 字段为 false,表示不存在:
{
"docs": [
{
"_index": "test001",
"_type": "article",
"_id": "1",
"_version": 1,
"found": true,
"_source": {
"id": 1,
"title": "标题 a",
"posttime": "2019-01-12",
"star": 100,
"content": "一起来熟悉文档相关的操作"
}
},
{
"_index": "test001",
"_type": "article",
"_id": "2",
"_version": 1,
"found": true,
"_source": {
"id": 2,
"title": "标题 b",
"posttime": "2019-01-13",
"star": 20,
"content": "Ubuntu16 安装 nodejs10"
}
},
{
"_index": "test001",
"_type": null,
"_id": "999",
"found": false
}
]
}
根据 id 一次获取多个文档(元字段_id)
除了使用_mget 命令,还可以通过_search 命令的方式,以元字段"_id"作为搜索条件,一次获取多个文档:
GET test001/_search
{
"query":{
"terms":{"_id":["1", "2"]}
}
}
返回码 200 表示成功,body 是搜索结果:
{
"took": 20,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1,
"hits": [
{
"_index": "test001",
"_type": "article",
"_id": "2",
"_score": 1,
"_source": {
"id": 2,
"title": "标题 b",
"posttime": "2019-01-13",
"content": "elasticsearch 实战三部曲之二"
}
},
{
"_index": "test001",
"_type": "article",
"_id": "1",
"_score": 1,
"_source": {
"id": 1,
"title": "标题 1",
"posttime": "2019-01-13",
"content": "Flink 消费 kafka 消息实战"
}
}
]
}
}
更新文档(doc 方式)
对于 id 为 1 的文档,如果要更新其 title 字段,请求报文如下,根节点名为"doc",可以对指定字段进行替换:
POST test001/article/1/_update
{
"doc":{
"title":"abc"
}
}
更新成功后,返回码 200,返回 body:
{
"_index": "test001",
"_type": "article",
"_id": "1",
"_version": 4,
"result": "updated",
"_shards": {
"total": 2,
"successful": 2,
"failed": 0
},
"_seq_no": 5,
"_primary_term": 3
}
更新文档(脚本方式)
还有一种更新文档的方式是提交一段 elasticsearch 支持的脚本,如下所示,“lang”:"painless"表示脚本语言类型为 painless,params 的内容就是入参,inline 的值就是脚本的内容,表示将 star 字段的值增加 100:
POST test001/article/1/_update
{
"script":{
"inline":"ctx._source.star += params.star",
"lang":"painless",
"params":{
"star":100
}
}
}
执行成功的返回码 200,报文:
{
"_index": "test001",
"_type": "article",
"_id": "1",
"_version": 6,
"result": "updated",
"_shards": {
"total": 2,
"successful": 2,
"failed": 0
},
"_seq_no": 7,
"_primary_term": 3
}
查询更新
前面介绍的更新都是指定 id 的,有的时候我们需要用其他字段查询并更新,例如查找 title 等于"abc"的记录,将其 content 字段更新为"123456":
POST test001/_update_by_query
{
"script":{
"inline":"ctx._source.content = '123456'",
"lang":"painless"
},
"query":{
"term":{"title":"abc"}
}
}
收到返回码 200,body 内容如下:
{
"took": 48,
"timed_out": false,
"total": 1,
"updated": 1,
"deleted": 0,
"batches": 1,
"version_conflicts": 0,
"noops": 0,
"retries": {
"bulk": 0,
"search": 0
},
"throttled_millis": 0,
"requests_per_second": -1,
"throttled_until_millis": 0,
"failures": []
}
评论