写点什么

elasticsearch 实战三部曲之二:文档操作,java 基础填空题

用户头像
极客good
关注
发布于: 刚刚

"_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 字段的值增加


【一线大厂Java面试题解析+核心总结学习笔记+最新架构讲解视频+实战项目源码讲义】
浏览器打开:qq.cn.hn/FTf 免费领取
复制代码


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": []


}

删除文档(指定 ID)

DELETE test001/article/2


删除成功返回码 200,body 如下:


{


"_index": "test001",


"_type": "article",


"_id": "2",


"_version": 2,


"result": "deleted",


"_shards": {


"total": 2,


"successful": 2,


"failed": 0


},


"_seq_no": 3,


"_primary_term": 2


}

删除文档(带查询条件)

如果想删除 title 字段等于"abc"的文档:


DELETE test001


{


"query":{


"term":{"title":"abc"}


}


}


删除成功返回码 200,body 如下:


{


"acknowledged": true


}


同样的操作再试一次,就会返回 404 错误,因为记录已经不存在了;

批量操作

要新增多个文档,可以将内容写入 json 文件,再通过批量操作的接口,将数据一次性 POST;


  1. 首先创建一个名为 book.json 的文件,内容如下:

用户头像

极客good

关注

还未添加个人签名 2021.03.18 加入

还未添加个人简介

评论

发布
暂无评论
elasticsearch实战三部曲之二:文档操作,java基础填空题