Skip to content

Commit 7912ae9

Browse files
authored
DATAES-987 - IndexOperations getMapping fail when using index alias.
Original PR: #560
1 parent 9bf6b6a commit 7912ae9

File tree

3 files changed

+58
-11
lines changed

3 files changed

+58
-11
lines changed

src/main/java/org/springframework/data/elasticsearch/core/DefaultIndexOperations.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.springframework.data.elasticsearch.core;
1717

1818
import java.util.ArrayList;
19+
import java.util.Collections;
1920
import java.util.List;
2021
import java.util.Map;
2122
import java.util.Set;
@@ -34,11 +35,13 @@
3435
import org.elasticsearch.client.indices.GetIndexTemplatesRequest;
3536
import org.elasticsearch.client.indices.GetIndexTemplatesResponse;
3637
import org.elasticsearch.client.indices.GetMappingsRequest;
37-
import org.elasticsearch.client.indices.GetMappingsResponse;
3838
import org.elasticsearch.client.indices.IndexTemplatesExistRequest;
3939
import org.elasticsearch.client.indices.PutIndexTemplateRequest;
4040
import org.elasticsearch.client.indices.PutMappingRequest;
4141
import org.elasticsearch.cluster.metadata.AliasMetadata;
42+
import org.elasticsearch.cluster.metadata.MappingMetadata;
43+
import org.slf4j.Logger;
44+
import org.slf4j.LoggerFactory;
4245
import org.springframework.data.elasticsearch.core.document.Document;
4346
import org.springframework.data.elasticsearch.core.index.AliasActions;
4447
import org.springframework.data.elasticsearch.core.index.AliasData;
@@ -61,6 +64,8 @@
6164
*/
6265
class DefaultIndexOperations extends AbstractDefaultIndexOperations implements IndexOperations {
6366

67+
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultIndexOperations.class);
68+
6469
private final ElasticsearchRestTemplate restTemplate;
6570

6671
public DefaultIndexOperations(ElasticsearchRestTemplate restTemplate, Class<?> boundClass) {
@@ -117,10 +122,19 @@ protected Map<String, Object> doGetMapping(IndexCoordinates index) {
117122
GetMappingsRequest mappingsRequest = requestFactory.getMappingsRequest(index);
118123

119124
return restTemplate.execute(client -> {
120-
GetMappingsResponse mapping = client.indices().getMapping(mappingsRequest, RequestOptions.DEFAULT);
121-
// we only return data for the first index name that was requested (always have done so)
122-
String index1 = mappingsRequest.indices()[0];
123-
return mapping.mappings().get(index1).getSourceAsMap();
125+
Map<String, MappingMetadata> mappings = client.indices() //
126+
.getMapping(mappingsRequest, RequestOptions.DEFAULT) //
127+
.mappings(); //
128+
129+
if (mappings == null || mappings.size() == 0) {
130+
return Collections.emptyMap();
131+
}
132+
133+
if (mappings.size() > 1) {
134+
LOGGER.warn("more than one mapping returned for " + index.getIndexName());
135+
}
136+
// we have at least one, take the first from the iterator
137+
return mappings.entrySet().iterator().next().getValue().getSourceAsMap();
124138
});
125139
}
126140

src/main/java/org/springframework/data/elasticsearch/core/DefaultTransportIndexOperations.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.data.elasticsearch.core;
1717

18+
import java.util.Collections;
1819
import java.util.HashSet;
1920
import java.util.Iterator;
2021
import java.util.LinkedHashMap;
@@ -40,6 +41,7 @@
4041
import org.elasticsearch.client.Client;
4142
import org.elasticsearch.cluster.metadata.AliasMetadata;
4243
import org.elasticsearch.cluster.metadata.IndexTemplateMetadata;
44+
import org.elasticsearch.cluster.metadata.MappingMetadata;
4345
import org.elasticsearch.common.collect.ImmutableOpenMap;
4446
import org.elasticsearch.common.compress.CompressedXContent;
4547
import org.elasticsearch.common.settings.Settings;
@@ -126,10 +128,19 @@ protected Map<String, Object> doGetMapping(IndexCoordinates index) {
126128

127129
GetMappingsRequest mappingsRequest = requestFactory.getMappingsRequest(client, index);
128130

129-
return client.admin().indices().getMappings( //
131+
ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetadata>> mappings = client.admin().indices().getMappings( //
130132
mappingsRequest).actionGet() //
131-
.getMappings().get(mappingsRequest.indices()[0]).get(IndexCoordinates.TYPE) //
132-
.getSourceAsMap();
133+
.getMappings();
134+
135+
if (mappings == null || mappings.size() == 0) {
136+
return Collections.emptyMap();
137+
}
138+
139+
if (mappings.size() > 1) {
140+
LOGGER.warn("more than one mapping returned for " + index.getIndexName());
141+
}
142+
// we have at least one, take the first from the iterator
143+
return mappings.iterator().next().value.get(IndexCoordinates.TYPE).getSourceAsMap();
133144
}
134145

135146
@Override

src/test/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplateTests.java

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,12 @@
121121
*/
122122
public abstract class ElasticsearchTemplateTests {
123123

124+
protected static final String INDEX_NAME_JOIN_SAMPLE_ENTITY = "test-index-sample-join-template";
124125
private static final String INDEX_NAME_SAMPLE_ENTITY = "test-index-sample-core-template";
125126
private static final String INDEX_1_NAME = "test-index-1";
126127
private static final String INDEX_2_NAME = "test-index-2";
127128
private static final String INDEX_3_NAME = "test-index-3";
128-
129129
protected final IndexCoordinates index = IndexCoordinates.of(INDEX_NAME_SAMPLE_ENTITY);
130-
protected static final String INDEX_NAME_JOIN_SAMPLE_ENTITY = "test-index-sample-join-template";
131-
132130
@Autowired protected ElasticsearchOperations operations;
133131
protected IndexOperations indexOperations;
134132

@@ -1466,6 +1464,30 @@ public void shouldPutMappingWithCustomIndexName() {
14661464
assertThat(mapping.get("properties")).isNotNull();
14671465
}
14681466

1467+
@Test // DATAES-987
1468+
@DisplayName("should read mappings from alias")
1469+
void shouldReadMappingsFromAlias() {
1470+
1471+
String aliasName = INDEX_NAME_SAMPLE_ENTITY + "alias";
1472+
indexOperations.alias( //
1473+
new AliasActions( //
1474+
new AliasAction.Add( //
1475+
AliasActionParameters.builder() //
1476+
.withIndices(INDEX_NAME_SAMPLE_ENTITY) //
1477+
.withAliases(aliasName) //
1478+
.build()) //
1479+
) //
1480+
);
1481+
1482+
IndexOperations aliasIndexOps = operations.indexOps(IndexCoordinates.of(aliasName));
1483+
Map<String, Object> mappingFromAlias = aliasIndexOps.getMapping();
1484+
1485+
assertThat(mappingFromAlias).isNotNull();
1486+
assertThat(
1487+
((Map<String, Object>) ((Map<String, Object>) mappingFromAlias.get("properties")).get("message")).get("type"))
1488+
.isEqualTo("text");
1489+
}
1490+
14691491
@Test
14701492
public void shouldDeleteIndexForGivenEntity() {
14711493

0 commit comments

Comments
 (0)