Elasticsearch Document Update API详解、原理与示例

本文涉及的产品
检索分析服务 Elasticsearch 版,2核4GB开发者规格 1个月
简介: Elasticsearch Document Update API详解、原理与示例

本文将详细介绍单文档(Document)的更新API,其更新API如下:


  • public final UpdateResponse update(UpdateRequest updateRequest, RequestOptions options) throws IOException
  • public final void updateAsync(UpdateRequest updateRequest, RequestOptions options, ActionListener<UpdateResponse> listener)


重点关注UpdateRequest。


1、UpdateRequest详解


UpdateRequest的核心类图如图所示:

f8ff5b2efecd4219b8eb7074799647b3.jpg

我们首先来看一下UpdateRequest的核心属性:


  • protected ShardId shardId:指定需要执行的分片信息。
  • protected String index:索引库,类似于关系型数据库的数据库。
  • private String type:类型名,类似于关系数据库的表。
  • private String id :文档ID,类似于关系数据库的主键ID。
  • private String routing:分片值,默认为id的值,es的分片路由算法为( hashcode(routing) % primary_sharding_count)
  • private String parent:
  • Script script:通过脚步更新文档。
  • private String[] fields:需要返回的字段信息,默认为不返回,已废弃,被fetchSourceContext代替。
  • private FetchSourceContext fetchSourceContext:执行更新操作后,如果命中,需要返回_source的上下文配置,与fields的区别是fetchSourceContext支持通配符表达式来匹配字段名,已经在Elasticsearch Document Get API详解、原理与示例详细介绍过
  • private long version = Versions.MATCH_ANY:版本号
  • private VersionType versionType = VersionType.INTERNAL:版本类型,分为内部版本、外部版本,默认为内部版本。
  • private int retryOnConflict = 0:更新冲突时重试次数。
  • private RefreshPolicy refreshPolicy = RefreshPolicy.NONE:刷新策略。NONE:代表不重试;
  • private ActiveShardCount waitForActiveShards = ActiveShardCount.DEFAULT:执行操作之前需要等待激活的副本数,已在Elasticsearch Document Get API详解、原理与示例中详细介绍。
  • private IndexRequest upsertRequest:使用该 字段进行更新操作,如果原索引不存在,则更新,类似于saveOrUpdate操作,该操作需要与脚步执行,详细将在后续章节中描述,
  • private boolean scriptedUpsert = false;是否是用脚步执行更新操作。
  • private boolean docAsUpsert = false; 是否使用saveOrUpdate模式,即是否使用IndexRequest upsertRequest进行更新操作。(docAsUpser=true+ doc组合,将使用saveOrUpdate模式)。
  • private boolean detectNoop = true;是否检查空操作,下文会进行详细介绍。
  • private IndexRequest doc;默认使用该请求进行更新操作。

从上述我们基本可以得知更新基本有3种方式,script、upsert、doc(普通更新)。


2、深入分析Elasticsearch Update API(更新API)



2.1 Script脚步更新


Elasticsearch可以通过脚本(painless)进行更新,本节将不会深入去学习其语法,后续会看单独的章节对其进行详细讲解。


2.2 部分字段更新(普通更新方式)


更新API支持传递一个部分文档(_source字段中包含类型的部门字段),它将被合并到现有的文档中(简单的递归合并,对象的内部合并,替换核心的“键/值”和数组)。如果需要完全替代现有的文档,请使用(Index API)。以下部分更新为现有文档添加了一个新字段:(下文会给出基于java的API调用)。

POST test/_doc/1/_update
{
    "doc" : {
        "name" : "new_name"
    }
}

如果指定了doc和script,则script属性优先,关于更新API一个比较好的实践是使用脚步更新(painless),后续会重点章节详细介绍。


2.3 检测空更新(检测本请求是否值得更新)


该功能特性的意思是当提交的请求,发现与原文档的数据并未发送变化,是否执行update操作,默认检测。如果开启检测,detectNoop=true,如果检测到数据并未发生变化,则返回结果为noop(空操作),如果detectNoop=false,每次操作都会执行,版本号将自增。


2.4 保存或更新(Upserts)


如果文档还不存在,upsert元素的内容将作为新文档插入。Elasticsearch支持scripted_upsert和doc_as_upsert两种模式,以scripted_upsert优先。通过UpdateRequest#scriptedUpsert和UpdateRequest#docAsUpsert控制。


2.5 核心参数一览表


更新API主要核心参数一览表:

image.png

注意:更新API不支持除内部以外的版本控制,外部(版本类型外部和外部的)或强制(版本类型的force)版本控制不受更新API的支持,因为它会导致弹性搜索版本号与外部系统不同步。


3、Update API使用示例


本节将暂时不会展示使用脚步进行更新的Demo,此部分会在后续文章中单独的章节来介绍ElasticSearch painless Script。


3.1 常规更新(更新部分字段)


public static void testUpdate_partial() {
        RestHighLevelClient client = EsClient.getClient();
        try {
            UpdateRequest request = new UpdateRequest("twitter", "_doc", "10");
            IndexRequest indexRequest = new IndexRequest("twitter", "_doc", "10");
            Map<String, String> source = new HashMap<>();
            source.put("user", "dingw2");
            indexRequest.source(source);
            request.doc(indexRequest);
            UpdateResponse result = client.update(request, RequestOptions.DEFAULT);
            System.out.println(result);
            testGet();
        } catch (Throwable e) {
            e.printStackTrace();
        } finally {
            EsClient.close(client);
        }
    }

最终结果:调用get API能反映出user字段已经更新为dingw2,及更新成功。


3.2 开启detectNoop示例(并且不改变原始数据)


public static void testUpdate_noop() {
        RestHighLevelClient client = EsClient.getClient();
        try {
            UpdateRequest request = new UpdateRequest("twitter", "_doc", "10");
            request.detectNoop(true);
            request.doc(buildIndexRequest());
            UpdateResponse result = client.update(request, RequestOptions.DEFAULT);
            System.out.println(result);
        } catch (Throwable e) {
            e.printStackTrace();
        } finally {
            EsClient.close(client);
        }
    }

返回结果:

{
   "_shards": {
        "total": 0,
        "successful": 0,
        "failed": 0
   },
   "_index": "twitter",
   "_type": "_doc",
   "_id": "10",
   "_version": 6,
   "result": "noop"
}


3.3不开启detectNoop示例(并且不改变原始数据)


public static void testUpdate_no_noop() {
        RestHighLevelClient client = EsClient.getClient();
        try {
            UpdateRequest request = new UpdateRequest("twitter", "_doc", "10");
            request.detectNoop(false);
            request.doc(buildIndexRequest());
            UpdateResponse result = client.update(request, RequestOptions.DEFAULT);
            System.out.println(result);
        } catch (Throwable e) {
            e.printStackTrace();
        } finally {
            EsClient.close(client);
        }
    }

返回结果:

{
   "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
   },
   "_index": "twitter",
   "_type": "_doc",
   "_id": "10",
   "_version": 7,
   "result": "updated"
}

其主要特征表现为result=updated,表示执行的动作为更新,并且版本号自增1,_shards反馈的是各分片的执行情况。


3.4 saveOrUpdate更新模式(upsert)


/**
     * 更新操作,原记录不存在,使用saveOrUpdate模式。
     */
    public static void testUpdate_upsert() {
        RestHighLevelClient client = EsClient.getClient();
        try {
            UpdateRequest request = new UpdateRequest("twitter", "_doc", "11");
            IndexRequest indexRequest = new IndexRequest("twitter", "_doc", "11");
            Map<String, String> source = new HashMap<>();
            source.put("user", "dingw");
            source.put("post_date", "2009-11-17T14:12:12");
            source.put("message", "hello,update upsert。");
            indexRequest.source(source);
            request.doc(indexRequest);
            request.docAsUpsert(true);
            UpdateResponse result = client.update(request, RequestOptions.DEFAULT);
            System.out.println(result);
        } catch (Throwable e) {
            e.printStackTrace();
        } finally {
            EsClient.close(client);
        }
    }

返回结果:

/**
     * 更新操作,原记录不存在,使用saveOrUpdate模式。
     */
    public static void testUpdate_upsert() {
        RestHighLevelClient client = EsClient.getClient();
        try {
            UpdateRequest request = new UpdateRequest("twitter", "_doc", "11");
            IndexRequest indexRequest = new IndexRequest("twitter", "_doc", "11");
            Map<String, String> source = new HashMap<>();
            source.put("user", "dingw");
            source.put("post_date", "2009-11-17T14:12:12");
            source.put("message", "hello,update upsert。");
            indexRequest.source(source);
            request.doc(indexRequest);
            request.docAsUpsert(true);
            UpdateResponse result = client.update(request, RequestOptions.DEFAULT);
            System.out.println(result);
        } catch (Throwable e) {
            e.printStackTrace();
        } finally {
            EsClient.close(client);
        }
    }

返回结果:

{
   "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
   },
   "_index": "twitter",
   "_type": "_doc",
   "_id": "11",
   "_version": 1,
   "result": "created"
}

返回结果其核心表现为:result:created,表示是一个新增操作。


Document API就讲解到这里了,本节详细介绍了Document Update  API的核心关键点以及实现要点,最后给出Demo展示如何在JAVA中使用Update API。

相关实践学习
使用阿里云Elasticsearch体验信息检索加速
通过创建登录阿里云Elasticsearch集群,使用DataWorks将MySQL数据同步至Elasticsearch,体验多条件检索效果,简单展示数据同步和信息检索加速的过程和操作。
ElasticSearch 入门精讲
ElasticSearch是一个开源的、基于Lucene的、分布式、高扩展、高实时的搜索与数据分析引擎。根据DB-Engines的排名显示,Elasticsearch是最受欢迎的企业搜索引擎,其次是Apache Solr(也是基于Lucene)。 ElasticSearch的实现原理主要分为以下几个步骤: 用户将数据提交到Elastic Search 数据库中 通过分词控制器去将对应的语句分词,将其权重和分词结果一并存入数据 当用户搜索数据时候,再根据权重将结果排名、打分 将返回结果呈现给用户 Elasticsearch可以用于搜索各种文档。它提供可扩展的搜索,具有接近实时的搜索,并支持多租户。
相关文章
|
1月前
|
存储 搜索推荐 数据挖掘
ElasticSearch架构介绍及原理解析
ElasticSearch架构介绍及原理解析
106 0
|
2月前
|
存储 API 索引
Elasticsearch Reroute API 的使用
Elasticsearch Reroute API 的使用
42 1
|
3月前
|
存储 JSON 自然语言处理
Elasticsearch 利用API进行搜索
Elasticsearch 利用API进行搜索
36 0
|
3月前
|
自然语言处理 API 索引
Elasticsearch Analyzer原理分析并实现中文分词
Elasticsearch Analyzer原理分析并实现中文分词
74 0
|
2天前
|
安全 Java API
Spring工厂API与原理
Spring工厂API与原理
23 10
|
16天前
|
JSON 监控 API
在API接口对接中关键示例问题(1)
在API接口对接中,有几个关键的问题需要注意,以确保接口的稳定性、安全性和易用性。以下是这些问题及部分示例代码的简要概述
|
1月前
|
Java API PHP
获取1688商品详情API:步骤与代码示例
在电子商务领域,阿里巴巴的1688平台是一个广受商家和开发者欢迎的批发交易市场。若您是一名开发者,希望建立自己的应用程序或网站来获取并展示1688上的商品信息,您可能需要使用到1688提供的API接口。以下是获取1688商品详情API的详细步骤说明。
|
1月前
|
Java API
Java 日期和时间 API:实用技巧与示例 - 轻松处理日期和时间
简介 Scanner 类用于获取用户输入,它位于 java.util 包中。 使用 Scanner 类 要使用 Scanner 类,请执行以下步骤: 导入 java.util.Scanner 包。 创建一个 Scanner 对象,并将其初始化为 System.in。 使用 Scanner 对象的方法读取用户输入。
54 1
|
2月前
|
XML 搜索推荐 API
通义千问API:让大模型使用各种工具
本章我们将通过一个简单的例子,揭示基于LangChain的Agent开发的秘密,从而了解如何扩展大模型的能力。
67278 184
通义千问API:让大模型使用各种工具
|
2月前
|
存储 SQL Java
聚合在Elasticsearch中的使用及示例验证
聚合在Elasticsearch中的使用及示例验证
71 0