From a89a44b08f8a041ce548068ea0369a4d6bc97f47 Mon Sep 17 00:00:00 2001 From: Peter Nowak Date: Fri, 18 Jan 2019 10:26:42 +0100 Subject: [PATCH] DATAES-531 - Fixed getMapping from ElasticsearchRestTemplate. The getMapping methods in the ElasticsearchRestTemplate now behave like the methods in the ElasticsearchTemplate and return the mapping for the specified index and type. Original pull request: #239 --- .../core/ElasticsearchRestTemplate.java | 11 ++++++----- .../core/ElasticsearchTemplateTests.java | 13 +++++++++++++ 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/springframework/data/elasticsearch/core/ElasticsearchRestTemplate.java b/src/main/java/org/springframework/data/elasticsearch/core/ElasticsearchRestTemplate.java index 5884eb474..5c0034c85 100644 --- a/src/main/java/org/springframework/data/elasticsearch/core/ElasticsearchRestTemplate.java +++ b/src/main/java/org/springframework/data/elasticsearch/core/ElasticsearchRestTemplate.java @@ -130,6 +130,7 @@ import org.springframework.util.StringUtils; * @author Ted Liang * @author Don Wellington * @author Zetang Zeng + * @author Peter Nowak */ public class ElasticsearchRestTemplate implements ElasticsearchOperations, EsClient, ApplicationContextAware { @@ -252,13 +253,13 @@ public class ElasticsearchRestTemplate @Override public Map getMapping(String indexName, String type) { - Assert.notNull(indexName, "No index defined for putMapping()"); - Assert.notNull(type, "No type defined for putMapping()"); + Assert.notNull(indexName, "No index defined for getMapping()"); + Assert.notNull(type, "No type defined for getMapping()"); Map mappings = null; RestClient restClient = client.getLowLevelClient(); try { Response response = restClient.performRequest("GET", "/" + indexName + "/_mapping/" + type); - mappings = convertMappingResponse(EntityUtils.toString(response.getEntity())); + mappings = convertMappingResponse(EntityUtils.toString(response.getEntity()), type); } catch (Exception e) { throw new ElasticsearchException( "Error while getting mapping for indexName : " + indexName + " type : " + type + " ", e); @@ -271,14 +272,14 @@ public class ElasticsearchRestTemplate return getMapping(getPersistentEntityFor(clazz).getIndexName(), getPersistentEntityFor(clazz).getIndexType()); } - private Map convertMappingResponse(String mappingResponse) { + private Map convertMappingResponse(String mappingResponse, String type) { ObjectMapper mapper = new ObjectMapper(); try { Map result = null; JsonNode node = mapper.readTree(mappingResponse); - node = node.findValue("settings"); + node = node.findValue("mappings").findValue(type); result = mapper.readValue(mapper.writeValueAsString(node), HashMap.class); return result; 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 58ef688b2..4fe02bff0 100755 --- a/src/test/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplateTests.java +++ b/src/test/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplateTests.java @@ -79,6 +79,7 @@ import static org.springframework.data.elasticsearch.utils.IndexBuilder.*; * @author Sascha Woo * @author Jean-Baptiste Nizet * @author Zetang Zeng + * @author Peter Nowak */ @Ignore @@ -2350,6 +2351,18 @@ public class ElasticsearchTemplateTests { " tokenizer: uax_url_email\n")); } + @Test // DATAES-531 + public void shouldReturnMappingForGivenEntityClass() { + // when + boolean created = elasticsearchTemplate.createIndex(SampleEntity.class); + elasticsearchTemplate.putMapping(SampleEntity.class); + final Map mapping = elasticsearchTemplate.getMapping(SampleEntity.class); + // then + assertThat(created, is(true)); + assertThat(mapping, notNullValue()); + assertThat(((Map) ((Map) mapping.get("properties")).get("message")).get("type"), Matchers.is("text")); + } + private IndexQuery getIndexQuery(SampleEntity sampleEntity) { return new IndexQueryBuilder() .withId(sampleEntity.getId())