资源简介

改类封装elasticsearch5.x以rest风格操作的索引的部分api。类中有详细的注释,可供参考学习。希望能帮到有需要的同学。

资源截图

代码片段和文件信息

package com.minstone.statisplatform.manager.elasticsearch;

import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.StatusLine;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.entity.ContentType;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.apache.http.impl.nio.reactor.IOReactorConfig;
import org.apache.http.nio.entity.NStringEntity;
import org.elasticsearch.action.DocWriteRequest;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.rest.RestStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;

import com.alibaba.fastjson.JSON;
import com.minstone.statisplatform.common.exception.ESIndexException;
import com.minstone.statisplatform.manager.statis.model.ArchiveMapper;
import com.minstone.statisplatform.manager.statis.model.ArchiveMapperVo;

/**
 * ClassName: ESTransportClient 
 * @Description: Elasticsearch操作类
 * 索引操作API:
 * low level https://www.elastic.co/guide/en/elasticsearch/client/java-rest/5.6/java-rest-low.html
 * high level https://www.elastic.co/guide/en/elasticsearch/client/java-rest/5.6/java-rest-high.html
 * @author JornTang
 * @date 2017年12月21日
 */
public class ESRestClient {
private static Logger log = LoggerFactory.getLogger(ESRestClient.class);
private static RestClient lowLevelRestClient = null;
private static RestHighLevelClient highLevelRestClient = null;
/**
 * @Description: 初始化  
 * @return void  
 * @throws
 * @author JornTang
 * @date 2017年12月23日
 */
public void init(){
    RestClientBuilder builder = RestClient.builder(new HttpHost(“127.0.0.1“ 9200));
    builder.setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() {  
            @Override  
            public RequestConfig.Builder customizeRequestConfig(RequestConfig.Builder requestConfigBuilder) {  
                requestConfigBuilder.setConnectTimeout(10000);  
                requestConfigBuilder.setSocketTimeout(30000);  
                requestConfigBuilder.setConnectionRequestTimeout(10000);  
                return requestConfigBuilder;  
            }  
        }); 
    
     builder.setHttpClientConfigCallback(new RestClientBuilder.Http

评论

共有 条评论