前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >elasticsearch之crud

elasticsearch之crud

作者头像
九转成圣
发布2024-04-10 17:15:11
690
发布2024-04-10 17:15:11
举报
文章被收录于专栏:csdncsdn

elasticsearch之crud

索引之crud

新增索引
代码语言:javascript
复制
PUT /book
{
  "mappings":{
    "properties":{
      "name":{"type": "keyword"},
      "info":{"type": "text"},
      "price":{"type": "integer"}
    }
  }
}
修改索引

索引不支持修改现有的,只支持新增

代码语言:javascript
复制
删除索引
代码语言:javascript
复制
DELETE /book
查询所有的索引
代码语言:javascript
复制
health status index                    uuid                   pri rep docs.count docs.deleted store.size pri.store.size
green  open   .kibana_task_manager_1   J_Qp6cL1QGSpNNGSFadkiQ   1   0          2            0      6.6kb          6.6kb
yellow open   book                     Itynp5i7QhmFyMQrKnf9hg   1   1          0            0       230b           230b
green  open   .apm-agent-configuration ce48C5agSqCWP7M3ZP2bEg   1   0          0            0       283b           283b
green  open   .kibana_1                XOg9ADncRKOByv5GAdN2xw   1   0          3            0     15.5kb         15.5kb

文档的crud

新增与覆盖文档

post:带id不存在则新增,存在则覆盖(全量更新),不带id永远新增,且自动生成id

put:必须带id,否则报错,如果存在则覆盖,如果不存在则新增

post带着id不存在则新增

代码语言:javascript
复制
POST /book/_doc/2
{
  "name":"mysql",
  "info":"mysql是最好用的数据库",
  "price":66
}
{
  "_index" : "book",
  "_type" : "_doc",
  "_id" : "2",
  "_version" : 4,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 14,
  "_primary_term" : 3
}
代码语言:javascript
复制
GET /book/_doc/2
{
  "_index" : "book",
  "_type" : "_doc",
  "_id" : "2",
  "_version" : 4,
  "_seq_no" : 14,
  "_primary_term" : 3,
  "found" : true,
  "_source" : {
    "name" : "mysql",
    "info" : "mysql是最好用的数据库",
    "price" : 66
  }
}

post带着id存在,则覆盖

代码语言:javascript
复制
POST /book/_doc/2
{
  "name":"mysql",
  "info":"mysql是最好用的数据库,而且还免费",
  "price":66
}
{
  "_index" : "book",
  "_type" : "_doc",
  "_id" : "2",
  "_version" : 5,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 15,
  "_primary_term" : 3
}
代码语言:javascript
复制
GET /book/_doc/2
{
  "_index" : "book",
  "_type" : "_doc",
  "_id" : "2",
  "_version" : 5,
  "_seq_no" : 15,
  "_primary_term" : 3,
  "found" : true,
  "_source" : {
    "name" : "mysql",
    "info" : "mysql是最好用的数据库,而且还免费"
  }
}

post不带id则新增,且自动生成id

代码语言:javascript
复制
POST /book/_doc
{
  "name":"php",
  "info":"php是最好用的语言",
  "price":66
}
{
  "_index" : "book",
  "_type" : "_doc",
  "_id" : "Zp2064sB7DFkWXEUKAII",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 1,
  "_primary_term" : 3
}

put请求不带id报错,带着id没有就新增,有就覆盖

put不带id报错

代码语言:javascript
复制
PUT /book/_doc
{
  "name":"oracle",
  "info":"oracle是收费的数据库",
  "price":66
}
{
  "error" : "Incorrect HTTP method for uri [/book/_doc?pretty=true] and method [PUT], allowed: [POST]",
  "status" : 405
}

put带着id,不存在则新增

代码语言:javascript
复制
PUT /book/_doc/3
{
  "name":"oracle",
  "info":"oracle是收费的数据库",
  "price":66
}
{
  "_index" : "book",
  "_type" : "_doc",
  "_id" : "3",
  "_version" : 6,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 11,
  "_primary_term" : 3
}
代码语言:javascript
复制
GET /book/_doc/3
{
  "_index" : "book",
  "_type" : "_doc",
  "_id" : "3",
  "_version" : 6,
  "_seq_no" : 11,
  "_primary_term" : 3,
  "found" : true,
  "_source" : {
    "name" : "oracle",
    "info" : "oracle是收费的数据库",
    "price" : 66
  }
}

put带着id存在则覆盖

代码语言:javascript
复制
PUT /book/_doc/3
{
  "name":"oracle",
  "info":"oracle是收费的数据库,所以小企业一般不用"
}
{
  "_index" : "book",
  "_type" : "_doc",
  "_id" : "3",
  "_version" : 7,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 12,
  "_primary_term" : 3
}
代码语言:javascript
复制
GET /book/_doc/3
{
  "_index" : "book",
  "_type" : "_doc",
  "_id" : "3",
  "_version" : 7,
  "_seq_no" : 12,
  "_primary_term" : 3,
  "found" : true,
  "_source" : {
    "name" : "oracle",
    "info" : "oracle是收费的数据库,所以小企业一般不用"
  }
}
强制新增

理解

POST /book/_doc/5可能是新增操作,也可能会是覆盖操作,如果加上_create就明确告诉es,我要新增,如果存在就会报错

代码语言:javascript
复制
POST /book/_doc/5/_create
{
  "name":"kafka",
  "price":66
}
代码语言:javascript
复制
{
  "_index" : "book",
  "_type" : "_doc",
  "_id" : "5",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 18,
  "_primary_term" : 3
}

再次新增时报错

代码语言:javascript
复制
{
  "error" : {
    "root_cause" : [
      {
        "type" : "version_conflict_engine_exception",
        "reason" : "[5]: version conflict, document already exists (current version [1])",
        "index_uuid" : "Itynp5i7QhmFyMQrKnf9hg",
        "shard" : "0",
        "index" : "book"
      }
    ],
    "type" : "version_conflict_engine_exception",
    "reason" : "[5]: version conflict, document already exists (current version [1])",
    "index_uuid" : "Itynp5i7QhmFyMQrKnf9hg",
    "shard" : "0",
    "index" : "book"
  },
  "status" : 409
}
删除文档
代码语言:javascript
复制
DELETE /book/_doc/1
修改文档

语法:POST /{index}/type /{id}/_update

或者POST /{index}/_update/{id}

代码语言:javascript
复制
POST /book/_update/4
{
  "doc": {
     "info":"redis一个内存数据库,目前很流行"
  }
}
{
  "_index" : "book",
  "_type" : "_doc",
  "_id" : "4",
  "_version" : 2,
  "result" : "noop",
  "_shards" : {
    "total" : 0,
    "successful" : 0,
    "failed" : 0
  },
  "_seq_no" : 17,
  "_primary_term" : 3
}
代码语言:javascript
复制
GET /book/_doc/4
{
  "_index" : "book",
  "_type" : "_doc",
  "_id" : "4",
  "_version" : 2,
  "_seq_no" : 17,
  "_primary_term" : 3,
  "found" : true,
  "_source" : {
    "name" : "redis",
    "info" : "redis一个内存数据库,目前很流行",
    "price" : 100
  }
}

更新只支持post请求

代码语言:javascript
复制
PUT /book/_update/4
{
  "doc": {
     "price": 200
  }
}
{
  "error" : "Incorrect HTTP method for uri [/book/_update/4?pretty=true] and method [PUT], allowed: [POST]",
  "status" : 405
}
并发修改文档
代码语言:javascript
复制
PUT /person/_doc/2?if_seq_no=4&if_primary_term=1
{
  "name":"ls2"
}

如果_seq_no不等于4或者_primary_term不等于1就会报错

代码语言:javascript
复制
{
  "error" : {
    "root_cause" : [
      {
        "type" : "version_conflict_engine_exception",
        "reason" : "[2]: version conflict, required seqNo [4], primary term [1]. current document has seqNo [5] and primary term [1]",
        "index_uuid" : "7CucaVfVTi-V5OvzmuXHgA",
        "shard" : "0",
        "index" : "person"
      }
    ],
    "type" : "version_conflict_engine_exception",
    "reason" : "[2]: version conflict, required seqNo [4], primary term [1]. current document has seqNo [5] and primary term [1]",
    "index_uuid" : "7CucaVfVTi-V5OvzmuXHgA",
    "shard" : "0",
    "index" : "person"
  },
  "status" : 409
}
查询文档

根据id查找

代码语言:javascript
复制
GET /book/_doc/2
代码语言:javascript
复制
{
  "_index" : "book",
  "_type" : "_doc",
  "_id" : "2",
  "_version" : 2,
  "_seq_no" : 5,
  "_primary_term" : 3,
  "found" : true,
  "_source" : {
    "name" : "mysql",
    "info" : "mysql是最好用的数据库,而且还免费",
    "price" : 66
  }
}

定制返回字段

代码语言:javascript
复制
GET /book/_doc/4?_source_includes=name,info
{
  "_index" : "book",
  "_type" : "_doc",
  "_id" : "4",
  "_version" : 2,
  "_seq_no" : 17,
  "_primary_term" : 3,
  "found" : true,
  "_source" : {
    "name" : "redis",
    "info" : "redis一个内存数据库,目前很流行"
  }
}
代码语言:javascript
复制
GET /book/_doc/4?_source_excludes=price
{
  "_index" : "book",
  "_type" : "_doc",
  "_id" : "4",
  "_version" : 2,
  "_seq_no" : 17,
  "_primary_term" : 3,
  "found" : true,
  "_source" : {
    "name" : "redis",
    "info" : "redis一个内存数据库,目前很流行"
  }
}

批量查询

代码语言:javascript
复制
GET /_mget
{
  "docs":[
    {"_id":1,"_index":"person"},
    {"_id":2,"_index":"book"}
  ]
}
代码语言:javascript
复制
{
  "docs" : [
    {
      "_index" : "person",
      "_type" : "_doc",
      "_id" : "1",
      "_version" : 2,
      "_seq_no" : 1,
      "_primary_term" : 1,
      "found" : true,
      "_source" : {
        "name" : "张三",
        "age" : 18
      }
    },
    {
      "_index" : "book",
      "_type" : "_doc",
      "_id" : "2",
      "_version" : 5,
      "_seq_no" : 15,
      "_primary_term" : 3,
      "found" : true,
      "_source" : {
        "name" : "mysql",
        "info" : "mysql是最好用的数据库,而且还免费"
      }
    }
  ]
}

同一索引下批量查询

类似与数据库的in效果

代码语言:javascript
复制
GET /book/_mget
{
  "docs":[
    {"_id":2},
    {"_id":4}
  ]
}
代码语言:javascript
复制
{
  "docs" : [
    {
      "_index" : "book",
      "_type" : "_doc",
      "_id" : "2",
      "_version" : 5,
      "_seq_no" : 15,
      "_primary_term" : 3,
      "found" : true,
      "_source" : {
        "name" : "mysql",
        "info" : "mysql是最好用的数据库,而且还免费"
      }
    },
    {
      "_index" : "book",
      "_type" : "_doc",
      "_id" : "4",
      "_version" : 2,
      "_seq_no" : 17,
      "_primary_term" : 3,
      "found" : true,
      "_source" : {
        "name" : "redis",
        "info" : "redis一个内存数据库,目前很流行",
        "price" : 100
      }
    }
  ]
}

搜索写法

代码语言:javascript
复制
GET /book/_search
{
  "query":{
    "ids":{
      "values":[2,4]
    }
  }
}
代码语言:javascript
复制
{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "book",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "name" : "mysql",
          "info" : "mysql是最好用的数据库,而且还免费"
        }
      },
      {
        "_index" : "book",
        "_type" : "_doc",
        "_id" : "4",
        "_score" : 1.0,
        "_source" : {
          "name" : "redis",
          "info" : "redis一个内存数据库,目前很流行",
          "price" : 100
        }
      }
    ]
  }
}

批量操作 bulk

代码语言:javascript
复制
{ "action": { ...metadata... }}
{ "operation": { ...document... }}
{ "action": { ...metadata... }}
{ "operation": { ...document... }}
...
  • action 描述操作的元数据,包括index(索引文档)、delete(删除文档)、update(更新文档)等。
  • operation 包含具体的文档数据。

举例

代码语言:javascript
复制
POST /_bulk
{ "delete": { "_index": "test_index",  "_id": "5" }} 
{ "create": { "_index": "test_index",  "_id": "14" }}
{ "test_field": "test14" }
{ "update": { "_index": "test_index",  "_id": "2"} }
{ "doc" : {"test_field" : "bulk test"} }
代码语言:javascript
复制
{ "delete": { "_index": "test_index",  "_id": "5" }} 
 删除了test_index中id=5的文档
代码语言:javascript
复制
{ "create": { "_index": "test_index",  "_id": "14" }}
{ "test_field": "test14" }
往test_index里新增了一条数据
代码语言:javascript
复制
{ "update": { "_index": "test_index",  "_id": "2"} }
{ "doc" : {"test_field" : "bulk test"} }
更新test_index中id=2的数据

create:相当于强制创建 PUT /index/type/id/_create

update:执行的是局部更新partial update操作

每个操作互不影响。操作失败的行会返回其失败信息

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2024-04-10,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • elasticsearch之crud
    • 索引之crud
      • 新增索引
      • 修改索引
      • 删除索引
      • 查询所有的索引
    • 文档的crud
      • 新增与覆盖文档
      • 强制新增
      • 删除文档
      • 修改文档
      • 并发修改文档
      • 查询文档
相关产品与服务
数据库
云数据库为企业提供了完善的关系型数据库、非关系型数据库、分析型数据库和数据库生态工具。您可以通过产品选择和组合搭建,轻松实现高可靠、高可用性、高性能等数据库需求。云数据库服务也可大幅减少您的运维工作量,更专注于业务发展,让企业一站式享受数据上云及分布式架构的技术红利!
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档