mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-06-20 02:52:12 +00:00
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:
parent
69dc36c6c3
commit
a89a44b08f
@ -130,6 +130,7 @@ import org.springframework.util.StringUtils;
|
|||||||
* @author Ted Liang
|
* @author Ted Liang
|
||||||
* @author Don Wellington
|
* @author Don Wellington
|
||||||
* @author Zetang Zeng
|
* @author Zetang Zeng
|
||||||
|
* @author Peter Nowak
|
||||||
*/
|
*/
|
||||||
public class ElasticsearchRestTemplate
|
public class ElasticsearchRestTemplate
|
||||||
implements ElasticsearchOperations, EsClient<RestHighLevelClient>, ApplicationContextAware {
|
implements ElasticsearchOperations, EsClient<RestHighLevelClient>, ApplicationContextAware {
|
||||||
@ -252,13 +253,13 @@ public class ElasticsearchRestTemplate
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Map getMapping(String indexName, String type) {
|
public Map getMapping(String indexName, String type) {
|
||||||
Assert.notNull(indexName, "No index defined for putMapping()");
|
Assert.notNull(indexName, "No index defined for getMapping()");
|
||||||
Assert.notNull(type, "No type defined for putMapping()");
|
Assert.notNull(type, "No type defined for getMapping()");
|
||||||
Map mappings = null;
|
Map mappings = null;
|
||||||
RestClient restClient = client.getLowLevelClient();
|
RestClient restClient = client.getLowLevelClient();
|
||||||
try {
|
try {
|
||||||
Response response = restClient.performRequest("GET", "/" + indexName + "/_mapping/" + type);
|
Response response = restClient.performRequest("GET", "/" + indexName + "/_mapping/" + type);
|
||||||
mappings = convertMappingResponse(EntityUtils.toString(response.getEntity()));
|
mappings = convertMappingResponse(EntityUtils.toString(response.getEntity()), type);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new ElasticsearchException(
|
throw new ElasticsearchException(
|
||||||
"Error while getting mapping for indexName : " + indexName + " type : " + type + " ", e);
|
"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());
|
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();
|
ObjectMapper mapper = new ObjectMapper();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Map result = null;
|
Map result = null;
|
||||||
JsonNode node = mapper.readTree(mappingResponse);
|
JsonNode node = mapper.readTree(mappingResponse);
|
||||||
|
|
||||||
node = node.findValue("settings");
|
node = node.findValue("mappings").findValue(type);
|
||||||
result = mapper.readValue(mapper.writeValueAsString(node), HashMap.class);
|
result = mapper.readValue(mapper.writeValueAsString(node), HashMap.class);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
@ -79,6 +79,7 @@ import static org.springframework.data.elasticsearch.utils.IndexBuilder.*;
|
|||||||
* @author Sascha Woo
|
* @author Sascha Woo
|
||||||
* @author Jean-Baptiste Nizet
|
* @author Jean-Baptiste Nizet
|
||||||
* @author Zetang Zeng
|
* @author Zetang Zeng
|
||||||
|
* @author Peter Nowak
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@Ignore
|
@Ignore
|
||||||
@ -2350,6 +2351,18 @@ public class ElasticsearchTemplateTests {
|
|||||||
" tokenizer: uax_url_email\n"));
|
" 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) {
|
private IndexQuery getIndexQuery(SampleEntity sampleEntity) {
|
||||||
return new IndexQueryBuilder()
|
return new IndexQueryBuilder()
|
||||||
.withId(sampleEntity.getId())
|
.withId(sampleEntity.getId())
|
||||||
|
Loading…
x
Reference in New Issue
Block a user