Elasticsearch文档的基本 CRUD 与批量操作

文档的CRUD

操作举例备注
IndexPUT my_index/_doc/1
{“user”:“adosn”,“comment”:“for search”}
如果ID不存在,创建新的文档。否则,先删除现有的文档,再创建新的文档,版本会增加
Create1. PUT my_index/_create/1
{“user”:“adosn”,“comment”:“for search”}
2. POST my_index/_doc
{“user”:“adosn”,“comment”:“for search”}
POST如果不指定ID,自动生成。如果ID已经存在,会失败。
ReadGET my_index/_doc/1读取
UpdatePOST my_index/_update/1
{“doc”: {“user”:“adosn”,“comment”:“for search”}
}
文档必须已经存在,更新只会对相应字段做增量修改
DeleteDELETE my_index/_doc/1删除

TYPE 名,约定都用_doc

create

  • 支持自动生成文档Id和指定文档Id两种方式
  • 通过调用"post users/_doc"
    • 系统自动生成document id
  • 使用HTTP PUT user/create/1创建时,URI中显示指定_create,此时如果该id的文档已经存在,操作失败

举个例子示范一下

// 自动生成方式创建
POST my_index/_doc
{
  "name":"adosn",
  "city":"nanjing",
  "age":"100"
}
// 创建结果
{
  "_index" : "my_index",
  "_type" : "_doc",
  "_id" : "3k_sSW0BMarnKHTX6nI4",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 2,
  "_primary_term" : 1
}
// 指定id方式创建,就指定刚刚已有的id,观察结果
PUT my_index/_create/3k_sSW0BMarnKHTX6nI4
{
  "name":"adosn",
  "city":"nanjing",
  "age":"100"
}
// 创建结果
{
  "error": {
    "root_cause": [
      {
        "type": "version_conflict_engine_exception",
        "reason": "[3k_sSW0BMarnKHTX6nI4]: version conflict, document already exists (current version [1])",
        "index_uuid": "GBXjqcpmSbSaKwdnABqsfw",
        "shard": "0",
        "index": "my_index"
      }
    ],
    "type": "version_conflict_engine_exception",
    "reason": "[3k_sSW0BMarnKHTX6nI4]: version conflict, document already exists (current version [1])",
    "index_uuid": "GBXjqcpmSbSaKwdnABqsfw",
    "shard": "0",
    "index": "my_index"
  },
  "status": 409
}

get

  • 找到文档,返回 200
    • 文档元信息
      • _index/_type/
      • 版本信息,同一个Id的文档,即使被删除,Version号也会不断增加
      • _source中默认包含了文档的所有原始信息
  • 找不到文档,返回404

举个例子示范一下

// GET 获取文档信息
GET my_index/_doc/3k_sSW0BMarnKHTX6nI4
// 请求结果
{
  "_index" : "my_index",
  "_type" : "_doc",
  "_id" : "3k_sSW0BMarnKHTX6nI4",
  "_version" : 1,
  "_seq_no" : 2,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "name" : "adosn",
    "city" : "nanjing",
    "age" : "100"
  }
}

// 随便GET一个
GET my_index/_doc/1111
// 请求结果
{
  "_index" : "my_index",
  "_type" : "_doc",
  "_id" : "1111",
  "found" : false
}

Index

  • Index和create不一样的地方:如果文档不存在,就索引新的文档,否则现有文档会被删除,新的文档被索引。版本信息+1
// 使用index替换原来的文档
PUT my_index/_doc/3k_sSW0BMarnKHTX6nI4
{
  "name":"adosn",
  "city":"nanjing+shanghai",
  "age":"200"
}
// 返回结果
{
  "_index" : "my_index",
  "_type" : "_doc",
  "_id" : "3k_sSW0BMarnKHTX6nI4",
  "_version" : 2,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 3,
  "_primary_term" : 1
}

update

  • update方法不会删除原来的文档,而是实现真正的数据更新
  • Post 方法 / Payload 需要包含在“doc”中
// update创建的文档
POST my_index/_update/3k_sSW0BMarnKHTX6nI4
{
  "doc":{
    "city":"beijing",
    "age":"300"  
  }
}
// 更新结果
{
  "_index" : "my_index",
  "_type" : "_doc",
  "_id" : "3k_sSW0BMarnKHTX6nI4",
  "_version" : 3,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 4,
  "_primary_term" : 1
}

delete

  • delete直接使用DELETE进行删除
// 删除创建的文档
DELETE my_index/_doc/3k_sSW0BMarnKHTX6nI4
// 执行结果
{
  "_index" : "my_index",
  "_type" : "_doc",
  "_id" : "3k_sSW0BMarnKHTX6nI4",
  "_version" : 4,
  "result" : "deleted",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 5,
  "_primary_term" : 1
}

Bulk api

概念

在一次请求时,执行多个不同的操作,减少网络开销

特点

  • 支持在一次API调用中,对不同的索引进行操作
  • 支持四种类型操作
    • Index
    • Create
    • Update
    • Delete
  • 可以在URI中指定index,也可以在请求的Payload中进行
  • 操作中单条操作失败,不会影响其他操作
  • 返回结果包括可每一条操作执行的结果
// Bulk 操作
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" : 118,
  "errors" : false,
  "items" : [
    {
      "index" : {
        "_index" : "test",
        "_type" : "_doc",
        "_id" : "1",
        "_version" : 1,
        "result" : "created",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "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" : 1,
          "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" : 1,
          "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" : 1,
          "failed" : 0
        },
        "_seq_no" : 2,
        "_primary_term" : 1,
        "status" : 200
      }
    }
  ]
}

mget - 批量操作

  • 批量操作,可以减少网络连接产生的开销,提高性能
  • 注意,bulk api和mget批量操作时不要一次携带太多的数据,反而会给es增加压力,造成性能下降
// 执行mget
GET /_mget
{
    "docs" : [
        {
            "_index" : "test",
            "_id" : "1"
        },
        {
            "_index" : "test",
            "_id" : "2"
        }
    ]
}

// 执行结果,test中不存在id为2的数据,第二条数据查询失败
{
  "docs" : [
    {
      "_index" : "test",
      "_type" : "_doc",
      "_id" : "1",
      "_version" : 2,
      "_seq_no" : 2,
      "_primary_term" : 1,
      "found" : true,
      "_source" : {
        "field1" : "value1",
        "field2" : "value2"
      }
    },
    {
      "_index" : "test",
      "_type" : "_doc",
      "_id" : "2",
      "found" : false
    }
  ]
}

常见错误返回和原因

问题原因
无法连接网络故障 / 集群挂了
连接无法关闭网络故障或节点出错
429集群繁忙(需重试请求或者增加节点)
4xx请求体格式有误
500集群内部错误

相关阅读

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值