|
@@ -3,9 +3,7 @@ package com.sckw.elasticsearch.service.es;
|
|
|
import com.alibaba.fastjson.JSON;
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
import com.alibaba.fastjson.JSONArray;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
-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.bulk.BulkRequest;
|
|
import org.elasticsearch.action.bulk.BulkRequest;
|
|
|
import org.elasticsearch.action.bulk.BulkResponse;
|
|
import org.elasticsearch.action.bulk.BulkResponse;
|
|
|
import org.elasticsearch.action.delete.DeleteRequest;
|
|
import org.elasticsearch.action.delete.DeleteRequest;
|
|
@@ -13,12 +11,13 @@ import org.elasticsearch.action.index.IndexRequest;
|
|
|
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;
|
|
|
-import org.elasticsearch.action.support.IndicesOptions;
|
|
|
|
|
import org.elasticsearch.action.update.UpdateRequest;
|
|
import org.elasticsearch.action.update.UpdateRequest;
|
|
|
import org.elasticsearch.client.RequestOptions;
|
|
import org.elasticsearch.client.RequestOptions;
|
|
|
import org.elasticsearch.client.RestHighLevelClient;
|
|
import org.elasticsearch.client.RestHighLevelClient;
|
|
|
import org.elasticsearch.client.core.CountRequest;
|
|
import org.elasticsearch.client.core.CountRequest;
|
|
|
import org.elasticsearch.client.core.CountResponse;
|
|
import org.elasticsearch.client.core.CountResponse;
|
|
|
|
|
+import org.elasticsearch.client.indices.CreateIndexRequest;
|
|
|
|
|
+import org.elasticsearch.client.indices.GetIndexRequest;
|
|
|
import org.elasticsearch.common.settings.Settings;
|
|
import org.elasticsearch.common.settings.Settings;
|
|
|
import org.elasticsearch.common.xcontent.XContentType;
|
|
import org.elasticsearch.common.xcontent.XContentType;
|
|
|
import org.elasticsearch.index.query.TermQueryBuilder;
|
|
import org.elasticsearch.index.query.TermQueryBuilder;
|
|
@@ -60,14 +59,14 @@ public class EsUtils {
|
|
|
* @param idxSql
|
|
* @param idxSql
|
|
|
*/
|
|
*/
|
|
|
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(idxSql, XContentType.JSON);
|
|
|
try {
|
|
try {
|
|
|
esUtils.client.indices().create(request, RequestOptions.DEFAULT);
|
|
esUtils.client.indices().create(request, RequestOptions.DEFAULT);
|
|
|
} catch (IOException e) {
|
|
} catch (IOException e) {
|
|
@@ -109,16 +108,11 @@ public class EsUtils {
|
|
|
* @return
|
|
* @return
|
|
|
*/
|
|
*/
|
|
|
public static boolean indexExist(String idxName) {
|
|
public static boolean indexExist(String idxName) {
|
|
|
- GetIndexRequest request = new GetIndexRequest();
|
|
|
|
|
- request.local(false);
|
|
|
|
|
- request.humanReadable(true);
|
|
|
|
|
- request.includeDefaults(false);
|
|
|
|
|
- request.indices(idxName);
|
|
|
|
|
- request.indicesOptions(IndicesOptions.lenientExpandOpen());
|
|
|
|
|
|
|
+ GetIndexRequest request = new GetIndexRequest(idxName);
|
|
|
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查询异常", e);
|
|
|
throw new RuntimeException(e);
|
|
throw new RuntimeException(e);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|