Elasticsearch 更新索引

2020-09-17  本文已影响0人  觉释

主要测试更新索引数据变化,采用动态建索引方式

添加一条索引记录

POST my_index_update/_doc/1
{
  "username" : "zhangsan",
  "age" : 30
}

返回信息 _version=1

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

查询索引信息 GET my_index_update/_search

 GET my_index_update/_search

返回数据

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "my_index_update",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "username" : "zhangsan",
          "age" : 30
        }
      }
    ]
  }
}

更新索引


PUT my_index_update/_doc/1
{
  "username" : "lisi",
  "age" : 20
}

返回数据 _version=2

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

再次查询索引信息 GET my_index_update/_search

 GET my_index_update/_search

返回数据

 {
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "my_index_update",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "username" : "lisi",
          "age" : 20
        }
      }
    ]
  }
}

说明:经过测试 POST 的时候如果不指定ID ,则会自动为我们生成ID,如果用PUT 必须指定索引ID,其他并无明显区别,按照规范来讲POST 一般为提交数据,PUT 为更新数据

上一篇 下一篇

猜你喜欢

热点阅读