Kaynağa Gözat

优化ES文档索引名称获取逻辑

woody 2 ay önce
ebeveyn
işleme
f5db310eda

+ 12 - 14
framework-elasticsearch/src/main/java/com/chelvc/framework/elasticsearch/DefaultElasticsearchHandler.java

@@ -48,6 +48,7 @@ import co.elastic.clients.transport.endpoints.BooleanResponse;
 import co.elastic.clients.util.ObjectBuilder;
 import com.chelvc.framework.common.util.ObjectUtils;
 import com.chelvc.framework.common.util.StringUtils;
+import com.chelvc.framework.elasticsearch.annotation.Index;
 import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
@@ -61,6 +62,7 @@ import lombok.NonNull;
  */
 public class DefaultElasticsearchHandler implements ElasticsearchHandler {
     private final ElasticsearchClient client;
+    private final Map<Class<?>, String> indexes = Maps.newConcurrentMap();
 
     public DefaultElasticsearchHandler(@NonNull ElasticsearchTransport transport) {
         this.client = new ElasticsearchClient(transport);
@@ -72,8 +74,13 @@ public class DefaultElasticsearchHandler implements ElasticsearchHandler {
      * @param model 文档模型
      * @return 索引名称
      */
-    protected String getIndex(@NonNull Class<?> model) {
-        return StringUtils.hump2underscore(model.getSimpleName());
+    private String getIndex(Class<?> model) {
+        String name = this.indexes.get(model);
+        return name != null ? name : this.indexes.computeIfAbsent(model, k -> {
+            Index index = model.getAnnotation(Index.class);
+            String value = ObjectUtils.ifNull(index, Index::value);
+            return StringUtils.notEmpty(value) ? value : StringUtils.hump2underscore(model.getSimpleName());
+        });
     }
 
     @Override
@@ -95,12 +102,9 @@ public class DefaultElasticsearchHandler implements ElasticsearchHandler {
 
     @Override
     public <T extends Document<?>> BulkResponse create(@NonNull Collection<T> documents) {
-        String index = null;
         List<BulkOperation> operations = Lists.newArrayListWithCapacity(documents.size());
         for (T document : documents) {
-            if (index == null) {
-                index = this.getIndex(document.getClass());
-            }
+            String index = this.getIndex(document.getClass());
             String id = ObjectUtils.ifNull(document.getId(), String::valueOf);
             CreateOperation<?> create = new CreateOperation.Builder<>().index(index).id(id).document(document).build();
             BulkOperation operation = new BulkOperation.Builder().create(create).build();
@@ -159,12 +163,9 @@ public class DefaultElasticsearchHandler implements ElasticsearchHandler {
 
     @Override
     public <T extends Document<?>> BulkResponse update(@NonNull Collection<T> documents) {
-        String index = null;
         List<BulkOperation> operations = Lists.newArrayListWithCapacity(documents.size());
         for (T document : documents) {
-            if (index == null) {
-                index = this.getIndex(document.getClass());
-            }
+            String index = this.getIndex(document.getClass());
             String id = ObjectUtils.ifNull(document.getId(), String::valueOf);
             UpdateOperation<T, Object> update = new UpdateOperation.Builder<T, Object>().index(index).id(id)
                     .action(a -> a.doc(document)).retryOnConflict(3).build();
@@ -284,12 +285,9 @@ public class DefaultElasticsearchHandler implements ElasticsearchHandler {
 
     @Override
     public <T extends Document<?>> BulkResponse index(@NonNull Collection<T> documents, Long primary, Long sequence) {
-        String index = null;
         List<BulkOperation> operations = Lists.newArrayListWithCapacity(documents.size());
         for (T document : documents) {
-            if (index == null) {
-                index = this.getIndex(document.getClass());
-            }
+            String index = this.getIndex(document.getClass());
             String id = ObjectUtils.ifNull(document.getId(), String::valueOf);
             IndexOperation<?> operation = new IndexOperation.Builder<>().index(index).id(id).document(document)
                     .ifPrimaryTerm(primary).ifSeqNo(sequence).build();

+ 25 - 0
framework-elasticsearch/src/main/java/com/chelvc/framework/elasticsearch/annotation/Index.java

@@ -0,0 +1,25 @@
+package com.chelvc.framework.elasticsearch.annotation;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * ES文档索引注解
+ *
+ * @author Woody
+ * @date 2024/4/3
+ */
+@Documented
+@Target(ElementType.TYPE)
+@Retention(RetentionPolicy.RUNTIME)
+public @interface Index {
+    /**
+     * 获取索引名称
+     *
+     * @return 索引名称
+     */
+    String value();
+}