ElasticSearch Java Api(三) -更新索引库
来源:互联网 发布:网络社群有哪些 编辑:程序博客网 时间:2024/05/22 12:50
官网文档:https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-docs-update.html
一、UpdateRequest
创建一个UpdateRequest,然后将其发送给client.
UpdateRequest uRequest = new UpdateRequest(); uRequest.index("blog"); uRequest.type("article"); uRequest.id("2"); uRequest.doc(jsonBuilder().startObject().field("content", "学习目标 掌握java泛型的产生意义ssss").endObject()); client.update(uRequest).get();
- 1
- 2
- 3
二、prepareUpdate()
2.1使用脚本方式
首先打开elasticsearch-2.3.3/config/elasticsearch.yml,新增一行:
script.engine.groovy.inline.update: on
之后重启elasticsearch.
client.prepareUpdate("blog", "article", "1") .setScript(new Script("ctx._source.title = \"git入门\"", ScriptService.ScriptType.INLINE, null, null)) .get();
- 1
2.2使用doc方式
client.prepareUpdate("blog", "article", "1") .setDoc(jsonBuilder().startObject().field("content", "SVN与Git对比。。。").endObject()).get();
- 1
三、updateRequest
UpdateRequest updateRequest = new UpdateRequest("blog", "article", "1") .doc(jsonBuilder().startObject().field("commet", "0").endObject()); client.update(updateRequest).get();
- 1
- 2
- 3
这种方式可以新增字段。
四、upsert
如果文档不存在则创建新的索引.
IndexRequest indexRequest = new IndexRequest("blog", "article", "10").source(jsonBuilder().startObject() .field("title", "Git安装10").field("content", "学习目标 git。。。10").endObject());UpdateRequest uRequest2 = new UpdateRequest("blog", "article", "10").doc( jsonBuilder().startObject().field("title", "Git安装").field("content", "学习目标 git。。。").endObject()) .upsert(indexRequest); client.update(uRequest2).get();
- 1
这个例子中,如果blog/article/10存在,那么根据UpdateRequest更新索引;如果不存在,新建indexRequest索引.
五、java demo
package cn.com.bropen.es;import java.io.IOException;import java.net.InetAddress;import java.net.UnknownHostException;import java.util.concurrent.ExecutionException;import org.elasticsearch.action.index.IndexRequest;import org.elasticsearch.action.update.UpdateRequest;import org.elasticsearch.client.Client;import org.elasticsearch.client.transport.TransportClient;import org.elasticsearch.common.transport.InetSocketTransportAddress;import org.elasticsearch.script.Script;import org.elasticsearch.script.ScriptService;import static org.elasticsearch.common.xcontent.XContentFactory.*;public class ElasticSearchUpdate { private static Client client; public static void main(String[] args) { try { // client startup client = TransportClient.builder().build() .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300)); } catch (UnknownHostException e) { e.printStackTrace(); } upMethod1(); } public static void upMethod1() { try { // 方法一:创建一个UpdateRequest,然后将其发送给client. UpdateRequest uRequest = new UpdateRequest(); uRequest.index("blog"); uRequest.type("article"); uRequest.id("22"); uRequest.doc(jsonBuilder().startObject().field("content", "学习目标 掌握java泛型的产生意义ssss").endObject()); client.update(uRequest).get(); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } } public static void upMethod2() { // 方法二:prepareUpdate() 使用脚本更新索引 client.prepareUpdate("blog", "article", "1") .setScript(new Script("ctx._source.title = \"git入门\"", ScriptService.ScriptType.INLINE, null, null)) .get(); } public static void upMethod3() { // 方法三:prepareUpdate() 使用doc更新索引 try { client.prepareUpdate("blog", "article", "1") .setDoc(jsonBuilder().startObject().field("content", "SVN与Git对比。。。").endObject()).get(); } catch (IOException e) { e.printStackTrace(); } } public static void upMethod4() { // 方法四: 增加新的字段 try { UpdateRequest updateRequest = new UpdateRequest("blog", "article", "1") .doc(jsonBuilder().startObject().field("commet", "0").endObject()); client.update(updateRequest).get(); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } } public static void upMethod5() { // 方法五:upsert 如果文档不存在则创建新的索引 try { IndexRequest indexRequest = new IndexRequest("blog", "article", "10").source(jsonBuilder().startObject() .field("title", "Git安装10").field("content", "学习目标 git。。。10").endObject()); UpdateRequest uRequest2 = new UpdateRequest("blog", "article", "10").doc( jsonBuilder().startObject().field("title", "Git安装").field("content", "学习目标 git。。。").endObject()) .upsert(indexRequest); client.update(uRequest2).get(); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } }}
阅读全文
0 0
- ElasticSearch Java Api(三) -更新索引库
- ElasticSearch Java Api(三) -更新索引库
- ElasticSearch Java Api(三) -更新索引库
- ElasticSearch Java Api(三) -更新索引库
- Elasticsearch Java API 的使用(4)—更新索引
- ElasticSearch Java Api(二) -检索索引库
- ElasticSearch Java Api(二) -检索索引库
- ElasticSearch Java Api(二) -检索索引库
- ElasticSearch Java Api(二) -检索索引库
- 分布式搜索elasticsearch java API 之(三)------索引数据
- 分布式搜索elasticsearch java API 之(三)------索引数据
- ElasticSearch Java Api -删除索引
- 分布式搜索elasticsearch java API 之(三)(四)------索引数据、删除索引数据
- Elasticsearch java API (9)更新的API
- Elasticsearch Java API之清空索引
- elasticsearch清空索引缓存 java api
- ElasticSearch Java Api(四) -删除索引
- Elasticsearch java api(五) Bulk批量索引
- Jmeter连接远程机器进行压力测试
- 抛异常的正确姿势
- 一天都被 Kotlin 刷屏了,CSDN学院不服气,我也来刷刷
- 全景图相关
- Java MVC Controller 中通过不同方式获取 @PathVariable 参数值
- ElasticSearch Java Api(三) -更新索引库
- Android列表用法之二:实战ListView高级用法
- Android中进程与线程及如何在子线程中操作UI线程
- 开发中用到的自定义封装工具类之三
- Js 跨域问题。
- jquery.validate 在html标签中写验证规则,jquery validate的正确用法
- 项目中svn使用的一些注意事项(新手)
- make nginx报错
- C#中Socket通信编程的同步实现