15902849627 2 лет назад
Родитель
Сommit
070c52cb34

+ 1 - 1
sckw-common/sckw-common-elasticsearch/pom.xml

@@ -18,7 +18,7 @@
         <maven.compiler.target>17</maven.compiler.target>
         <maven.compiler.target>17</maven.compiler.target>
         <maven.compiler.compilerVersion>17</maven.compiler.compilerVersion>
         <maven.compiler.compilerVersion>17</maven.compiler.compilerVersion>
         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
-        <elasticsearch.version>7.9.3</elasticsearch.version>
+        <elasticsearch.version>7.14.0</elasticsearch.version>
     </properties>
     </properties>
 
 
 
 

+ 6 - 12
sckw-common/sckw-common-elasticsearch/src/main/java/com/sckw/elasticsearch/service/es/EsUtils.java

@@ -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);
         }
         }
     }
     }

+ 5 - 5
sckw-modules/sckw-example/src/main/java/com/sckw/example/controller/EsController.java

@@ -39,13 +39,13 @@ public class EsController {
         System.out.println("mapping is as follows: ");
         System.out.println("mapping is as follows: ");
         System.out.println(mappings);
         System.out.println(mappings);
 
 
-        EsUtils.createIndex("employees", mappings);
+        EsUtils.createIndex("test", mappings);
     }
     }
 
 
 
 
     @GetMapping(value = "/deleteIndex")
     @GetMapping(value = "/deleteIndex")
-    public void deleteIndex(@RequestParam(value = "indexName") String indexName) {
-        EsUtils.deleteIndex(indexName);
+    public void deleteIndex() {
+        EsUtils.deleteIndex("test");
     }
     }
 
 
     @GetMapping(value = "/bulkSave")
     @GetMapping(value = "/bulkSave")
@@ -55,13 +55,13 @@ public class EsController {
         Employees jason = Employees.builder().id("3").name("Jason").build();
         Employees jason = Employees.builder().id("3").name("Jason").build();
         list.add(nancy);
         list.add(nancy);
         list.add(jason);
         list.add(jason);
-        EsUtils.bulkSave("employees", list);
+        EsUtils.bulkSave("test", list);
 
 
     }
     }
 
 
     @GetMapping(value = "/search")
     @GetMapping(value = "/search")
     public void search() {
     public void search() {
-        SearchRequest request = new SearchRequest("employees");
+        SearchRequest request = new SearchRequest("test");
         SearchSourceBuilder builder = new SearchSourceBuilder();
         SearchSourceBuilder builder = new SearchSourceBuilder();
         request.source(builder);
         request.source(builder);
         SearchHits hits = EsUtils.boolSearch(request);
         SearchHits hits = EsUtils.boolSearch(request);