elasticsearch实战三部曲之二:文档操作

《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》点击传送门,即可获取!

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

}

最后

面试题文档来啦,内容很多,485页!

由于笔记的内容太多,没办法全部展示出来,下面只截取部分内容展示。

1111道Java工程师必问面试题

MyBatis 27题 + ZooKeeper 25题 + Dubbo 30题:

Elasticsearch 24 题 +Memcached + Redis 40题:

Spring 26 题+ 微服务 27题+ Linux 45题:

Java面试题合集:

《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》点击传送门,即可获取!
0,

“requests_per_second”: -1,

“throttled_until_millis”: 0,

“failures”: []

}

最后

面试题文档来啦,内容很多,485页!

由于笔记的内容太多,没办法全部展示出来,下面只截取部分内容展示。

1111道Java工程师必问面试题

[外链图片转存中…(img-KM7wUUAJ-1714420663700)]

MyBatis 27题 + ZooKeeper 25题 + Dubbo 30题:

[外链图片转存中…(img-2TsnkJ0Y-1714420663701)]

Elasticsearch 24 题 +Memcached + Redis 40题:

[外链图片转存中…(img-CNFVv8A0-1714420663701)]

Spring 26 题+ 微服务 27题+ Linux 45题:

[外链图片转存中…(img-CS0WhiI9-1714420663701)]

Java面试题合集:

[外链图片转存中…(img-7q19njtw-1714420663701)]

《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》点击传送门,即可获取!

  • 27
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值