首页 > 代码库 > Elasticsearch5.0 Java Api(二) -- 更新索引

Elasticsearch5.0 Java Api(二) -- 更新索引

  elasticsearch提供了多种更新索引的方式,这里简单介绍其中五种

技术分享
  1 package com.juyun.test;  2   3 import java.io.IOException;  4 import java.net.InetAddress;  5 import java.util.concurrent.ExecutionException;  6   7 import org.elasticsearch.action.index.IndexRequest;  8 import org.elasticsearch.action.update.UpdateRequest;  9 import org.elasticsearch.client.Client; 10 import org.elasticsearch.common.settings.Settings; 11 import org.elasticsearch.common.transport.InetSocketTransportAddress; 12 import org.elasticsearch.script.Script; 13 import org.elasticsearch.script.ScriptService; 14 import org.elasticsearch.transport.client.PreBuiltTransportClient; 15 import static org.elasticsearch.common.xcontent.XContentFactory.*; 16  17 public class ElasticSearchUpdate { 18      19     private static Client client; 20  21     /** 22      * 多种更新索引库的方式 23      * @param args 24      */ 25     public static void main(String[] args) { 26  27         try { 28             // 设置集群名称  29             Settings settings = Settings.builder().put("cluster.name", "elasticsearch").build(); 30             // 创建client 31             client = new PreBuiltTransportClient(settings) 32                     .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("172.16.0.210"), 9300)); 33            34         } catch (Exception e) { 35             e.printStackTrace(); 36         } 37         upMethod5(); 38          39         // 关闭client 40         client.close(); 41     } 42      43     // 方法一:创建一个UpdateRequest,然后将其发送给client. 44     // 原文档不存在时,使用此方法会报错 45     public static void upMethod1() { 46         try { 47             UpdateRequest uRequest = new UpdateRequest(); 48             uRequest.index("flow"); 49             uRequest.type("data"); 50             uRequest.id("AVvl93FjL55UXUpfSV9X"); 51             uRequest.doc(jsonBuilder().startObject().field("inbyte", 200.0).endObject()); 52             client.update(uRequest).get(); 53         } catch (IOException e) { 54             e.printStackTrace(); 55         } catch (InterruptedException e) { 56             e.printStackTrace(); 57         } catch (ExecutionException e) { 58             e.printStackTrace(); 59         } 60  61     } 62  63     // 方法二:prepareUpdate() 使用脚本更新索引 64     // 需要在elasticsearch.yml,新增一行:script.engine.groovy.inline.update: on,再重启ES 65     public static void upMethod2() { 66         client.prepareUpdate("flow", "data", "2") 67                 .setScript(new Script("ctx._source.inbyte = \"3.14\"", ScriptService.ScriptType.INLINE, null, null)) 68                 .get(); 69     } 70  71     // 方法三:prepareUpdate() 使用doc更新索引 72     public static void upMethod3() { 73         try { 74             client.prepareUpdate("flow", "data", "AVvl93JtL55UXUpfSV9Y") 75                     .setDoc(jsonBuilder().startObject().field("inbyte", 3.14).endObject()).get(); 76         } catch (IOException e) { 77             e.printStackTrace(); 78         } 79  80     } 81  82     // 方法四:增加新的字段 83     public static void upMethod4() { 84         try { 85             UpdateRequest updateRequest = new UpdateRequest("flow","data", "AVvl93JtL55UXUpfSV9Y") 86                     .doc(jsonBuilder().startObject().field("package", "100").endObject()); 87             client.update(updateRequest).get(); 88         } catch (IOException e) { 89             e.printStackTrace(); 90         } catch (InterruptedException e) { 91             e.printStackTrace(); 92         } catch (ExecutionException e) { 93             e.printStackTrace(); 94         } 95     } 96  97     // 方法五:upsert 如果文档不存在则创建新的索引 98     // 如果存在,那么根据UpdateRequest更新索引;如果不存在,新建indexRequest索引. 99     public static void upMethod5() {100         try {101             IndexRequest indexRequest = new IndexRequest("flow", "data", "5").source(jsonBuilder().startObject()102                     .field("time", "20170508").field("package", "121").endObject());103 104             UpdateRequest uRequest2 = new UpdateRequest("flow", "data", "5").doc(105                     jsonBuilder().startObject().field("time", "20170508").field("package", "12114").endObject())106                     .upsert(indexRequest);107             client.update(uRequest2).get();108         } catch (IOException e) {109             e.printStackTrace();110         } catch (InterruptedException e) {111             e.printStackTrace();112         } catch (ExecutionException e) {113             e.printStackTrace();114         }115 116     }117 118 }
ElasticSearchUpdate

 

Elasticsearch5.0 Java Api(二) -- 更新索引