elasticsearch中文文档精解二_基础_增删查改批量操作

取回一个文档

restful api 分为 post delete get put ,对应了增删查改。这边整理了下es中基础的restful api增删查改的操作。这个是es比较简单也是比较重要的环节。我个人习惯在postman中操作,但是也给出了CURL的操作dome

1
2
3
4
5
6
7
8
9
10
11
12
13
14
curl -X GET "localhost:9200/phpzjj/article/123?pretty"

{
"_index": "phpzjj",
"_type": "article",
"_id": "1",
"_version": 3,
"found": true,
"_source": {
"title": "es文档写入",
"text": "演示下文档是怎么写入的",
"date": "2018/11/19"
}
}

解读

curl -X GET localhost:9200 /phpzjj /article /123 ?pretty
请求方式 域名:端口 _index _type _id pretty-print功能,美化json

参数列表

名称 作用
pretty 美化json
_source=title,text 返回一部分字段
_source 只返回source内容

postman列子

取回一个文档

文档更新

1
2
3
4
5
6
7
8
curl -X PUT "localhost:9200/phpzjj/article/1" -H 'Content-Type: application/json' -d'
{
"title": "My first blog entry",
"text": "I am starting to get the hang of this...",
"date": "2014/01/02"
}
'

返回

1
2
3
4
5
6
7
8
9
10
11
12
13
{
"_index": "phpzjj",
"_type": "article",
"_id": "1",
"_version": 5, #版本号
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": false
}

解读

curl -X PUT “localhost:9200 /phpzjj /article /1” -H
请求方式 域名:端口 _index _type _id

更新一条数据的过程

  • 从旧文档构建 JSON
  • 更改该 JSON
  • 删除旧文档
  • 索引一个新文档

postman栗子

文档更新

创建文档保证不覆盖

  1. 使用post方式由es自动生成ID
  2. URL末端加上 _create,重复会抛出错误状态码409

postman栗子

创建文档保证不覆盖

删除文档

1
2
curl -X DELETE "localhost:9200/phpzjj/article/101"

返回 状态码200

1
2
3
4
5
6
7
8
9
10
11
12
13
{
"found": true,
"_index": "phpzjj",
"_type": "article",
"_id": "101",
"_version": 2,
"result": "deleted",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
}
}

如果文档不存在返回状态码 404

1
2
3
4
5
6
7
8
9
10
11
12
13
{
"found": false,
"_index": "phpzjj",
"_type": "article",
"_id": "101",
"_version": 1,
"result": "not_found",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
}
}

文档部分更新

_update语句会跟新部分字段,虽然如此但是它在内部实现的时候依旧是删除原有文档再重新建立的过程

1
2
3
4
5
6
7
8
9
curl -X POST "localhost:9200/phpzjj/article/2/_update" -H 'Content-Type: application/json' -d'
{
"doc" : {
"tags" : [ "testing" ],
"views": 0
}
}
'

常用参数

一 .文档中的_source在内部脚本中称为ctx._source

ctx._source.tags+=new_tag

二 .失败后执行次数
retry_on_conflict=5

1
2
3
4
5
6
7
8
9
curl -X POST "localhost:9200/website/pageviews/1/_update?retry_on_conflict=5" -H 'Content-Type: application/json' -d'
{
"script" : "ctx._source.views+=1",
"upsert": {
"views": 0
}
}
'

合并请求,批量取回文档

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
curl -X GET "localhost:9200/_mget" -H 'Content-Type: application/json' -d'
{
"docs" : [
{
"_index" : "phpzjj",
"_type" : "article",
"_id" : 2
},
{
"_index" : "phpzjj",
"_type" : "article",
"_id" : 1,
"_source": "title"
}
]
}
'

postman无法get内带body curl跑一波

_mget

批量操作(需要使用时对照文档完形填空)

1
2
3
4
5
6
7
8
9
curl -X POST "localhost:9200/_bulk" -H 'Content-Type: application/json' -d'
{ "delete": { "_index": "website", "_type": "blog", "_id": "123" }}
{ "create": { "_index": "website", "_type": "blog", "_id": "123" }}
{ "title": "My first blog post" }
{ "index": { "_index": "website", "_type": "blog" }}
{ "title": "My second blog post" }
{ "update": { "_index": "website", "_type": "blog", "_id": "123", "_retry_on_conflict" : 3} }
{ "doc" : {"title" : "My updated blog post"} }
'
  • 版权声明: 本博客所有文章除特别声明外,著作权归作者所有。转载请注明出处!

请我喝杯咖啡吧~