ElasticSearch(六) Update API

一、修改部分字段By  UpdateRequest

UpdateRequest updateRequest = new UpdateRequest();
updateRequest.index("index");
updateRequest.type("type");
updateRequest.id("1");
updateRequest.doc(jsonBuilder()
        .startObject()
            .field("gender", "male")
        .endObject());
client.update(updateRequest).get();

client.prepareUpdate("ttl", "doc", "1")
.setScript(new Script("ctx._source.gender = \"male\"" , ScriptService.ScriptType.INLINE, null, null))
.get();

client.prepareUpdate("ttl", "doc", "1")
.setDoc(jsonBuilder()
.startObject()
.field("gender", "male")
.endObject())
.get();

UpdateRequest updateRequest = new UpdateRequest("ttl", "doc", "1")
        .script(new Script("ctx._source.gender = \"male\""));
client.update(updateRequest).get();
UpdateRequest updateRequest = new UpdateRequest("index", "type", "1")
        .doc(jsonBuilder()
            .startObject()
                .field("gender", "male")
            .endObject());
client.update(updateRequest).get();

二、乐观锁方式 指定Version 版本更新(es 5)

RefreshPolicy.IMMEDIATE 更新策略为立即更新,ElasticSearch 实际上是伪实时的,所有分片之间默认1s同步更新

IndexResponse response = client.prepareIndex().setRefreshPolicy(RefreshPolicy.IMMEDIATE).setIndex(indexName)
.setType(tableName).setId(statid).setVersion(version).setSource(jsondata).execute().actionGet();

三、批量更新(es 5)

/**
* 批量索引
*
* @param indexName
* @param tableName
* @param maps
*/
public void bulkIndex(String indexName, String tableName, Map<String, String> maps) {

if (maps == null || maps.isEmpty()) {
return;
}

logger.info("[[title=bulkIndex]] start");
BulkRequestBuilder bulkRequest = client.prepareBulk().setRefreshPolicy(RefreshPolicy.IMMEDIATE);

int i = 1;
for (Map.Entry<String, String> entry : maps.entrySet()) {

bulkRequest.add(client.prepareIndex().setIndex(indexName).setType(tableName).setId(entry.getKey())
.setSource(entry.getValue()));

if (i % 100 == 0) {
bulkRequest.execute().actionGet();
}
i++;
}

bulkRequest.execute().actionGet();

String fieldString = SensitiveInfoMask.maskJsonSensitiveField(JSON.toJSONString(maps),
"(contactMobile|clientName|contactName|idCardNo)");
logger.info("[[title=bulkIndex]]批量索引信息={}", fieldString);
logger.info("[[title=bulkIndex]] end");
}

Upsertedit
There is also support for upsert. If the document does not exist, the content of the upsert element will be used to index the fresh doc:

IndexRequest indexRequest = new IndexRequest("index", "type", "1")
.source(jsonBuilder()
.startObject()
.field("name", "Joe Smith")
.field("gender", "male")
.endObject());
UpdateRequest updateRequest = new UpdateRequest("index", "type", "1")
.doc(jsonBuilder()
.startObject()
.field("gender", "male")
.endObject())
.upsert(indexRequest);
client.update(updateRequest).get();

If the document does not exist, the one in indexRequest will be added

If the document index/type/1 already exists, we will have after this operation a document like:

{
"name" : "Joe Dalton",
"gender": "male"
}

This field is added by the update request

If it does not exist, we will have a new document:

{
"name" : "Joe Smith",
"gender": "male"
}



原文地址:https://www.cnblogs.com/xiaocandou/p/8118638.html

时间: 2024-11-17 20:34:28

ElasticSearch(六) Update API的相关文章

elasticsearch 6.0java api的使用

elasticsearch 6.0 中java api的使用 1:使用java api创建elasticsearch客户端 package com.search.elasticsearch; import org.elasticsearch.client.transport.TransportClient; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.Tr

使用JAVA操作ElasticSearch(Java API 和Spring Data ElasticSearch)

Java API 我的ElasticSearch集群的版本是6.2.4,导入elasticsearch相关的maven依赖也是6.2.4,不同版本的api可能会有差异 一:maven依赖 <!--elasticsearch核心依赖--> <dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version

ElasticSearch之Java Api 测试

增加Maven依赖 <dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>5.0.0</version> </dependency> <dependency> <groupId>org.elasticsearch.client</groupI

Elasticsearch使用REST API实现全文检索

通过rest api添加检索数据,阅读官方文档可以发现,elasticsearch支持动态映射,但是其中有不少问题,且听慢慢详解. 本文主要讲述三点内容: 1 Elasticsearch常用的rest api 2 Elasticsearch在添加索引时,动态映射报错:MapperParsingException[failed to parse]nested;JsonParseException 3 Elasticsearch使用bulk命令添加索引数据 ES REST API elasticse

Elasticsearch常用操作API

1. 查询所有的索引 [[email protected] cx]# curl '10.0.0.5:9200/_cat/indices?v' health status index    pri rep docs.count docs.deleted store.size pri.store.size  yellow open   customer   5   1          2            0      6.6kb          6.6kb  yellow open   b

探索Elasticsearch集群API

Elasticsearch提供了一个非常全面和强大的REST API,您可以使用与您的集群进行交互.为数不多的可以用API的事情如下: 检查您的集群.节点和索引健康状态和统计数据 管理集群.节点和索引数据和元数据 执行CRUD(创建.读取.更新和删除)索引和搜索操作 执行高级搜索操作,比如分页.排序.过滤.脚本.聚合,和许多其他人 1. 可以使用 _cat API查看elasticsearch的健康状况,但是启动时不能以守护进程方式启动. [[email protected] ~]#  curl

ElasticSearch 中 REST API 详解

本文主要内容: 1 ElasticSearch常用的操作 2 ElasticSearchbulk命令 ES REST API elasticsearch支持多种通讯,其中包括http请求响应服务,因此通过curl命令,可以发送http请求,并得到json返回内容. 常用的REST请求有: 检查集群状态 curl localhost:9200/_cat/health?v 检查节点状态 curl localhost:9200/_cat/nodes?v 查询全部索引 curl localhost:92

ElasticSearch partial update+mget+bulk

一.partial update 1.什么是partial update? PUT /index/type/id,创建文档&替换文档,就是一样的语法 一般对应到应用程序中,每次的执行流程基本是这样的: (1)应用程序先发起一个get请求,获取到document,展示到前台界面,供用户查看和修改(2)用户在前台界面修改数据,发送到后台(3)后台代码,会将用户修改的数据在内存中进行执行,然后封装好修改后的全量数据(4)然后发送PUT请求,到es中,进行全量替换(5)es将老的document标记为d

Elasticsearch技术解析与实战(七)Elasticsearch partial update

普通的partial update 1.插入测试数据 PUT /test_index/test_type/10 { "test_field1": "test1", "test_field2": "test2" } 2.更新 POST /test_index/test_type/10/_update { "doc": { "test_field2": "updated test2