Elasticsearch核心技术与实战学习笔记 第三章 11 | 文档的基本CRUD与批量操作

一 序

   本文属于极客时间Elasticsearch核心技术与实战学习笔记系列。

   本节课学习文档的基本CRUD操作。

二 文档CRUD

create 

支持自动生成文档 Id 和指定文档 Id 两种方法.

1 自动生成文档 Id

#create document. 自动生成 _id
POST users/_doc
{
	"user" : "Mike",
    "post_date" : "2019-04-15T14:12:12",
    "message" : "trying out Kibana"
}

返回:

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

#create document. 指定Id。如果id已经存在,报错
PUT users/_doc/1?op_type=create
{
    "user" : "Jack",
    "post_date" : "2019-05-15T14:12:12",
    "message" : "trying out Elasticsearch"
}

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

存在是提示:

{
  "error": {
    "root_cause": [
      {
        "type": "version_conflict_engine_exception",
        "reason": "[1]: version conflict, document already exists (current version [1])",
        "index_uuid": "fqVg1Y6NQHurMFOXu4fjQw",
        "shard": "0",
        "index": "users"
      }
    ],
    "type": "version_conflict_engine_exception",
    "reason": "[1]: version conflict, document already exists (current version [1])",
    "index_uuid": "fqVg1Y6NQHurMFOXu4fjQw",
    "shard": "0",
    "index": "users"
  },
  "status": 409
}

2、Get 一个文档


1)找到文档,返回 HTTP 200

GET users/_doc/1 

其中,文档元信息

  • _index/_type/,_type 在版本7中只有 _doc 类型
  • _version 版本信息,同一个 Id 的文档被删除了,版本号也会增加
  • _source 中默认包含文档的原始信息

2)没找到文档,返回HTTP404

3 Index 文档

Index 也是用于创建文档的方法,和 Create 不同有一些不同,如果文档不存在情况,直接创建新文档,否者删除原来的文档,新文档被索引,_version 版本加一。

   PUT users/_doc/1
{
    "user" : "Mike"
}

 

3)index 操作后查看数据

、Update 文档

Update 方法不会删除原来文档,而是实现真正的数据更新
Post方法/Payload需要包含在 “doc”中

post结果:

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

再次执行查询,多了两个字段

5、Delete 文档
1)通过id删除文档
2)请求中要包含在 “doc”中

# 删除文档
DELETE users/_doc/1

三、批量操作

脚本:

#执行第1次
POST _bulk
{ "index" : { "_index" : "test", "_id" : "1" } }
{ "field1" : "value1" }
{ "delete" : { "_index" : "test", "_id" : "2" } }
{ "create" : { "_index" : "test2", "_id" : "3" } }
{ "field1" : "value3" }
{ "update" : {"_id" : "1", "_index" : "test"} }
{ "doc" : {"field2" : "value2"} }
{
  "took" : 3665,
  "errors" : false,
  "items" : [
    {
      "index" : {
        "_index" : "test",
        "_type" : "_doc",
        "_id" : "1",
        "_version" : 1,
        "result" : "created",
        "_shards" : {
          "total" : 2,
          "successful" : 2,
          "failed" : 0
        },
        "_seq_no" : 0,
        "_primary_term" : 1,
        "status" : 201
      }
    },
    {
      "delete" : {
        "_index" : "test",
        "_type" : "_doc",
        "_id" : "2",
        "_version" : 1,
        "result" : "not_found",
        "_shards" : {
          "total" : 2,
          "successful" : 2,
          "failed" : 0
        },
        "_seq_no" : 1,
        "_primary_term" : 1,
        "status" : 404
      }
    },
    {
      "create" : {
        "_index" : "test2",
        "_type" : "_doc",
        "_id" : "3",
        "_version" : 1,
        "result" : "created",
        "_shards" : {
          "total" : 2,
          "successful" : 2,
          "failed" : 0
        },
        "_seq_no" : 0,
        "_primary_term" : 1,
        "status" : 201
      }
    },
    {
      "update" : {
        "_index" : "test",
        "_type" : "_doc",
        "_id" : "1",
        "_version" : 2,
        "result" : "updated",
        "_shards" : {
          "total" : 2,
          "successful" : 2,
          "failed" : 0
        },
        "_seq_no" : 2,
        "_primary_term" : 1,
        "status" : 200
      }
    }
  ]
}

2)批量读取-mget
批量操作,可以减少网络连接开销,提高性能
可以在docs中指定index,也可以在URL路径中指定index,

### mget 操作
GET /_mget
{
    "docs" : [
        {
            "_index" : "test",
            "_id" : "1"
        },
        {
            "_index" : "test",
            "_id" : "2"
        }
    ]
}

   

#URI中指定index
GET /test/_mget
{
    "docs" : [
        {

            "_id" : "1"
        },
        {

            "_id" : "2"
        }
    ]
}

3)批量查询-msearch
多个查询匹配条件组合

### msearch 操作
POST kibana_sample_data_ecommerce/_msearch
{}
{"query" : {"match_all" : {}},"size":1}
{"index" : "kibana_sample_data_flights"}
{"query" : {"match_all" : {}},"size":2}

常见错误

 

猜你喜欢

转载自blog.csdn.net/bohu83/article/details/106065217