• 欢迎浏览“String me = Creater\忠实的资深Linux玩家;”,请文明浏览,理性发言,有侵犯你的权益请邮件我(creater@vip.qq.com).
  • 把任何的失败都当作一次尝试,不要自卑;把所有的成功都想成是一种幸运,不要自傲。
  •    2年前 (2016-08-18)  Elasticsearch |   抢沙发  13 
    文章评分 0 次,平均分 0.0

    官网文档: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();

    二、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();

    2.2使用doc方式

    client.prepareUpdate("blog", "article", "1")
                        .setDoc(jsonBuilder().startObject().field("content", "SVN与Git对比。。。").endObject()).get();

    三、updateRequest

    UpdateRequest updateRequest = new UpdateRequest("blog", "article", "1")
                        .doc(jsonBuilder().startObject().field("commet", "0").endObject());
                client.update(updateRequest).get();

    这种方式可以新增字段。

    四、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();

    这个例子中,如果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();
            }
    
        }
    }
     

    除特别注明外,本站所有文章均为String me = "Creater\忠实的资深Linux玩家";原创,转载请注明出处来自http://unix8.net/home.php/5063.html

    关于

    发表评论

    暂无评论

    切换注册

    登录

    忘记密码 ?

    切换登录

    注册

    扫一扫二维码分享