插入文档

注意 :

  1. 索引的字段是在上篇文章中建立的.可以按照自己创建的索引来修改对应的文档内容.
  2. 创建文档需要指定文档ID,或者使用ES自带的ID创建.

文档内容

{
    "name":"二滑大魔王登场1",
    "author":"chengyuqiang",
    "cat_id":1,
    "image":"https://blog.qvbilam.xin",
    "url":"https://blog.qvbilam.xin",
    "type":1,
    "content":"二了吧唧的测试",
    "uploader":1,
    "create_time":"1559503461",
    "update_time":"1559503461",
    "status":1,
    "video_id":"123"
}

指定ID创建

错误请求:

PUT http://localhost:8101/small_video/_doc/

错误信息

{
    "error":"Incorrect HTTP method for uri [/small_video/_doc/] and method [PUT], allowed: [POST]",
    "status":405
}

错误原因

  用PUT方式创建文档失败,提示我们用POST方式,在之前介绍RESTUFUL风格API的时候说过.PUT相当于CREATE.POST相当于UPDATE.
  那为什么创建文档用POST会出错呢?
  是因为在ES中创建文档需要指定文档ID或者用ES自带的ID

正确请求

PUT http://localhost:8101/small_video/_doc/1

返回信息

{
    "_index":"small_video",
    "_type":"_doc",
    "_id":"1",
    "_version":1,
    "result":"created",
    "_shards":{
        "total":2,
        "successful":2,
        "failed":0
    },
    "_seq_no":3,
    "_primary_term":3
}

自带ID创建

请求方式

POST http://localhost:8101/small_video/_doc/

返回结果

{
    "_index":"small_video",
    "_type":"_doc",
    "_id":"YtKoGWsB_kz4sr5J5Y-K",
    "_version":1,
    "result":"created",
    "_shards":{
        "total":2,
        "successful":2,
        "failed":0
    },
    "_seq_no":0,
    "_primary_term":1
}

防覆盖创建

请求方式
请求地址后面多加个_create
PUT http://localhost:8101/small_video/_doc/1/_create

存在时返回结果

{
    "error":{
        "root_cause":[
            {
                "type":"version_conflict_engine_exception",
                "reason":"[1]: version conflict, document already exists (current version [2])",
                "index_uuid":"2CRT5UYYQbmvuYXvFetUeg",
                "shard":"4",
                "index":"small_video"
            }
        ],
        "type":"version_conflict_engine_exception",
        "reason":"[1]: version conflict, document already exists (current version [2])",
        "index_uuid":"2CRT5UYYQbmvuYXvFetUeg",
        "shard":"4",
        "index":"small_video"
    },
    "status":409
}

响应结果说明

{
    "_index":"索引名",
    "_type":"类型名",
    "_id":"文档Id",
    "_version":"文档版本",
    "result":"created已创建",
    "_shards":{
        "这个是":"索引操作的复制过程的信息",
        "total":"主分片和副本分片的数量",
        "successful":"成功的分片副本数",
        "failed":"索引相关错误"
    },
    "_seq_no":0,
    "_primary_term":1
}

验证文档

# CURL用HEAD请求要带着-I参数,不然会报错的哦~
curl -I -XHEAD http://127.0.0.1:8101/small_video/_doc/1

存在时返回

HTTP/1.1 200 OK
content-type: application/json; charset=UTF-8
content-length: 177

不存在时返回

HTTP/1.1 404 Not Found
content-type: application/json; charset=UTF-8
content-length: 63

获取文档

请求方式

GET http://localhost:8101/small_video/_doc/1/

返回结果

{
    "_index":"small_video",
    "_type":"_doc",
    "_id":"1",
    "_version":1,
    "_seq_no":3,
    "_primary_term":3,
    "found":true,
    "_source":{
        "name":"二滑大魔王登场1",
        "author":"chengyuqiang",
        "cat_id":1,
        "image":"https://blog.qvbilam.xin",
        "url":"https://blog.qvbilam.xin",
        "type":1,
        "content":"二了吧唧的测试",
        "uploader":1,
        "create_time":"1559503461",
        "update_time":"1559503461",
        "status":1,
        "video_id":"123"
    }
}

文档不存在时返回结果

{
    "_index":"small_video",
    "_type":"_doc",
    "_id":"100",
    "found":false
}

更新文档

菜鸟操作

仅修改content字段

注意:这样操作相当于重新覆值.不建议这样修改

请求方式

PUT http://localhost:8101/small_video/_doc/1/

请求参数

{
    "name":"二滑大魔王登场1",
    "author":"chengyuqiang",
    "cat_id":1,
    "image":"https://blog.qvbilam.xin",
    "url":"https://blog.qvbilam.xin",
    "type":1,
    "content":"二了吧唧的测试啊啊啊",
    "uploader":1,
    "create_time":"1559503461",
    "update_time":"1559503461",
    "status":1,
    "video_id":"123"
}

删除字段

注意:按照下面的传参请求会修改content字段的内容.并且删除出了content字段外的所有字段.

请求方式

PUT http://localhost:8101/small_video/_doc/1/

请求传参

{
  "content": "二了吧唧的测试123"
}

正确方式

  通过脚本更新制定字段,其中ctx是脚本语言中的一个执行对象,先获取_source,再修改字段

通用请求

POST http://localhost:8101/small_video/_doc/1/_update

  1. 下面的增删改操作请求方式不变.只是更改传参.
  2. "为转义"也可以直接使用单引号哈~
修改字段
{
    "script":{
        "source":"ctx._source.content=\"二滑瑟瑟发抖\""
    }
}
添加字段
{
    "script":{
        "source":"ctx._source.angel=\"guoyi\""
    }
}
删除字段
{
    "script":{
        "source":"ctx._source.remove(\"angel\")"
    }
}

删除文档

请求方式

DELETE http://localhost:8101/small_video/_doc/1/

Last modification:February 18th, 2020 at 10:27 pm