Jelajahi Sumber

fix #647 change to alias

Shinsuke Sugaya 9 tahun lalu
induk
melakukan
6e8f2119c9

+ 53 - 16
src/main/java/org/codelibs/fess/es/client/FessEsClient.java

@@ -22,9 +22,11 @@ import java.io.File;
 import java.net.InetAddress;
 import java.net.InetAddress;
 import java.net.UnknownHostException;
 import java.net.UnknownHostException;
 import java.nio.charset.StandardCharsets;
 import java.nio.charset.StandardCharsets;
+import java.text.SimpleDateFormat;
 import java.util.ArrayList;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.Collections;
+import java.util.Date;
 import java.util.HashMap;
 import java.util.HashMap;
 import java.util.List;
 import java.util.List;
 import java.util.Map;
 import java.util.Map;
@@ -71,6 +73,7 @@ import org.elasticsearch.action.admin.indices.alias.IndicesAliasesResponse;
 import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
 import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
 import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse;
 import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse;
 import org.elasticsearch.action.admin.indices.flush.FlushResponse;
 import org.elasticsearch.action.admin.indices.flush.FlushResponse;
+import org.elasticsearch.action.admin.indices.get.GetIndexResponse;
 import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse;
 import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse;
 import org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse;
 import org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse;
 import org.elasticsearch.action.admin.indices.refresh.RefreshResponse;
 import org.elasticsearch.action.admin.indices.refresh.RefreshResponse;
@@ -309,9 +312,16 @@ public class FessEsClient implements Client {
                 final String configIndex = values[0];
                 final String configIndex = values[0];
                 final String configType = values[1];
                 final String configType = values[1];
                 boolean exists = false;
                 boolean exists = false;
+                final String indexName;
+                final boolean isFessIndex = configIndex.equals("fess");
+                if (isFessIndex) {
+                    indexName = fessConfig.getIndexDocumentUpdateIndex();
+                } else {
+                    indexName = configIndex;
+                }
                 try {
                 try {
                     final IndicesExistsResponse response =
                     final IndicesExistsResponse response =
-                            client.admin().indices().prepareExists(configIndex).execute().actionGet(fessConfig.getIndexSearchTimeout());
+                            client.admin().indices().prepareExists(indexName).execute().actionGet(fessConfig.getIndexSearchTimeout());
                     exists = response.isExists();
                     exists = response.isExists();
                 } catch (final Exception e) {
                 } catch (final Exception e) {
                     // ignore
                     // ignore
@@ -347,18 +357,24 @@ public class FessEsClient implements Client {
                     logger.warn("Failed to flush config files.", e);
                     logger.warn("Failed to flush config files.", e);
                 }
                 }
 
 
+                final String createdIndexName;
+                if (isFessIndex) {
+                    createdIndexName = generateNewIndexName(configIndex);
+                } else {
+                    createdIndexName = configIndex;
+                }
                 final String indexConfigFile = indexConfigPath + "/" + configIndex + ".json";
                 final String indexConfigFile = indexConfigPath + "/" + configIndex + ".json";
                 try {
                 try {
                     String source = FileUtil.readUTF8(indexConfigFile);
                     String source = FileUtil.readUTF8(indexConfigFile);
                     final String dictionaryPath = System.getProperty("fess.dictionary.path", StringUtil.EMPTY);
                     final String dictionaryPath = System.getProperty("fess.dictionary.path", StringUtil.EMPTY);
                     source = source.replaceAll(Pattern.quote("${fess.dictionary.path}"), dictionaryPath);
                     source = source.replaceAll(Pattern.quote("${fess.dictionary.path}"), dictionaryPath);
                     final CreateIndexResponse indexResponse =
                     final CreateIndexResponse indexResponse =
-                            client.admin().indices().prepareCreate(configIndex).setSource(source).execute()
+                            client.admin().indices().prepareCreate(createdIndexName).setSource(source).execute()
                                     .actionGet(fessConfig.getIndexIndicesTimeout());
                                     .actionGet(fessConfig.getIndexIndicesTimeout());
                     if (indexResponse.isAcknowledged()) {
                     if (indexResponse.isAcknowledged()) {
-                        logger.info("Created " + configIndex + " index.");
+                        logger.info("Created " + createdIndexName + " index.");
                     } else if (logger.isDebugEnabled()) {
                     } else if (logger.isDebugEnabled()) {
-                        logger.debug("Failed to create " + configIndex + " index.");
+                        logger.debug("Failed to create " + createdIndexName + " index.");
                     }
                     }
                 } catch (final IndexAlreadyExistsException e) {
                 } catch (final IndexAlreadyExistsException e) {
                     // ignore
                     // ignore
@@ -374,14 +390,17 @@ public class FessEsClient implements Client {
                         stream(aliasConfigDir.listFiles((dir, name) -> name.endsWith(".json"))).of(
                         stream(aliasConfigDir.listFiles((dir, name) -> name.endsWith(".json"))).of(
                                 stream -> stream.forEach(f -> {
                                 stream -> stream.forEach(f -> {
                                     final String aliasName = f.getName().replaceFirst(".json$", "");
                                     final String aliasName = f.getName().replaceFirst(".json$", "");
-                                    final String source = FileUtil.readUTF8(f);
+                                    String source = FileUtil.readUTF8(f);
+                                    if (source.trim().equals("{}")) {
+                                        source = null;
+                                    }
                                     final IndicesAliasesResponse response =
                                     final IndicesAliasesResponse response =
-                                            client.admin().indices().prepareAliases().addAlias(configIndex, aliasName, source).execute()
-                                                    .actionGet(fessConfig.getIndexIndicesTimeout());
+                                            client.admin().indices().prepareAliases().addAlias(createdIndexName, aliasName, source)
+                                                    .execute().actionGet(fessConfig.getIndexIndicesTimeout());
                                     if (response.isAcknowledged()) {
                                     if (response.isAcknowledged()) {
-                                        logger.info("Created " + aliasName + " alias for " + configIndex);
+                                        logger.info("Created " + aliasName + " alias for " + createdIndexName);
                                     } else if (logger.isDebugEnabled()) {
                                     } else if (logger.isDebugEnabled()) {
-                                        logger.debug("Failed to create " + aliasName + " alias for " + configIndex);
+                                        logger.debug("Failed to create " + aliasName + " alias for " + createdIndexName);
                                     }
                                     }
                                 }));
                                 }));
                     }
                     }
@@ -392,9 +411,23 @@ public class FessEsClient implements Client {
                 }
                 }
             }
             }
 
 
+            final String updatedIndexName;
+            if (isFessIndex) {
+                final GetIndexResponse response =
+                        client.admin().indices().prepareGetIndex().addIndices(fessConfig.getIndexDocumentUpdateIndex()).execute()
+                                .actionGet(fessConfig.getIndexIndicesTimeout());
+                final String[] indices = response.indices();
+                if (indices.length == 1) {
+                    updatedIndexName = indices[0];
+                } else {
+                    updatedIndexName = configIndex;
+                }
+            } else {
+                updatedIndexName = configIndex;
+            }
             final GetMappingsResponse getMappingsResponse =
             final GetMappingsResponse getMappingsResponse =
-                    client.admin().indices().prepareGetMappings(configIndex).execute().actionGet(fessConfig.getIndexIndicesTimeout());
-            final ImmutableOpenMap<String, MappingMetaData> indexMappings = getMappingsResponse.mappings().get(configIndex);
+                    client.admin().indices().prepareGetMappings(updatedIndexName).execute().actionGet(fessConfig.getIndexIndicesTimeout());
+            final ImmutableOpenMap<String, MappingMetaData> indexMappings = getMappingsResponse.mappings().get(updatedIndexName);
             if (indexMappings == null || !indexMappings.containsKey(configType)) {
             if (indexMappings == null || !indexMappings.containsKey(configType)) {
                 String source = null;
                 String source = null;
                 final String mappingFile = indexConfigPath + "/" + configIndex + "/" + configType + ".json";
                 final String mappingFile = indexConfigPath + "/" + configIndex + "/" + configType + ".json";
@@ -405,12 +438,12 @@ public class FessEsClient implements Client {
                 }
                 }
                 try {
                 try {
                     final PutMappingResponse putMappingResponse =
                     final PutMappingResponse putMappingResponse =
-                            client.admin().indices().preparePutMapping(configIndex).setType(configType).setSource(source).execute()
+                            client.admin().indices().preparePutMapping(updatedIndexName).setType(configType).setSource(source).execute()
                                     .actionGet(fessConfig.getIndexIndicesTimeout());
                                     .actionGet(fessConfig.getIndexIndicesTimeout());
                     if (putMappingResponse.isAcknowledged()) {
                     if (putMappingResponse.isAcknowledged()) {
-                        logger.info("Created " + configIndex + "/" + configType + " mapping.");
+                        logger.info("Created " + updatedIndexName + "/" + configType + " mapping.");
                     } else {
                     } else {
-                        logger.warn("Failed to create " + configIndex + "/" + configType + " mapping.");
+                        logger.warn("Failed to create " + updatedIndexName + "/" + configType + " mapping.");
                     }
                     }
 
 
                     final String dataPath = indexConfigPath + "/" + configIndex + "/" + configType + ".bulk";
                     final String dataPath = indexConfigPath + "/" + configIndex + "/" + configType + ".bulk";
@@ -418,10 +451,10 @@ public class FessEsClient implements Client {
                         insertBulkData(fessConfig, configIndex, configType, dataPath);
                         insertBulkData(fessConfig, configIndex, configType, dataPath);
                     }
                     }
                 } catch (final Exception e) {
                 } catch (final Exception e) {
-                    logger.warn("Failed to create " + configIndex + "/" + configType + " mapping.", e);
+                    logger.warn("Failed to create " + updatedIndexName + "/" + configType + " mapping.", e);
                 }
                 }
             } else if (logger.isDebugEnabled()) {
             } else if (logger.isDebugEnabled()) {
-                logger.debug(configIndex + "/" + configType + " mapping exists.");
+                logger.debug(updatedIndexName + "/" + configType + " mapping exists.");
             }
             }
         } else {
         } else {
             logger.warn("Invalid index config name: " + configName);
             logger.warn("Invalid index config name: " + configName);
@@ -429,6 +462,10 @@ public class FessEsClient implements Client {
     })  ;
     })  ;
     }
     }
 
 
+    protected String generateNewIndexName(final String configIndex) {
+        return configIndex + "." + new SimpleDateFormat("yyyyMMdd").format(new Date());
+    }
+
     protected void insertBulkData(final FessConfig fessConfig, final String configIndex, final String configType, final String dataPath) {
     protected void insertBulkData(final FessConfig fessConfig, final String configIndex, final String configType, final String dataPath) {
         try {
         try {
             final BulkRequestBuilder builder = client.prepareBulk();
             final BulkRequestBuilder builder = client.prepareBulk();

+ 4 - 4
src/main/java/org/codelibs/fess/mylasta/direction/FessConfig.java

@@ -348,10 +348,10 @@ public interface FessConfig extends FessEnv, org.codelibs.fess.mylasta.direction
     /** The key of the configuration. e.g. site_path */
     /** The key of the configuration. e.g. site_path */
     String RESPONSE_FIELD_site_path = "response.field.site_path";
     String RESPONSE_FIELD_site_path = "response.field.site_path";
 
 
-    /** The key of the configuration. e.g. fess */
+    /** The key of the configuration. e.g. fess.search */
     String INDEX_DOCUMENT_SEARCH_INDEX = "index.document.search.index";
     String INDEX_DOCUMENT_SEARCH_INDEX = "index.document.search.index";
 
 
-    /** The key of the configuration. e.g. fess */
+    /** The key of the configuration. e.g. fess.update */
     String INDEX_DOCUMENT_UPDATE_INDEX = "index.document.update.index";
     String INDEX_DOCUMENT_UPDATE_INDEX = "index.document.update.index";
 
 
     /** The key of the configuration. e.g. doc */
     /** The key of the configuration. e.g. doc */
@@ -2006,7 +2006,7 @@ public interface FessConfig extends FessEnv, org.codelibs.fess.mylasta.direction
 
 
     /**
     /**
      * Get the value for the key 'index.document.search.index'. <br>
      * Get the value for the key 'index.document.search.index'. <br>
-     * The value is, e.g. fess <br>
+     * The value is, e.g. fess.search <br>
      * comment: document index
      * comment: document index
      * @return The value of found property. (NotNull: if not found, exception but basically no way)
      * @return The value of found property. (NotNull: if not found, exception but basically no way)
      */
      */
@@ -2014,7 +2014,7 @@ public interface FessConfig extends FessEnv, org.codelibs.fess.mylasta.direction
 
 
     /**
     /**
      * Get the value for the key 'index.document.update.index'. <br>
      * Get the value for the key 'index.document.update.index'. <br>
-     * The value is, e.g. fess <br>
+     * The value is, e.g. fess.update <br>
      * @return The value of found property. (NotNull: if not found, exception but basically no way)
      * @return The value of found property. (NotNull: if not found, exception but basically no way)
      */
      */
     String getIndexDocumentUpdateIndex();
     String getIndexDocumentUpdateIndex();

+ 2 - 2
src/main/resources/fess_config.properties

@@ -168,8 +168,8 @@ response.field.url_link=url_link
 response.field.site_path=site_path
 response.field.site_path=site_path
 
 
 # document index
 # document index
-index.document.search.index=fess
-index.document.update.index=fess
+index.document.search.index=fess.search
+index.document.update.index=fess.update
 index.document.type=doc
 index.document.type=doc
 
 
 # doc management
 # doc management