四、ElasticSearch 8.6.0 快速使用 - 文档 API 的基本操作

作者: 温新

分类: 【Elasticsearch】

阅读: 1313

时间: 2023-02-22 07:48:25

hi,我是温新,一名 PHPer

ES 版本:ElasticSearch 8.6.0

系统版本:Rocky Linux 9.1

学习目标:掌握文档的 CURD 操作

本篇文章结合官方文档编写及参考网络资料编写,虽非全部原创,但也是结合了自己的理解,若转载请附带本文 URL,编写不易,持续编写更不易,谢谢!

文档操作类型

enum OpType {
   INDEX(0),
   CREATE(1)
}
  • index 索引,既可以是创建,也可以是全量替换;
  • create 不存在则创建,存在则报错。

当操作的对象为数据流时,op_type 必须为 create

创建文档(指定 ID)

若没有 op_type=create 时,PUT 添加文档时,若文档存在,则会被覆盖;若有该参数时,添加的文档已经存在时,则会报错。

语法:

PUT /<index_name>/_doc/<_id>?op_type=create

案例:创建一条 _id 为 1 的文档,并有 name 和 age 两个字段

PUT ziruchu/_doc/1?op_type=create
{
  "name": "张无忌",
  "age": 19
}

返回值:

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

创建文档(不指定 ID)

语法:

POST ziruchu/_doc

案例

POST ziruchu/_doc
{
  "name": "周芷若",
  "age": 18
}	

返回值:

{
  "_index": "ziruchu",
  "_id": "SA1jdIYBDc1n1ijvC4sQ",
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 4,
  "_primary_term": 1
}

使用此方法添加时,不会判断记录是否存在,可以这样理解,就是以追加的形式不断的添加下去;

不指定 ID 时会自动生成一个唯一 ID

修改文档

语法:

# 方式一
PUT /<index_name>/_doc/<_id>?op_type=index
{
  ...
}
# 方式二:简写
PUT /<index_name>/_doc/<_id>	
{
  ...
}

案例:

PUT ziruchu/_doc/1
{
  "name": "张翠山",
  "age": 40
}

返回值:

{
  "_index": "ziruchu",
  "_id": "1",
  "_version": 2,
  "result": "updated", // 更新
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 5,
  "_primary_term": 1
}

注意:文档不存在时,为添加操作,文档存在时,为更新操作。

判断指定文档是否存在

语法:

HEAD  <index>/_doc/<_id>

案例:

HEAD ziruchu/_doc/1

返回值:

200 - OK

获取一条文档

1、查询完整数据

语法:

GET <index>/_doc/<_id>

案例:根据指定 ID 查询文档

GET ziruchu/_doc/1

返回值:

{
  "_index": "ziruchu",
  "_id": "1",
  "_version": 2,
  "_seq_no": 5,
  "_primary_term": 1,
  "found": true,
  "_source": {
    "name": "张翠山",
    "age": 40
  }
}

2、关闭源数据字段

语法:GET <index>/_doc/<_id>?_source=false

案例:

GET ziruchu/_doc/1?_source=false

返回值:

{
  "_index": "ziruchu",
  "_id": "1",
  "_version": 2,
  "_seq_no": 5,
  "_primary_term": 1,
  "found": true
}

3、只获取 _source 数据

语法:GET <index>/_source/<_id>

案例:

GET ziruchu/_source/1

返回值:

{
  "name": "张翠山",
  "age": 40
}

查询多条文档

语法:

# 基本语法
GET /_mget
{
  "docs": [
    {
      "_index": "<index_name>",
      "_id": "<_id>"
    },
    {
      "_index": "<index_name>",
      "_id": "<_id>"
    }
  ]
}

# 简写语法
GET <index_name>/_mget
{
  "docs": [
    {
      "_id": "<_id>"
    },
    {
      "_id": "<_id>"
    }
  ]
}

# 精简语法
GET /<index_name>/_mget
{
  "ids" : ["<_id_1>", "<_id_2>"]
}

案例:

GET ziruchu/_mget
{
  "ids": ["1", "RQ1fdIYBDc1n1ijvoItc"]
}

返回值:

{
  "docs": [
    {
      "_index": "ziruchu",
      "_id": "1",
      "_version": 2,
      "_seq_no": 5,
      "_primary_term": 1,
      "found": true,
      "_source": {
        "name": "张翠山",
        "age": 40
      }
    },
    {
      "_index": "ziruchu",
      "_id": "RQ1fdIYBDc1n1ijvoItc",
      "_version": 2,
      "_seq_no": 6,
      "_primary_term": 1,
      "found": true,
      "_source": {
        "name": "赵敏",
        "age": 18
      }
    }
  ]
}

查询所有文档

语法:

GET <index_name>/search

案例:

GET ziruchu/_search

修改文档

1、修改局部字段

语法:

POST /<index>/_update/<_id>
{
  "doc": {
    "<field_name>": "<field_value>"
  }
}

案例:

POST ziruchu/_update/RQ1fdIYBDc1n1ijvoItc
{
  "doc": {
    "name": "赵敏"
  }
}

返回值:

{
  "_index": "ziruchu",
  "_id": "RQ1fdIYBDc1n1ijvoItc",
  "_version": 2,
  "result": "updated",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 6,
  "_primary_term": 1
}

删除文档

语法:

DELETE /<index>/_doc/<_id>

案例:

DELETE ziruchu/_doc/Rg1gdIYBDc1n1ijvPYuS

返回值:

{
  "_index": "ziruchu",
  "_id": "Rg1gdIYBDc1n1ijvPYuS",
  "_version": 2,
  "result": "deleted",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 9,
  "_primary_term": 1
}

Bulk API 批量操作

语法:

POST /_bulk
POST /<index>/_bulk
{"action": {"mata data"}}
{"data"}

案例:

PUT ziruchu/_bulk
{"delete": {"_index":"ziruchu", "_id":"1"}}
{"create": {"_index":"ziruchu", "_id":11}}
{"name":"张无忌","age":"3"}
{"update": {"_index":"ziruchu", "_id":"Rw1gdIYBDc1n1ijvVYva"}}
{"doc":{"name":"谢逊"}}

返回值:

{
  "took": 4,
  "errors": false,
  "items": [
    {
      "delete": {
        "_index": "ziruchu",
        "_id": "1",
        "_version": 3,
        "result": "deleted",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "_seq_no": 10,
        "_primary_term": 1,
        "status": 200
      }
    },
    {
      "create": {
        "_index": "ziruchu",
        "_id": "11",
        "_version": 1,
        "result": "created",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "_seq_no": 11,
        "_primary_term": 1,
        "status": 201
      }
    },
    {
      "update": {
        "_index": "ziruchu",
        "_id": "Rw1gdIYBDc1n1ijvVYva",
        "_version": 2,
        "result": "updated",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "_seq_no": 12,
        "_primary_term": 1,
        "status": 200
      }
    }
  ]
}
请登录后再评论