Elasticsearch JAVA API 之 Document API

注: 本篇为介绍ES java api 第一篇,对于API的接口含义不做过多解读(这类文章有很多),代码为主。
ESClient 类

public class ESClient {
    private static Logger log = LoggerFactory.getLogger(ESClient.class);

    public static Client getLocalHostTransportClient(String clusterName) {
        Settings settings = Settings.builder()
                .put("cluster.name", clusterName).build();

        TransportClient client = new PreBuiltTransportClient(settings);
        try {
            client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));
        } catch (UnknownHostException e) {
            log.error(e.getMessage(), e);
        }

        return client;
    }

    public static Client getLocalHostTransportClient() {

        TransportClient client = new PreBuiltTransportClient(Settings.EMPTY);
        try {
            client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));
        } catch (UnknownHostException e) {
            log.error(e.getMessage(), e);
        }

        return client;
    }
    }

Index(此处Index为动词) Document 分四种场景,分别是:

  • Manually (aka do it yourself) using native byte[] or as a String

  • Using a Map that will be automatically converted to its JSON equivalent

  • Using a third party library to serialize your beans such as Jackson

  • Using built-in helpers XContentFactory.jsonBuilder()

import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.bulk.*;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.get.MultiGetItemResponse;
import org.elasticsearch.action.get.MultiGetResponse;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.unit.ByteSizeUnit;
import org.elasticsearch.common.unit.ByteSizeValue;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.reindex.BulkIndexByScrollResponse;
import org.elasticsearch.index.reindex.DeleteByQueryAction;
import org.elasticsearch.rest.RestStatus;

import java.io.IOException;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.ExecutionException;

import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;

/**
 * Created by root on 17-2-23.
 */
public class IndexDocument {
    /**
     *  index by json
     */
    public static IndexResponse indexDocByJSON(IndexRequestBuilder indexRequestBuilder, String json) {

        // Index
        indexRequestBuilder.setSource(json);


        IndexResponse indexResponse = indexRequestBuilder.get();

        System.out.println(indexResponse.toString());
        return indexResponse;
    }
/*index by map */
    public static IndexResponse indexDocByMap(IndexRequestBuilder indexRequestBuilder, Map<String, Object> map) {

        indexRequestBuilder.setSource(map);

        IndexResponse indexResponse = indexRequestBuilder.get();
        System.out.println(indexResponse.toString());
        return indexResponse;
    }
/*Index by bean*/
    public static IndexResponse indexDocByBean(IndexRequestBuilder indexRequestBuilder, Object bean) {

        ObjectMapper mapper = new ObjectMapper();
        try {
            byte[] json = mapper.writeValueAsBytes(bean);
            indexRequestBuilder.setSource(json);
        } catch (JsonProcessingException e) {
            return null;
        } catch (IOException e) {
            e.printStackTrace();
        }

        IndexResponse response = indexRequestBuilder.get();
        System.out.println(response.toString());
        RestStatus restStatus = response.status();
//        restStatus.getStatus();
        System.out.println(restStatus.getStatus());
        return response;
    }
/*index by XContentBuilder*/
    public static IndexResponse indexDocByXContentBuilder(IndexRequestBuilder indexRequestBuilder, XContentBuilder xContentBuilder) {

        indexRequestBuilder.setSource(xContentBuilder);

        IndexResponse response = indexRequestBuilder.get();
        return response;

    }

  /*自己指定id*/
    public static IndexRequestBuilder getIndexRequestBuilder(Client client, String index, String type, String id) {
        IndexRequestBuilder indexRequestBuilder=client.prepareIndex(index, type, id);
        client.close();
        return indexRequestBuilder;
    }

/*自己不指定,es生成id*/
    public static IndexRequestBuilder getIndexRequestBuilder(Client client, String index, String type) {
        IndexRequestBuilder indexRequestBuilder=client.prepareIndex(index, type);
        client.close();
        return indexRequestBuilder;
    }
/*Get api*/
    public static GetResponse getApi(Client client, String index, String type, String id) {

        GetResponse response = client.prepareGet(index, type, id)
                .setOperationThreaded(false)
                .get();
        System.out.println(response.getSourceAsString());
        client.close();
        return response;
    }
/*deleta api*/
    public static DeleteResponse deleteApi(Client client, String index, String type, String id) {

        DeleteResponse response = client.prepareDelete(index, type, id)
                .get();
        System.out.println(response.toString());
        client.close();
        return response;
    }

    public static BulkIndexByScrollResponse deleteByQueryApi(Client client, String index) {

        BulkIndexByScrollResponse response =
                DeleteByQueryAction.INSTANCE.newRequestBuilder(client)
                        .filter(QueryBuilders.matchQuery("name", "Sherry"))
                        .source(index)
                        .get();

        long deleted = response.getDeleted();
        System.out.println(deleted);

        System.out.println(response.toString());
        client.close();
        return response;
    }

    public static void asynDeleteByQueryApi(Client client, String index) {


        DeleteByQueryAction.INSTANCE.newRequestBuilder(client)
                .filter(QueryBuilders.matchQuery("gender", "male"))
                .source("persons")
                .execute(new ActionListener<BulkIndexByScrollResponse>() {
                    @Override
                    public void onResponse(BulkIndexByScrollResponse response) {
                        long deleted = response.getDeleted();
                        System.out.println(deleted);
                    }

                    @Override
                    public void onFailure(Exception e) {
                        // Handle the exception
                    }
                });
    }

    public static void updateByUpdateRequest(Client client, String index, String type, String id) throws ExecutionException, InterruptedException, IOException {
        UpdateRequest updateRequest = new UpdateRequest();
        updateRequest.index(index);
        updateRequest.type(type);
        updateRequest.id(id);
        updateRequest.doc(jsonBuilder()
                .startObject()
                .field("name", "Wangermazi")
                .endObject());
        client.update(updateRequest).get();
        client.close();
    }

    public static void multiGet(Client client) throws ExecutionException, InterruptedException, IOException {
        MultiGetResponse multiGetItemResponses = client.prepareMultiGet()
                .add("football", "player", "1")
                .add("football", "player", "2", "3", "4")
                .add("football", "player", "test")
                .get();

        for (MultiGetItemResponse itemResponse : multiGetItemResponses) {
            GetResponse response = itemResponse.getResponse();
            if (response.isExists()) {
                String json = response.getSourceAsString();
                System.out.println(json);
            }
        }
        client.close();
    }
    public static void bulkApi(Client client) throws ExecutionException, InterruptedException, IOException {
        BulkRequestBuilder bulkRequest = client.prepareBulk();

// either use client#prepare, or use Requests# to directly build index/delete requests
        bulkRequest.add(client.prepareIndex("twitter", "tweet", "1")
                .setSource(jsonBuilder()
                        .startObject()
                        .field("user", "kimchy")
                        .field("postDate", new Date())
                        .field("message", "trying out Elasticsearch")
                        .endObject()
                )
        );

        bulkRequest.add(client.prepareIndex("twitter", "tweet", "2")
                .setSource(jsonBuilder()
                        .startObject()
                        .field("user", "kimchy")
                        .field("postDate", new Date())
                        .field("message", "another post")
                        .endObject()
                )
        );

        BulkResponse bulkResponse = bulkRequest.get();
        if (bulkResponse.hasFailures()) {
            // process failures by iterating through each bulk response item
        }
        client.close();
    }
    public static void bulkProcessor(Client client) throws ExecutionException, InterruptedException, IOException {
        BulkProcessor bulkProcessor = BulkProcessor.builder(
                client,
                new BulkProcessor.Listener() {
                    @Override
                    public void beforeBulk(long executionId,
                                           BulkRequest request) {  }

                    @Override
                    public void afterBulk(long executionId,
                                          BulkRequest request,
                                          BulkResponse response) {  }

                    @Override
                    public void afterBulk(long executionId,
                                          BulkRequest request,
                                          Throwable failure) {  }
                })
                .setBulkActions(10000)
                .setBulkSize(new ByteSizeValue(5, ByteSizeUnit.MB))
                .setFlushInterval(TimeValue.timeValueSeconds(5))
                .setConcurrentRequests(1)
                .setBackoffPolicy(
                        BackoffPolicy.exponentialBackoff(TimeValue.timeValueMillis(100), 3))
                .build();
        client.close();
    }


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值