mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-05-31 09:12:11 +00:00
DATAES-987 - IndexOperations getMapping fail when using index alias.
Original PR: #560
This commit is contained in:
parent
9bf6b6a984
commit
7912ae9779
@ -16,6 +16,7 @@
|
|||||||
package org.springframework.data.elasticsearch.core;
|
package org.springframework.data.elasticsearch.core;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
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.GetIndexTemplatesRequest;
|
||||||
import org.elasticsearch.client.indices.GetIndexTemplatesResponse;
|
import org.elasticsearch.client.indices.GetIndexTemplatesResponse;
|
||||||
import org.elasticsearch.client.indices.GetMappingsRequest;
|
import org.elasticsearch.client.indices.GetMappingsRequest;
|
||||||
import org.elasticsearch.client.indices.GetMappingsResponse;
|
|
||||||
import org.elasticsearch.client.indices.IndexTemplatesExistRequest;
|
import org.elasticsearch.client.indices.IndexTemplatesExistRequest;
|
||||||
import org.elasticsearch.client.indices.PutIndexTemplateRequest;
|
import org.elasticsearch.client.indices.PutIndexTemplateRequest;
|
||||||
import org.elasticsearch.client.indices.PutMappingRequest;
|
import org.elasticsearch.client.indices.PutMappingRequest;
|
||||||
import org.elasticsearch.cluster.metadata.AliasMetadata;
|
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.document.Document;
|
||||||
import org.springframework.data.elasticsearch.core.index.AliasActions;
|
import org.springframework.data.elasticsearch.core.index.AliasActions;
|
||||||
import org.springframework.data.elasticsearch.core.index.AliasData;
|
import org.springframework.data.elasticsearch.core.index.AliasData;
|
||||||
@ -61,6 +64,8 @@ import org.springframework.util.Assert;
|
|||||||
*/
|
*/
|
||||||
class DefaultIndexOperations extends AbstractDefaultIndexOperations implements IndexOperations {
|
class DefaultIndexOperations extends AbstractDefaultIndexOperations implements IndexOperations {
|
||||||
|
|
||||||
|
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultIndexOperations.class);
|
||||||
|
|
||||||
private final ElasticsearchRestTemplate restTemplate;
|
private final ElasticsearchRestTemplate restTemplate;
|
||||||
|
|
||||||
public DefaultIndexOperations(ElasticsearchRestTemplate restTemplate, Class<?> boundClass) {
|
public DefaultIndexOperations(ElasticsearchRestTemplate restTemplate, Class<?> boundClass) {
|
||||||
@ -117,10 +122,19 @@ class DefaultIndexOperations extends AbstractDefaultIndexOperations implements I
|
|||||||
GetMappingsRequest mappingsRequest = requestFactory.getMappingsRequest(index);
|
GetMappingsRequest mappingsRequest = requestFactory.getMappingsRequest(index);
|
||||||
|
|
||||||
return restTemplate.execute(client -> {
|
return restTemplate.execute(client -> {
|
||||||
GetMappingsResponse mapping = client.indices().getMapping(mappingsRequest, RequestOptions.DEFAULT);
|
Map<String, MappingMetadata> mappings = client.indices() //
|
||||||
// we only return data for the first index name that was requested (always have done so)
|
.getMapping(mappingsRequest, RequestOptions.DEFAULT) //
|
||||||
String index1 = mappingsRequest.indices()[0];
|
.mappings(); //
|
||||||
return mapping.mappings().get(index1).getSourceAsMap();
|
|
||||||
|
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();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -15,6 +15,7 @@
|
|||||||
*/
|
*/
|
||||||
package org.springframework.data.elasticsearch.core;
|
package org.springframework.data.elasticsearch.core;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.LinkedHashMap;
|
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.client.Client;
|
||||||
import org.elasticsearch.cluster.metadata.AliasMetadata;
|
import org.elasticsearch.cluster.metadata.AliasMetadata;
|
||||||
import org.elasticsearch.cluster.metadata.IndexTemplateMetadata;
|
import org.elasticsearch.cluster.metadata.IndexTemplateMetadata;
|
||||||
|
import org.elasticsearch.cluster.metadata.MappingMetadata;
|
||||||
import org.elasticsearch.common.collect.ImmutableOpenMap;
|
import org.elasticsearch.common.collect.ImmutableOpenMap;
|
||||||
import org.elasticsearch.common.compress.CompressedXContent;
|
import org.elasticsearch.common.compress.CompressedXContent;
|
||||||
import org.elasticsearch.common.settings.Settings;
|
import org.elasticsearch.common.settings.Settings;
|
||||||
@ -126,10 +128,19 @@ class DefaultTransportIndexOperations extends AbstractDefaultIndexOperations imp
|
|||||||
|
|
||||||
GetMappingsRequest mappingsRequest = requestFactory.getMappingsRequest(client, index);
|
GetMappingsRequest mappingsRequest = requestFactory.getMappingsRequest(client, index);
|
||||||
|
|
||||||
return client.admin().indices().getMappings( //
|
ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetadata>> mappings = client.admin().indices().getMappings( //
|
||||||
mappingsRequest).actionGet() //
|
mappingsRequest).actionGet() //
|
||||||
.getMappings().get(mappingsRequest.indices()[0]).get(IndexCoordinates.TYPE) //
|
.getMappings();
|
||||||
.getSourceAsMap();
|
|
||||||
|
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
|
@Override
|
||||||
|
@ -121,14 +121,12 @@ import org.springframework.lang.Nullable;
|
|||||||
*/
|
*/
|
||||||
public abstract class ElasticsearchTemplateTests {
|
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_NAME_SAMPLE_ENTITY = "test-index-sample-core-template";
|
||||||
private static final String INDEX_1_NAME = "test-index-1";
|
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_2_NAME = "test-index-2";
|
||||||
private static final String INDEX_3_NAME = "test-index-3";
|
private static final String INDEX_3_NAME = "test-index-3";
|
||||||
|
|
||||||
protected final IndexCoordinates index = IndexCoordinates.of(INDEX_NAME_SAMPLE_ENTITY);
|
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;
|
@Autowired protected ElasticsearchOperations operations;
|
||||||
protected IndexOperations indexOperations;
|
protected IndexOperations indexOperations;
|
||||||
|
|
||||||
@ -1466,6 +1464,30 @@ public abstract class ElasticsearchTemplateTests {
|
|||||||
assertThat(mapping.get("properties")).isNotNull();
|
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<String, Object> mappingFromAlias = aliasIndexOps.getMapping();
|
||||||
|
|
||||||
|
assertThat(mappingFromAlias).isNotNull();
|
||||||
|
assertThat(
|
||||||
|
((Map<String, Object>) ((Map<String, Object>) mappingFromAlias.get("properties")).get("message")).get("type"))
|
||||||
|
.isEqualTo("text");
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldDeleteIndexForGivenEntity() {
|
public void shouldDeleteIndexForGivenEntity() {
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user