基本crud

基本curd

post请求

post请求不带id,会自动生成id

POST st_test_2021_01_25/_doc
{
  "user" : "zhangsan",
  "post_data" : "2021-01-25",
  "message" : "zhangsan try to create"
}

post请求在文档上增加字段,不会先删除后创建,是在原来的document上增加

POST st_test_2021_01_25/_doc/1/_update
{
  "doc":{
      "age" : 12
  }
}

执行结果,会发现这里的user、post_data、message等属性没有发生变化,属性值保留

{
  "_index" : "st_test_2021_01_25",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 4,
  "_seq_no" : 4,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "user" : "st1",
    "post_data" : "2021-01-25",
    "message" : "st1 try to create",
    "age" : 12
  }
}

put请求

put请求,指定id,如果id已经存在了,会报错
创建形式1

PUT st_test_2021_01_25/_doc/1?op_type=create
{
  "user" : "st",
  "post_data" : "2021-01-25",
  "message" : "try to create"
}

创建形式2

PUT st_test_2021_01_25/_doc/_create
{
  "user" : "lisi",
  "post_data" : "2021-01-25",
  "message" : "lisi try to create"
}

结果

{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 3,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "st_test_2021_01_25",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "user" : "st",
          "post_data" : "2021-01-25",
          "message" : "try to create"
        }
      },
      {
        "_index" : "st_test_2021_01_25",
        "_type" : "_doc",
        "_id" : "qTg5OHcBrEuQ-ZBavdCx",
        "_score" : 1.0,
        "_source" : {
          "user" : "zhangsan",
          "post_data" : "2021-01-25",
          "message" : "zhangsan try to create"
        }
      },
      {
        "_index" : "st_test_2021_01_25",
        "_type" : "_doc",
        "_id" : "_create",
        "_score" : 1.0,
        "_source" : {
          "user" : "lisi",
          "post_data" : "2021-01-25",
          "message" : "lisi try to create"
        }
      }
    ]
  }
}

put 请求指定id,会进行先删除,再重新创建的操作

PUT st_test_2021_01_25/_doc/1
{
  "user" : "st1",
  "post_data" : "2021-01-25",
  "message" : "st1 try to create"
}

执行结果

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

get请求

GET st_test_2021_01_25/_doc/1

delete请求

DELETE st_test_2021_01_25/_doc/qTg5OHcBrEuQ-ZBavdCx
自律人的才是可怕的人
原文地址:https://www.cnblogs.com/lovelifest/p/14325296.html