Skip to content

Commit a89a44b

Browse files
pnowak85xhaggi
authored andcommitted
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: spring-projects#239
1 parent 69dc36c commit a89a44b

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@
130130
* @author Ted Liang
131131
* @author Don Wellington
132132
* @author Zetang Zeng
133+
* @author Peter Nowak
133134
*/
134135
public class ElasticsearchRestTemplate
135136
implements ElasticsearchOperations, EsClient<RestHighLevelClient>, ApplicationContextAware {
@@ -252,13 +253,13 @@ public boolean putMapping(String indexName, String type, Object mapping) {
252253

253254
@Override
254255
public Map getMapping(String indexName, String type) {
255-
Assert.notNull(indexName, "No index defined for putMapping()");
256-
Assert.notNull(type, "No type defined for putMapping()");
256+
Assert.notNull(indexName, "No index defined for getMapping()");
257+
Assert.notNull(type, "No type defined for getMapping()");
257258
Map mappings = null;
258259
RestClient restClient = client.getLowLevelClient();
259260
try {
260261
Response response = restClient.performRequest("GET", "/" + indexName + "/_mapping/" + type);
261-
mappings = convertMappingResponse(EntityUtils.toString(response.getEntity()));
262+
mappings = convertMappingResponse(EntityUtils.toString(response.getEntity()), type);
262263
} catch (Exception e) {
263264
throw new ElasticsearchException(
264265
"Error while getting mapping for indexName : " + indexName + " type : " + type + " ", e);
@@ -271,14 +272,14 @@ public <T> Map getMapping(Class<T> clazz) {
271272
return getMapping(getPersistentEntityFor(clazz).getIndexName(), getPersistentEntityFor(clazz).getIndexType());
272273
}
273274

274-
private Map<String, Object> convertMappingResponse(String mappingResponse) {
275+
private Map<String, Object> convertMappingResponse(String mappingResponse, String type) {
275276
ObjectMapper mapper = new ObjectMapper();
276277

277278
try {
278279
Map result = null;
279280
JsonNode node = mapper.readTree(mappingResponse);
280281

281-
node = node.findValue("settings");
282+
node = node.findValue("mappings").findValue(type);
282283
result = mapper.readValue(mapper.writeValueAsString(node), HashMap.class);
283284

284285
return result;

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
* @author Sascha Woo
8080
* @author Jean-Baptiste Nizet
8181
* @author Zetang Zeng
82+
* @author Peter Nowak
8283
*/
8384

8485
@Ignore
@@ -2350,6 +2351,18 @@ public void shouldReadFileFromClasspathRetainingNewlines() {
23502351
" tokenizer: uax_url_email\n"));
23512352
}
23522353

2354+
@Test // DATAES-531
2355+
public void shouldReturnMappingForGivenEntityClass() {
2356+
// when
2357+
boolean created = elasticsearchTemplate.createIndex(SampleEntity.class);
2358+
elasticsearchTemplate.putMapping(SampleEntity.class);
2359+
final Map mapping = elasticsearchTemplate.getMapping(SampleEntity.class);
2360+
// then
2361+
assertThat(created, is(true));
2362+
assertThat(mapping, notNullValue());
2363+
assertThat(((Map) ((Map) mapping.get("properties")).get("message")).get("type"), Matchers.<Object>is("text"));
2364+
}
2365+
23532366
private IndexQuery getIndexQuery(SampleEntity sampleEntity) {
23542367
return new IndexQueryBuilder()
23552368
.withId(sampleEntity.getId())

0 commit comments

Comments
 (0)