3.文档的基本CRUD与批量操作

本文详细介绍了Elasticsearch中如何进行文档的创建、获取、索引、更新操作,以及Bulk API的使用,包括批量读取、批量搜索、删除索引等,强调了批量操作对性能提升的重要性,并提及了倒排索引的概念。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.文档的CRUD

在这里插入图片描述

  • Type名,约定都用_doc
  • Create 如果ID已经存在,会失败
  • Index 如果ID不存在,创建新的文档,否则,先删除现有的文档,再创建新的文档,版本会增加
  • Update 文档必须已经存在,更新只会对相应字段做增量修改

1.1 CREATE 一个文档

支持自动生成文档ID 和 指定文档ID 两种方式

自动生成文档ID

通过调用”post /users/_doc 系统会自动生成document ID

POST users/_doc
{
  "user":"Mike",
    "post_date":"2019-07-10",
    "message":"trying out kibana"
}

运行成功结果

{
  "_index" : "users",
  "_type" : "_doc",
  "_id" : "vJXKIWwBd9mJYCU0J9hg", // 自动生成的文档ID
  "_version" : 1, 
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "failed" : 0
  },
  "_seq_no" : 7,
  "_primary_term" : 2
}

指定文档ID

使用HTTP PUT user/_create/1创建时,URI中显示指定 _create ,此时如果该id的文档已经存在,操作失败

	PUT users/_create/1
	{
		"firstName":"jack",
		"lastName":"Johnson",
		"tags":["guiar","skateboard"]
	}
第一次运行 运行成功
		{
 			 "_index" : "users",
 			 "_type" : "_doc",
  			 "_id" : "3",
  			 "_version" : 1,
  			 "result" : "created",
 			 "_shards" : {
   			 "total" : 2,
   			 "successful" : 2,
   			 "failed" : 0
 		 },
 			 "_seq_no" : 4,
 			 "_primary_term" : 2
	}
	第二次运行相同的命令 出现错误
		{
	  "error": {
	    "root_cause": [
	      {
	        "type": "version_conflict_engine_exception",
	        "reason": "[3]: version conflict, document already exists (current version [1])",
	        "index_uuid": "90f6Tjm4RbiErbHWuSDKEw",
	        "shard": "0",
	        "index": "users"
	      }
	    ],
	    "type": "version_conflict_engine_exception",
	    "reason": "[3]: version conflict, document already exists (current version [1])",
	    "index_uuid": "90f6Tjm4RbiErbHWuSDKEw",
	    "shard": "0",
	    "index": "users"
	  },
	  "status": 409
	}

1.2 GET 一个文档

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


		GET users/_doc/2
		查询结果
		{
		  "_index" : "users",
		  "_type" : "_doc",
		  "_id" : "2",
		  "_version" : 1,
		  "_seq_no" : 1,
		  "_primary_term" : 1,
		  "found" : true,
		  "_source" : {
		    "name" : "twobird",
		    "interests" : [
		      "reading",
		      "music"
		    ]
		  }
		}

1.3 Index 文档

Index和Create不一样的地方:如果文档不存在,就索引新的文档。否则现有文档会被删除,新的文档被索引。版本信息 +1

	1. 索引新的文档
	PUT users/_doc/11
	{
		"user":"Mack"
	}
	GET users/_doc/11
	// 查询结果为如下
	{
	  "_index" : "users",
	  "_type" : "_doc",
	  "_id" : "11",
	  "_version" : 1,
	  "_seq_no" : 10,
	  "_primary_term" : 2,
	  "found" : true,
	  "_source" : {
	    "user" : "Mack"
	  }
	}
	
2.索引已经存在id的文档
2.1首先查看 id=1是否已存在,如下输出结果已存在
GET users/_doc/1
		{
	  "_index" : "users",
	  "_type" : "_doc",
	  "_id" : "1",
	  "_version" : 1,
	  "_seq_no" : 2,
	  "_primary_term" : 1,
	  "found" : true,
	  "_source" : {
	    "name" : "twobird",
	    "interests" : [
	      "reading",
	      "music"
	    ]
	  }
	}
2.2在ID=1的文档上索引新的文档
PUT uses/_doc/1
	{
	"user":"Mark"
	}
2.3 查看id=1 的文档的记录 课件source改变 版本号也已经改变
GET users/_doc/1
{
  "_index" : "users",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 2,
  "_seq_no" : 9,
  "_primary_term" : 2,
  "found" : true,
  "_source" : {
    "user" : "Mack"
  }
}

1.4 Update 文档

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

POST users/_update/1
{
	"doc":{
		"post_date":"2019-07-24",
		"message":"this is update"
		}
}
GET users/_doc/1
查看输出的结果
{
  "_index" : "users",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 2,
  "_seq_no" : 11,
  "_primary_term" : 2,
  "found" : true,
  "_source" : {
    "user" : "Mack",
    "post_date" : "2019-09-10",
    "message" : "this is update"
  }
}

2 Bulk API

  • 支持在一次API调用中,对不同的索引进行操作

  • 支持四种类型操作

    • Index
    • Create
    • Update
    • Delete
  • 可以再URI中指定Index,也可以在请求的Payload中进行

  • 操作中单条操作失败,并不会影响其他操作

  • 返回结果包括了每一条操作执行的结果

      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" : 1803,
    "errors" : false,
    "items" : [
      {
        "index" : {       					//索引的返回结果,索引成功
          "_index" : "test",
          "_type" : "_doc",
          "_id" : "1",
          "_version" : 1,
          "result" : "created",
          "_shards" : {
            "total" : 3,
            "successful" : 2,
            "failed" : 0
          },
          "_seq_no" : 0,
          "_primary_term" : 1,
          "status" : 201
        }
      },
      {
        "delete" : {
          "_index" : "test",
          "_type" : "_doc",
          "_id" : "2",
          "_version" : 1,
          "result" : "not_found",					//delete返回结果 没有找到 id=2的文档
          "_shards" : {
            "total" : 3,
            "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" : 3,
            "successful" : 2,
            "failed" : 0
          },
          "_seq_no" : 0,
          "_primary_term" : 1,
          "status" : 201
        }
      },
      {
        "update" : {					//update test/_doc/1  成功
          "_index" : "test",
          "_type" : "_doc",
          "_id" : "1",
          "_version" : 2,
          "result" : "updated",
          "_shards" : {
            "total" : 3,
            "successful" : 2,
            "failed" : 0
          },
          "_seq_no" : 2,
          "_primary_term" : 1,
          "status" : 200
        }
      }
    ]
      }
    

2.1 批量读取 mget

批量操作,可以减少网络连接所产生的开销,提高性能

GET /_mget
{
    "docs" : [
        {
            "_index" : "test",
            "_id" : "1"
        },
        {
            "_index" : "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
    }
  ]
}

Demo2

GET test/_mget  		//可以直接在URI中指定index
{
    "docs" : [
        {
            "_id" : "1",
            "_source" : ["field1","field2"]    //指定查看的字段
        },
        {
            "_id" : "1",
            "_source" : {
                "include": ["field1"],   //过滤 指定要显示的字段
                "exclude": ["field2"]   // 过滤 指定不要显示的字段
            }
        }
    ]
}

2.2 批量搜索 msearch

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

2.3 删除索引

delete test
delete test2
delete test1

2.4 常见错误返回

在这里插入图片描述

3 倒排索引

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
可以参考
Elasticsearch中的倒排索引

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值