es 中使用update 、create 、index的区别

文章目录

      • [1. `index` 操作](#1. index 操作)
        • [示例:`index` 操作](#示例:index 操作)
        • [Bulk 示例中的 `index` 操作:](#Bulk 示例中的 index 操作:)
      • [2. `create` 操作](#2. create 操作)
        • [示例:`create` 操作](#示例:create 操作)
        • [Bulk 示例中的 `create` 操作:](#Bulk 示例中的 create 操作:)
      • [3. `update` 操作](#3. update 操作)
        • [示例:`update` 操作](#示例:update 操作)
        • [Bulk 示例中的 `update` 操作:](#Bulk 示例中的 update 操作:)
      • [4. 区别总结](#4. 区别总结)
      • 总结

在 Elasticsearch 中, indexupdatecreate 是常用的操作类型,它们用于不同的场景,且具有不同的行为。下面详细解释这三个操作的区别,并提供相关的示例。

1. index 操作

  • 功能index 操作用于插入文档,如果文档的 _id 已经存在,则会覆盖该文档(即进行替换)。
  • 使用场景:适用于需要插入新文档,或者更新已有文档的场景。
  • 注意index 操作是幂等的(idempotent)。即使你多次执行相同的操作,只要文档内容没有变化,最终结果不会受到影响。
示例:index 操作
json 复制代码
POST /articles/_doc/1
{
  "title": "Tech News Today",
  "tags": ["tech", "news", "AI"]
}
  • 如果 _id1 的文档不存在,则会插入一个新的文档。

    POST /articles/_search

    [
    {
    "_index" : "articles",
    "_type" : "_doc",
    "_id" : "1",
    "_score" : 1.0,
    "_source" : {
    "title" : "Tech News Today",
    "tags" : [
    "tech",
    "news",
    "AI"
    ]
    }
    }
    ]

  • 如果 _id1 的文档已存在,则会被替换为新的文档。

    POST /articles/_doc/1
    {
    "title": "Tech News Today"
    }

    POST /articles/_search

    [
    {
    "_index" : "articles",
    "_type" : "_doc",
    "_id" : "1",
    "_score" : 1.0,
    "_source" : {
    "title" : "Tech News Today"
    }
    }
    ]

Bulk 示例中的 index 操作:
json 复制代码
{ "index": { "_id": 1 } }
{ "title": "Tech News Today", "tags": ["tech", "news", "AI"] }

2. create 操作

  • 功能create 操作用于插入新文档。如果文档的 _id 已经存在,操作会失败(返回错误)。这意味着该操作只能用于创建新文档,而不能更新或替换已有文档。
  • 使用场景:适用于你想要确保文档不存在时插入文档的情况。比如避免覆盖现有的文档。
示例:create 操作
json 复制代码
POST /articles/_doc/1/_create
{
  "title": "Tech News Today",
  "tags": ["tech", "news", "AI"]
}
  • 如果 _id1 的文档已存在,Elasticsearch 会返回错误,提示该文档已经存在。

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

  • 如果 _id1 的文档不存在,它将会被插入。

Bulk 示例中的 create 操作:
json 复制代码
{ "create": { "_id": 1 } }
{ "title": "Tech News Today", "tags": ["tech", "news", "AI"] }

3. update 操作

  • 功能update 操作用于更新已有的文档。如果文档存在,它会更新文档的部分内容;如果文档不存在,则会抛出错误。
  • 使用场景 :适用于部分更新文档的场景,特别是当你只需要更新文档的一部分字段时。update 操作不会覆盖整个文档,只会对指定字段进行修改。
示例:update 操作
json 复制代码
POST /articles/_doc/1/_update
{
  "doc": {
    "tags": ["tech", "news", "AI", "machine learning"]
  }
}
  • 如果 _id1 的文档存在,tags 字段将会被更新为新的值。

    POST /articles/_search

    [
    {
    "_index" : "articles",
    "_type" : "_doc",
    "_id" : "1",
    "_score" : 1.0,
    "_source" : {
    "title" : "Tech News Today",
    "tags" : [
    "tech",
    "news",
    "AI",
    "machine learning"
    ]
    }
    }
    ]

  • 如果 _id1 的文档不存在,Elasticsearch 会返回错误。

    DELETE articles

    POST /articles/_doc/1/_update
    {
    "doc": {
    "tags": ["tech", "news", "AI", "machine learning"]
    }
    }

    {
    "error" : {
    "root_cause" : [
    {
    "type" : "document_missing_exception",
    "reason" : "[_doc][1]: document missing",
    "index_uuid" : "B9MrCqCwT5u-GCvgdWB-Rw",
    "shard" : "0",
    "index" : "articles"
    }
    ],
    "type" : "document_missing_exception",
    "reason" : "[_doc][1]: document missing",
    "index_uuid" : "B9MrCqCwT5u-GCvgdWB-Rw",
    "shard" : "0",
    "index" : "articles"
    },
    "status" : 404
    }

Bulk 示例中的 update 操作:
json 复制代码
{ "update": { "_id": 1 } }
{ "doc": { "tags": ["tech", "news", "AI", "machine learning"] } }

4. 区别总结

操作类型 目标 说明 示例
index 插入或替换 文档插入,如果文档已经存在则会被替换。 POST /articles/_doc/1
create 插入 仅在文档不存在时插入文档,如果文档已存在,则返回错误。 POST /articles/_doc/1/_create
update 更新 仅更新文档的一部分内容,文档必须存在。如果文档不存在,则返回错误。 POST /articles/_doc/1/_update

总结

  • index 用于插入新文档或替换现有文档。
  • create 用于仅在文档不存在时插入新文档,若文档已存在则返回错误。
  • update 用于更新现有文档的部分内容,若文档不存在则返回错误。
相关推荐
C_V_Better33 分钟前
Elasticsearch 创建索引别名的正确姿势
大数据·elasticsearch
在未来等你40 分钟前
Kafka面试精讲 Day 16:生产者性能优化策略
大数据·分布式·面试·kafka·消息队列
王大帅の王同学1 小时前
Thinkphp6接入讯飞星火大模型Spark Lite完全免费的API
大数据·分布式·spark
fanstuck8 小时前
基于大模型的个性化推荐系统实现探索与应用
大数据·人工智能·语言模型·数据挖掘
IT学长编程9 小时前
计算机毕业设计 基于大数据技术的医疗数据分析与研究 Python 大数据毕业设计 Hadoop毕业设计选题【附源码+文档报告+安装调试】
大数据·hadoop·机器学习·数据分析·毕业设计·毕业论文·医疗数据分析
lwprain10 小时前
龙蜥8.10中spark各种集群及单机模式的搭建spark3.5.6(基于hadoop3.3.6集群)
大数据·ajax·spark
shallwe小威10 小时前
SpringBoot集成ElasticSearch
数据库·spring boot·elasticsearch
电商软件开发 小银11 小时前
本地生活服务平台创新模式观察:积分体系如何重塑消费生态?
大数据·人工智能·数字化转型·私域运营·消费者心理学
chenglin01611 小时前
TOGAF——ArchiMate
大数据
扬帆起航1311 小时前
亚马逊新品推广破局指南:从手动试错到智能闭环的系统化路径
大数据·数据库·人工智能