mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-06-23 12:32:10 +00:00
DATAES-63 - Ability to add mapping using arbitrary JSON and XContentBuilder using ElasticsearchTemplate
This commit is contained in:
parent
6b6f3beabe
commit
ad0409c7ae
@ -69,6 +69,24 @@ public interface ElasticsearchOperations {
|
||||
*/
|
||||
<T> boolean putMapping(Class<T> clazz);
|
||||
|
||||
/**
|
||||
* Create mapping for a given indexName and type
|
||||
*
|
||||
* @param indexName
|
||||
* @param type
|
||||
* @param mappings
|
||||
*/
|
||||
boolean putMapping(String indexName, String type, Object mappings);
|
||||
|
||||
/**
|
||||
* Create mapping for a class
|
||||
*
|
||||
* @param clazz
|
||||
* @param mappings
|
||||
*/
|
||||
<T> boolean putMapping(Class<T> clazz, Object mappings);
|
||||
|
||||
|
||||
/**
|
||||
* Get mapping for a class
|
||||
*
|
||||
|
@ -139,16 +139,35 @@ public class ElasticsearchTemplate implements ElasticsearchOperations {
|
||||
@Override
|
||||
public <T> boolean putMapping(Class<T> clazz) {
|
||||
ElasticsearchPersistentEntity<T> persistentEntity = getPersistentEntityFor(clazz);
|
||||
PutMappingRequestBuilder requestBuilder = client.admin().indices()
|
||||
.preparePutMapping(persistentEntity.getIndexName()).setType(persistentEntity.getIndexType());
|
||||
|
||||
XContentBuilder xContentBuilder = null;
|
||||
try {
|
||||
XContentBuilder xContentBuilder = buildMapping(clazz, persistentEntity.getIndexType(), persistentEntity
|
||||
xContentBuilder = buildMapping(clazz, persistentEntity.getIndexType(), persistentEntity
|
||||
.getIdProperty().getFieldName(), persistentEntity.getParentType());
|
||||
return requestBuilder.setSource(xContentBuilder).execute().actionGet().isAcknowledged();
|
||||
} catch (Exception e) {
|
||||
throw new ElasticsearchException("Failed to build mapping for " + clazz.getSimpleName(), e);
|
||||
}
|
||||
return putMapping(clazz, xContentBuilder);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> boolean putMapping(Class<T> clazz, Object mapping) {
|
||||
return putMapping(getPersistentEntityFor(clazz).getIndexName(), getPersistentEntityFor(clazz).getIndexType(), mapping);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean putMapping(String indexName, String type, Object mapping) {
|
||||
Assert.notNull(indexName, "No index defined for putMapping()");
|
||||
Assert.notNull(type, "No type defined for putMapping()");
|
||||
PutMappingRequestBuilder requestBuilder = client.admin().indices()
|
||||
.preparePutMapping(indexName).setType(type);
|
||||
if (mapping instanceof String) {
|
||||
requestBuilder.setSource(String.valueOf(mapping));
|
||||
} else if (mapping instanceof Map) {
|
||||
requestBuilder.setSource((Map) mapping);
|
||||
} else if (mapping instanceof XContentBuilder) {
|
||||
requestBuilder.setSource((XContentBuilder) mapping);
|
||||
}
|
||||
return requestBuilder.execute().actionGet().isAcknowledged();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -123,4 +123,29 @@ public class SettingEntityRepositoryTest {
|
||||
assertThat(((String) ((Map) properties.get("email")).get("type")), is("string"));
|
||||
assertThat((String) ((Map)properties.get("email")).get("analyzer"), is("emailAnalyzer"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCreateMappingWithSpecifiedMappings() {
|
||||
//given
|
||||
elasticsearchTemplate.deleteIndex(SettingEntity.class);
|
||||
elasticsearchTemplate.createIndex(SettingEntity.class);
|
||||
elasticsearchTemplate.refresh(SettingEntity.class, true);
|
||||
//when
|
||||
String mappings = "{\n" +
|
||||
" \"test-setting-type\" : {\n" +
|
||||
" \"properties\" : {\n" +
|
||||
" \"email\" : {\"type\" : \"string\", \"analyzer\" : \"emailAnalyzer\" }\n" +
|
||||
" }\n" +
|
||||
" }\n" +
|
||||
"}";
|
||||
elasticsearchTemplate.putMapping(SettingEntity.class, mappings);
|
||||
elasticsearchTemplate.refresh(SettingEntity.class, true);
|
||||
//then
|
||||
Map mapping = elasticsearchTemplate.getMapping(SettingEntity.class);
|
||||
Map properties = (Map) mapping.get("properties");
|
||||
assertThat(mapping, is(notNullValue()));
|
||||
assertThat(properties, is(notNullValue()));
|
||||
assertThat(((String) ((Map) properties.get("email")).get("type")), is("string"));
|
||||
assertThat((String) ((Map)properties.get("email")).get("analyzer"), is("emailAnalyzer"));
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user