elasticsearch实战三部曲之二:文档操作,mybatis自动映射原理

  1. elasticsearch-head安装在一号机器,访问地址:http://192.168.119.152:9100

  2. 已经建立了索引test001;

数据格式说明

为了便于和读者沟通,我们来约定一下如何在文章中表达请求和响应的信息:

  1. 假设通过Postman工具向服务器发送一个PUT类型的请求,地址是:http://192.168.119.152:9200/test001/article/1

  2. 请求的内容是JSON格式的,内容如下:

{

“id”:1,

“title”:“标题a”,

“posttime”:“2019-01-12”,

“content”:“一起来熟悉文档相关的操作”

}

对于上面的请求,我在文章中就以如下格式描述:

PUT test001/article/1

{

“id”:1,

“title”:“标题a”,

“posttime”:“2019-01-12”,

“content”:“一起来熟悉文档相关的操作”

}

读者您看到上述内容,就可以在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

}

更新文档(脚本方式)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值