DATAES-85 - Add support to get elasticsearch mappings

This commit is contained in:
Mohsin Husen 2014-04-24 15:19:00 +01:00
parent 5ca0ed7ff1
commit 6b6f3beabe
3 changed files with 50 additions and 0 deletions

View File

@ -69,6 +69,22 @@ public interface ElasticsearchOperations {
*/
<T> boolean putMapping(Class<T> clazz);
/**
* Get mapping for a class
*
* @param clazz
* @param <T>
*/
<T> Map getMapping(Class<T> clazz);
/**
* Get mapping for a given indexName and type
*
* @param indexName
* @param type
*/
Map getMapping(String indexName, String type);
/**
* Get settings for a given indexName
*

View File

@ -35,6 +35,7 @@ import org.elasticsearch.action.admin.cluster.state.ClusterStateRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.admin.indices.mapping.delete.DeleteMappingRequest;
import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsRequest;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequestBuilder;
import org.elasticsearch.action.admin.indices.settings.get.GetSettingsRequest;
import org.elasticsearch.action.bulk.BulkItemResponse;
@ -150,6 +151,25 @@ public class ElasticsearchTemplate implements ElasticsearchOperations {
}
}
@Override
public Map getMapping(String indexName, String type) {
Assert.notNull(indexName, "No index defined for putMapping()");
Assert.notNull(type, "No type defined for putMapping()");
Map mappings = null;
try {
mappings = client.admin().indices().getMappings(new GetMappingsRequest().indices(indexName).types(type))
.actionGet().getMappings().get(indexName).get(type).getSourceAsMap();
} catch (Exception e) {
throw new ElasticsearchException("Error while getting mapping for indexName : " + indexName + " type : " + type + " " + e.getMessage());
}
return mappings;
}
@Override
public <T> Map getMapping(Class<T> clazz) {
return getMapping(getPersistentEntityFor(clazz).getIndexName(), getPersistentEntityFor(clazz).getIndexType());
}
@Override
public ElasticsearchConverter getElasticsearchConverter() {
return elasticsearchConverter;

View File

@ -109,4 +109,18 @@ public class SettingEntityRepositoryTest {
assertThat(entityList.size(), is(1));
assertThat(entityList.get(0).getEmail(), is(settingEntity1.getEmail()));
}
@Test
public void shouldGetMappingForGivenIndexAndType() {
//given
//delete , create and apply mapping in before method
//when
Map mapping = elasticsearchTemplate.getMapping(SettingEntity.class);
//then
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"));
}
}