elasticSearch官方文档地址 https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high.html
因为官方后续不会支持transportClient,所以,本篇博客采用的是RestHighLevelClient客户端。版本是6.4.2。
RestHighLevelClient支持Restful风格。
代码地址:https://github.com/yywa/practice.git
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.sun.glass.ui.Size;
import com.sun.org.apache.bcel.internal.generic.GETFIELD;
import org.apache.http.HttpHost;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.reflection.MetaObject;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
import org.elasticsearch.action.admin.indices.get.GetIndexResponse;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
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.ClearScrollRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchScrollRequest;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.*;
import org.elasticsearch.search.Scroll;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.metrics.max.Max;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import sun.rmi.runtime.Log;
import javax.xml.crypto.Data;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;
/**
* @author yyw
*/
public class ElasticSearchOperation {
private static String host = "127.0.0.1";
private static Integer port = 9200;
private static final Integer MAX = 10000;
private static ThreadLocal<Map<String, String>> scrollThreadLocal = new ThreadLocal<Map<String, String>>() {
@Override
protected Map<String, String> initialValue() {
return new HashMap();
}
};
public static RestHighLevelClient client;
/**
* 建立客户端链接
*/
public static void createConnection() {
try {
HttpHost[] httpHosts = new HttpHost[1];
httpHosts[0] = new HttpHost(host, port);
client = new RestHighLevelClient(RestClient.builder(httpHosts));
System.out.println("成功");
} catch (Exception e) {
e.printStackTrace();
System.out.println("失败");
}
}
/**
* 验证是否有客户端链接
*/
public static void validateClient() {
if (client == null) {
createConnection();
}
}
/**
* 根据条件获取模糊匹配的索引
*
* @param condition 条件
* @return 数组
*/
public String[] getIndices(String condition) {
validateClient();
try {
GetIndexRequest getIndexRequest = new GetIndexRequest();
getIndexRequest.indices(condition + "*");
GetIndexResponse response = client.indices().get(getIndexRequest, RequestOptions.DEFAULT);
return response.getIndices();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 创建索引
*
* @param indexName index
* @return 是否成功
*/
public static boolean createIndex(String indexName) {
validateClient();
try {
CreateIndexRequest createIndexRequest = new CreateIndexRequest(indexName);
client.indices().create(createIndexRequest, RequestOptions.DEFAULT);
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
/**
* 验证索引是否存在。
*
* @param indexName index
* @return 是否成功
*/
public static boolean indexExist(String indexName) {
boolean flag = false;
validateClient();
try {
GetIndexRequest getIndexRequest = new GetIndexRequest();
getIndexRequest.indices(indexName);
flag = client.indices().exists(getIndexRequest, RequestOptions.DEFAULT);
} catch (IOException e) {
e.printStackTrace();
}
return flag;
}
/**
* 删除index
*
* @param indexName index
* @return 是否成功
*/
public static boolean deleteIndex(String indexName) {
boolean flag = false;
validat