package com.thinkgem.jeesite.util.elasticsearch;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.linkedList;
import java.util.List;
import java.util.Map;
import com.thinkgem.jeesite.common.mapper.JsonMapper;
import com.thinkgem.jeesite.modules.lore.entity.Knowledge;
import com.thinkgem.jeesite.modules.sys.utils.HttpClient3;
import com.thinkgem.jeesite.modules.sys.utils.SysUtils;
import com.thinkgem.jeesite.util.elasticsearch.bean.GroupByListItem;
import com.thinkgem.jeesite.util.elasticsearch.jsonFormat.DefaultJsonFormat;
import com.thinkgem.jeesite.util.elasticsearch.jsonFormat.JsonFormatInterface;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.apache.http.util.EntityUtils;
import org.elasticsearch.action.DocWriteResponse.Result;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
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.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.*;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.common.unit.Timevalue;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.BucketOrder;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.elasticsearch.search.sort.SortBuilder;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.junit.Test;
public class ElasticSearchUtil {
private RestHighLevelClient restHighLevelClient;
private RestClient restClient;
private String hostname = "127.0.0.1";
private int port = 9200;
private String scheme = "http";
private String username = ""; //elasticsearch链接的用户名,如果es本身没设置使用用户名密码的,那这里就不用设置
private String password = ""; //elasticsearch链接的密码,如果es本身没设置使用用户名密码的,那这里就不用设置
private JsonFormatInterface jsonFormatInterface; //JSON格式化接口。默认使用 DefaultJsonFormat();
private HttpHost[] httpHosts;
public Map>> cacheMap;
public int cacheMaxNumber = 100; //如果使用缓存,这里是缓存中的最大条数,超过这些条就会自动打包提交
public ElasticSearchUtil(HttpHost... httpHosts) {
this.httpHosts = httpHosts;
cacheMap = new HashMap>>();
jsonFormatInterface = new DefaultJsonFormat();
}
public ElasticSearchUtil(String hostname) {
this.hostname = hostname;
cacheMap = new HashMap>>();
jsonFormatInterface = new DefaultJsonFormat();
}
public ElasticSearchUtil(String hostname, int port, String scheme) {
this.hostname = hostname;
this.port = port;
this.scheme = scheme;
cacheMap = new HashMap>>();
jsonFormatInterface = new DefaultJsonFormat();
}
public ElasticSearchUtil(String hostname, int port, String scheme, String username, String password) {
this.hostname = hostname;
this.port = port;
this.scheme = scheme;
if(username != null && username.length() > 0) {
this.username = username;
}
if(password != null && password.length() > 0) {
this.password = password;
}
cacheMap = new HashMap>>();
jsonFormatInterface = new DefaultJsonFormat();
}
public void setUsernameAndPassword(String username, String password) {
if(username != null && username.length() > 0) {
this.username = username;
}
if(password != null && password.length() > 0) {
this.password = password;
}
}
public void setCacheMaxNumber(int cacheMaxNumber) {
this.cacheMaxNumber = cacheMaxNumber;
}
public void setJsonFormatInterface(JsonFormatInterface jsonFormatInterface) {
this.jsonFormatInterface = jsonFormatInterface;
}
public RestHighLevelClient getRestHighLevelClient(){
if(this.restHighLevelClient == null){
if(this.httpHosts == null){
//没有直接传入 httpshosts,那么就是使用单个的
HttpHost httpHost = new HttpHost(this.hostname, this.port, this.scheme);
this.httpHosts = new HttpHost[1];
this.httpHosts[0] = httpHost;
}
if(this.username.length() > 0 && this.password.length() > 0) {
//当前elasticsearch 设置了连接的用户名密码
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY,new UsernamePasswordCredentials(username, password)); //es账号密码(默认用户名为elastic)
this.restHighLevelClient =new RestHighLevelClient(
RestClient.builder(this.httpHosts).setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
httpClientBuilder.disableAuthCaching();
return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
}
})
);
}else{
this.restHighLevelClient = new RestHighLevelClient(RestClient.builder(this.httpHosts));
}
}
return this.restHighLevelClient;
}
public RestClient getRestClient(){
if(this.restClient == null){
if(this.httpHosts == null){
//没有直接传入 httpshosts,那么就是使用单个的
HttpHost httpHost = new HttpHost(this.hostname, this.port, this.scheme);
this.httpHosts = new HttpHost[1];
this.httpHosts[0] = httpHost;
}
if(this.username.length() > 0 && this.password.length() > 0) {
//当前elasticsearch 设置了连接的用户名密码
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY,new UsernamePasswordCredentials(username, password)); //es账号密码(默认用户名为elastic)
this.restClient = RestClient.builder(this.httpHosts).setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
httpClientBuilder.disableAuthCaching();
return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
}
}).build();
}
this.restClient = RestClient.builder(this.httpHosts).build();
}
return this.restClient;
}
public static RestHighLevelClient getStaticRestHighLevelClient(){
String hostName = SysUtils.getSysConfigValue("ElasticSearchIp","90");
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(RestClient.builder("172.0.0.1"));
return restHighLevelClient;
}
public synchronized void cache(Map params, String indexName){
List