更新索引库

 

ElasticSearch Java Api(三) -更新索引库

标签: elasticsearchupdateesjava-api
 分类:
 

目录(?)[+]

 

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

    }
}
 
 
posted @ 2017-05-15 14:10  洞玄巅峰  阅读(429)  评论(0编辑  收藏  举报