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
This commit is contained in:
Peter Nowak 2019-01-18 10:26:42 +01:00 committed by xhaggi
parent 69dc36c6c3
commit a89a44b08f
2 changed files with 19 additions and 5 deletions

View File

@ -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<RestHighLevelClient>, 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<String, Object> convertMappingResponse(String mappingResponse) {
private Map<String, Object> 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;

View File

@ -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.<Object>is("text"));
}
private IndexQuery getIndexQuery(SampleEntity sampleEntity) {
return new IndexQueryBuilder()
.withId(sampleEntity.getId())