diff --git a/indexer-core/src/main/java/org/opengroup/osdu/indexer/service/IndicesServiceImpl.java b/indexer-core/src/main/java/org/opengroup/osdu/indexer/service/IndicesServiceImpl.java index db5a2f37a19857fd609da3fc3abdc953c5889fd8..dd821395188d20a0a5291cc8d7fbaf26d0aa8f53 100644 --- a/indexer-core/src/main/java/org/opengroup/osdu/indexer/service/IndicesServiceImpl.java +++ b/indexer-core/src/main/java/org/opengroup/osdu/indexer/service/IndicesServiceImpl.java @@ -24,6 +24,7 @@ import org.elasticsearch.ElasticsearchException; import org.elasticsearch.ElasticsearchStatusException; import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest; import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse; +import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest; import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; import org.elasticsearch.action.support.master.AcknowledgedResponse; import org.elasticsearch.client.Request; @@ -107,6 +108,8 @@ public class IndicesServiceImpl implements IndicesService { if (indexStatus) { this.indexCache.put(index, true); this.log.info(String.format("Time taken to successfully create new index %s : %d milliseconds", request.index(), stopTime-startTime)); + + createIndexAlias(client, index); } return indexStatus; @@ -303,4 +306,30 @@ public class IndicesServiceImpl implements IndicesService { throw exception; } } -} \ No newline at end of file + + private void createIndexAlias(RestHighLevelClient client, String index) throws IOException { + String kind = this.elasticIndexNameResolver.getKindFromIndexName(index); + String kindWithMajorVersion = getKindWithMajorVersion(kind); + for (String kd : Arrays.asList(kind, kindWithMajorVersion)) { + index = elasticIndexNameResolver.getIndexNameFromKind(kd); + String alias = elasticIndexNameResolver.getIndexAliasFromKind(kd); + IndicesAliasesRequest addRequest = new IndicesAliasesRequest(); + IndicesAliasesRequest.AliasActions aliasActions = new IndicesAliasesRequest.AliasActions(IndicesAliasesRequest.AliasActions.Type.ADD) + .index(index) + .alias(alias); + addRequest.addAliasAction(aliasActions); + AcknowledgedResponse response = client.indices().updateAliases(addRequest, RequestOptions.DEFAULT); + if (response.isAcknowledged()) { + this.log.info(String.format("Alias %s was created for index %s", alias, index)); + } + } + } + + private String getKindWithMajorVersion(String kind) { + // If kind is common:welldb:wellbore:1.2.0, then kind with major version is common:welldb:wellbore:1.*.* + int idx = kind.lastIndexOf(":"); + String version = kind.substring(idx+1); + String majorVersion = version.substring(0, version.indexOf(".")); + return String.format("%s:%s.*.*", kind.substring(0, idx), majorVersion); + } +} diff --git a/pom.xml b/pom.xml index 2552ca9c0bab62ad418973339c54fb7b3898c292..b4e7781420c3b9c964adeccfec2b2c35c85c95ed 100644 --- a/pom.xml +++ b/pom.xml @@ -13,7 +13,7 @@ <maven.compiler.target>1.8</maven.compiler.target> <maven.compiler.source>1.8</maven.compiler.source> <spring-cloud.version>Greenwich.SR2</spring-cloud.version> - <os-core-common.version>0.16.1</os-core-common.version> + <os-core-common.version>0.19.0-SNAPSHOT-ALIAS</os-core-common.version> <snakeyaml.version>1.33</snakeyaml.version> <hibernate-validator.version>6.1.5.Final</hibernate-validator.version> <jackson-databind.version>2.13.4.2</jackson-databind.version>