日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

ElasticSearch(六) Update API

發布時間:2023/11/27 生活经验 58 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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

總結

以上是生活随笔為你收集整理的ElasticSearch(六) Update API的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。