|
@ -8,6 +8,9 @@ import com.alibaba.druid.sql.parser.Token;
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
import com.yihu.ehr.elasticsearch.ElasticSearchPool;
|
|
|
import io.searchbox.client.JestClient;
|
|
|
import io.searchbox.client.JestClientFactory;
|
|
|
import io.searchbox.client.config.HttpClientConfig;
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
import org.elasticsearch.action.bulk.BulkRequestBuilder;
|
|
|
import org.elasticsearch.action.bulk.BulkResponse;
|
|
@ -17,6 +20,8 @@ import org.elasticsearch.action.search.SearchRequestBuilder;
|
|
|
import org.elasticsearch.action.search.SearchResponse;
|
|
|
import org.elasticsearch.client.Client;
|
|
|
import org.elasticsearch.client.transport.TransportClient;
|
|
|
import org.elasticsearch.common.settings.Settings;
|
|
|
import org.elasticsearch.common.transport.InetSocketTransportAddress;
|
|
|
import org.elasticsearch.index.query.BoolQueryBuilder;
|
|
|
import org.elasticsearch.search.SearchHit;
|
|
|
import org.elasticsearch.search.SearchHits;
|
|
@ -39,10 +44,13 @@ import org.nlpcn.es4sql.query.SqlElasticSearchRequestBuilder;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import java.net.InetAddress;
|
|
|
import java.util.*;
|
|
|
|
|
|
/**
|
|
|
* Created by janseny on 2017/7/21.
|
|
|
*
|
|
|
* @author janseny
|
|
|
* @date 2017/7/21
|
|
|
*/
|
|
|
@Component
|
|
|
public class ElasticsearchUtil {
|
|
@ -52,6 +60,38 @@ public class ElasticsearchUtil {
|
|
|
@Autowired
|
|
|
private ElasticSearchPool elasticSearchPool;
|
|
|
|
|
|
/**
|
|
|
* @param host "localhost"
|
|
|
* @param port 9200
|
|
|
* @return
|
|
|
*/
|
|
|
public JestClient getJestClient(String host, Integer port) {
|
|
|
String hostAddress = "http://" + host + ":" + port;
|
|
|
JestClientFactory factory = new JestClientFactory();
|
|
|
factory.setHttpClientConfig(new HttpClientConfig
|
|
|
.Builder(hostAddress)
|
|
|
.multiThreaded(true)
|
|
|
//.discoveryEnabled(true)
|
|
|
.readTimeout(60000)//30秒 -60s
|
|
|
.build());
|
|
|
return factory.getObject();
|
|
|
}
|
|
|
|
|
|
public Client getClient(String host, Integer port, String clusterName) {
|
|
|
try {
|
|
|
Settings settings = Settings.settingsBuilder()
|
|
|
.put("cluster.name", org.springframework.util.StringUtils.isEmpty(clusterName) ? "elasticsearch" : clusterName)
|
|
|
.put("client.transport.sniff", false)
|
|
|
.build();
|
|
|
Client client = TransportClient.builder().settings(settings).build()
|
|
|
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(host), port));
|
|
|
return client;
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* @param boolQueryBuilder 查询参数 build
|