|
@@ -5,8 +5,6 @@ import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
|
|
|
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
|
|
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
|
|
|
import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
|
|
import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
|
|
|
import org.elasticsearch.action.bulk.BulkRequest;
|
|
import org.elasticsearch.action.bulk.BulkRequest;
|
|
|
-import org.elasticsearch.action.bulk.BulkResponse;
|
|
|
|
|
-import org.elasticsearch.action.delete.DeleteRequest;
|
|
|
|
|
import org.elasticsearch.action.search.ClearScrollRequest;
|
|
import org.elasticsearch.action.search.ClearScrollRequest;
|
|
|
import org.elasticsearch.action.search.SearchRequest;
|
|
import org.elasticsearch.action.search.SearchRequest;
|
|
|
import org.elasticsearch.action.search.SearchResponse;
|
|
import org.elasticsearch.action.search.SearchResponse;
|
|
@@ -19,11 +17,9 @@ import org.elasticsearch.common.settings.Settings;
|
|
|
import org.elasticsearch.common.xcontent.XContentType;
|
|
import org.elasticsearch.common.xcontent.XContentType;
|
|
|
import org.elasticsearch.search.SearchHits;
|
|
import org.elasticsearch.search.SearchHits;
|
|
|
import org.elasticsearch.search.aggregations.Aggregations;
|
|
import org.elasticsearch.search.aggregations.Aggregations;
|
|
|
-import org.springframework.util.CollectionUtils;
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
|
import java.util.Arrays;
|
|
import java.util.Arrays;
|
|
|
-import java.util.List;
|
|
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* @author yuzhongchuan
|
|
* @author yuzhongchuan
|
|
@@ -49,28 +45,29 @@ public class EsUtils {
|
|
|
* @param idxSql
|
|
* @param idxSql
|
|
|
* @throws Exception
|
|
* @throws Exception
|
|
|
*/
|
|
*/
|
|
|
- public static void createIndex(String idxName,String idxSql){
|
|
|
|
|
|
|
+ public static void createIndex(String idxName, String idxSql) {
|
|
|
if (!indexExist(idxName)) {
|
|
if (!indexExist(idxName)) {
|
|
|
- log.error(" idxName={} 已经存在,idxSql={}",idxName,idxSql);
|
|
|
|
|
|
|
+ log.error(" idxName={} 已经存在,idxSql={}", idxName, idxSql);
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
- log.info(" idxName={} 创建索引,idxSql={}",idxName,idxSql);
|
|
|
|
|
|
|
+ log.info(" idxName={} 创建索引,idxSql={}", idxName, idxSql);
|
|
|
CreateIndexRequest request = new CreateIndexRequest(idxName);
|
|
CreateIndexRequest request = new CreateIndexRequest(idxName);
|
|
|
buildSetting(request);
|
|
buildSetting(request);
|
|
|
- request.mapping("_doc",idxSql, XContentType.JSON);
|
|
|
|
|
|
|
+ request.mapping("_doc", idxSql, XContentType.JSON);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 删除索引
|
|
* 删除索引
|
|
|
|
|
+ *
|
|
|
* @param idxName 索引名称
|
|
* @param idxName 索引名称
|
|
|
*/
|
|
*/
|
|
|
- public static void deleteIndex(String idxName){
|
|
|
|
|
|
|
+ public static void deleteIndex(String idxName) {
|
|
|
if (!indexExist(idxName)) {
|
|
if (!indexExist(idxName)) {
|
|
|
- log.error(" idxName={} 索引不存在",idxName);
|
|
|
|
|
|
|
+ log.error(" idxName={} 索引不存在", idxName);
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
try {
|
|
try {
|
|
|
- esUtils.client.indices().delete(new DeleteIndexRequest(idxName),RequestOptions.DEFAULT);
|
|
|
|
|
|
|
+ esUtils.client.indices().delete(new DeleteIndexRequest(idxName), RequestOptions.DEFAULT);
|
|
|
} catch (IOException e) {
|
|
} catch (IOException e) {
|
|
|
log.error("ES deleteIndex异常", e);
|
|
log.error("ES deleteIndex异常", e);
|
|
|
}
|
|
}
|
|
@@ -78,15 +75,17 @@ public class EsUtils {
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 设置分片
|
|
* 设置分片
|
|
|
|
|
+ *
|
|
|
* @param request
|
|
* @param request
|
|
|
*/
|
|
*/
|
|
|
- public static void buildSetting(CreateIndexRequest request){
|
|
|
|
|
- request.settings(Settings.builder().put("index.number_of_shards",9)
|
|
|
|
|
- .put("index.number_of_replicas",1));
|
|
|
|
|
|
|
+ public static void buildSetting(CreateIndexRequest request) {
|
|
|
|
|
+ request.settings(Settings.builder().put("index.number_of_shards", 9)
|
|
|
|
|
+ .put("index.number_of_replicas", 1));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 断某个index是否存在
|
|
* 断某个index是否存在
|
|
|
|
|
+ *
|
|
|
* @param idxName
|
|
* @param idxName
|
|
|
* @return
|
|
* @return
|
|
|
*/
|
|
*/
|
|
@@ -98,7 +97,7 @@ public class EsUtils {
|
|
|
request.indices(idxName);
|
|
request.indices(idxName);
|
|
|
request.indicesOptions(IndicesOptions.lenientExpandOpen());
|
|
request.indicesOptions(IndicesOptions.lenientExpandOpen());
|
|
|
try {
|
|
try {
|
|
|
- return esUtils.client.indices().exists(request,RequestOptions.DEFAULT);
|
|
|
|
|
|
|
+ return esUtils.client.indices().exists(request, RequestOptions.DEFAULT);
|
|
|
} catch (IOException e) {
|
|
} catch (IOException e) {
|
|
|
log.error("ES indexExist查询SearchRequest异常", e);
|
|
log.error("ES indexExist查询SearchRequest异常", e);
|
|
|
throw new RuntimeException(e);
|
|
throw new RuntimeException(e);
|
|
@@ -164,7 +163,7 @@ public class EsUtils {
|
|
|
* @param countRequest
|
|
* @param countRequest
|
|
|
* @return
|
|
* @return
|
|
|
*/
|
|
*/
|
|
|
- public static long count(CountRequest countRequest ){
|
|
|
|
|
|
|
+ public static long count(CountRequest countRequest) {
|
|
|
try {
|
|
try {
|
|
|
|
|
|
|
|
log.info("开始ES查询indexName:{}的count", Arrays.toString(countRequest.indices()));
|
|
log.info("开始ES查询indexName:{}的count", Arrays.toString(countRequest.indices()));
|