From 7912ae977945846347acbeb8a223ccd3fa64158b Mon Sep 17 00:00:00 2001 From: Peter-Josef Meisch Date: Thu, 26 Nov 2020 06:54:20 +0100 Subject: [PATCH] DATAES-987 - IndexOperations getMapping fail when using index alias. Original PR: #560 --- .../core/DefaultIndexOperations.java | 24 ++++++++++++---- .../core/DefaultTransportIndexOperations.java | 17 +++++++++-- .../core/ElasticsearchTemplateTests.java | 28 +++++++++++++++++-- 3 files changed, 58 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/springframework/data/elasticsearch/core/DefaultIndexOperations.java b/src/main/java/org/springframework/data/elasticsearch/core/DefaultIndexOperations.java index f30c9aafa..6c78acbb1 100644 --- a/src/main/java/org/springframework/data/elasticsearch/core/DefaultIndexOperations.java +++ b/src/main/java/org/springframework/data/elasticsearch/core/DefaultIndexOperations.java @@ -16,6 +16,7 @@ package org.springframework.data.elasticsearch.core; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.Map; import java.util.Set; @@ -34,11 +35,13 @@ import org.elasticsearch.client.indices.GetIndexRequest; import org.elasticsearch.client.indices.GetIndexTemplatesRequest; import org.elasticsearch.client.indices.GetIndexTemplatesResponse; import org.elasticsearch.client.indices.GetMappingsRequest; -import org.elasticsearch.client.indices.GetMappingsResponse; import org.elasticsearch.client.indices.IndexTemplatesExistRequest; import org.elasticsearch.client.indices.PutIndexTemplateRequest; import org.elasticsearch.client.indices.PutMappingRequest; import org.elasticsearch.cluster.metadata.AliasMetadata; +import org.elasticsearch.cluster.metadata.MappingMetadata; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.data.elasticsearch.core.document.Document; import org.springframework.data.elasticsearch.core.index.AliasActions; import org.springframework.data.elasticsearch.core.index.AliasData; @@ -61,6 +64,8 @@ import org.springframework.util.Assert; */ class DefaultIndexOperations extends AbstractDefaultIndexOperations implements IndexOperations { + private static final Logger LOGGER = LoggerFactory.getLogger(DefaultIndexOperations.class); + private final ElasticsearchRestTemplate restTemplate; public DefaultIndexOperations(ElasticsearchRestTemplate restTemplate, Class boundClass) { @@ -117,10 +122,19 @@ class DefaultIndexOperations extends AbstractDefaultIndexOperations implements I GetMappingsRequest mappingsRequest = requestFactory.getMappingsRequest(index); return restTemplate.execute(client -> { - GetMappingsResponse mapping = client.indices().getMapping(mappingsRequest, RequestOptions.DEFAULT); - // we only return data for the first index name that was requested (always have done so) - String index1 = mappingsRequest.indices()[0]; - return mapping.mappings().get(index1).getSourceAsMap(); + Map mappings = client.indices() // + .getMapping(mappingsRequest, RequestOptions.DEFAULT) // + .mappings(); // + + if (mappings == null || mappings.size() == 0) { + return Collections.emptyMap(); + } + + if (mappings.size() > 1) { + LOGGER.warn("more than one mapping returned for " + index.getIndexName()); + } + // we have at least one, take the first from the iterator + return mappings.entrySet().iterator().next().getValue().getSourceAsMap(); }); } diff --git a/src/main/java/org/springframework/data/elasticsearch/core/DefaultTransportIndexOperations.java b/src/main/java/org/springframework/data/elasticsearch/core/DefaultTransportIndexOperations.java index 5bdce272d..8a664a2f2 100644 --- a/src/main/java/org/springframework/data/elasticsearch/core/DefaultTransportIndexOperations.java +++ b/src/main/java/org/springframework/data/elasticsearch/core/DefaultTransportIndexOperations.java @@ -15,6 +15,7 @@ */ package org.springframework.data.elasticsearch.core; +import java.util.Collections; import java.util.HashSet; import java.util.Iterator; import java.util.LinkedHashMap; @@ -40,6 +41,7 @@ import org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateReque import org.elasticsearch.client.Client; import org.elasticsearch.cluster.metadata.AliasMetadata; import org.elasticsearch.cluster.metadata.IndexTemplateMetadata; +import org.elasticsearch.cluster.metadata.MappingMetadata; import org.elasticsearch.common.collect.ImmutableOpenMap; import org.elasticsearch.common.compress.CompressedXContent; import org.elasticsearch.common.settings.Settings; @@ -126,10 +128,19 @@ class DefaultTransportIndexOperations extends AbstractDefaultIndexOperations imp GetMappingsRequest mappingsRequest = requestFactory.getMappingsRequest(client, index); - return client.admin().indices().getMappings( // + ImmutableOpenMap> mappings = client.admin().indices().getMappings( // mappingsRequest).actionGet() // - .getMappings().get(mappingsRequest.indices()[0]).get(IndexCoordinates.TYPE) // - .getSourceAsMap(); + .getMappings(); + + if (mappings == null || mappings.size() == 0) { + return Collections.emptyMap(); + } + + if (mappings.size() > 1) { + LOGGER.warn("more than one mapping returned for " + index.getIndexName()); + } + // we have at least one, take the first from the iterator + return mappings.iterator().next().value.get(IndexCoordinates.TYPE).getSourceAsMap(); } @Override diff --git a/src/test/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplateTests.java b/src/test/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplateTests.java index b92f27e40..87e0c76a0 100755 --- a/src/test/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplateTests.java +++ b/src/test/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplateTests.java @@ -121,14 +121,12 @@ import org.springframework.lang.Nullable; */ public abstract class ElasticsearchTemplateTests { + protected static final String INDEX_NAME_JOIN_SAMPLE_ENTITY = "test-index-sample-join-template"; private static final String INDEX_NAME_SAMPLE_ENTITY = "test-index-sample-core-template"; private static final String INDEX_1_NAME = "test-index-1"; private static final String INDEX_2_NAME = "test-index-2"; private static final String INDEX_3_NAME = "test-index-3"; - protected final IndexCoordinates index = IndexCoordinates.of(INDEX_NAME_SAMPLE_ENTITY); - protected static final String INDEX_NAME_JOIN_SAMPLE_ENTITY = "test-index-sample-join-template"; - @Autowired protected ElasticsearchOperations operations; protected IndexOperations indexOperations; @@ -1466,6 +1464,30 @@ public abstract class ElasticsearchTemplateTests { assertThat(mapping.get("properties")).isNotNull(); } + @Test // DATAES-987 + @DisplayName("should read mappings from alias") + void shouldReadMappingsFromAlias() { + + String aliasName = INDEX_NAME_SAMPLE_ENTITY + "alias"; + indexOperations.alias( // + new AliasActions( // + new AliasAction.Add( // + AliasActionParameters.builder() // + .withIndices(INDEX_NAME_SAMPLE_ENTITY) // + .withAliases(aliasName) // + .build()) // + ) // + ); + + IndexOperations aliasIndexOps = operations.indexOps(IndexCoordinates.of(aliasName)); + Map mappingFromAlias = aliasIndexOps.getMapping(); + + assertThat(mappingFromAlias).isNotNull(); + assertThat( + ((Map) ((Map) mappingFromAlias.get("properties")).get("message")).get("type")) + .isEqualTo("text"); + } + @Test public void shouldDeleteIndexForGivenEntity() {