Skip to content

Commit 6b6f3be

Browse files
committed
DATAES-85 - Add support to get elasticsearch mappings
1 parent 5ca0ed7 commit 6b6f3be

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,22 @@ public interface ElasticsearchOperations {
6969
*/
7070
<T> boolean putMapping(Class<T> clazz);
7171

72+
/**
73+
* Get mapping for a class
74+
*
75+
* @param clazz
76+
* @param <T>
77+
*/
78+
<T> Map getMapping(Class<T> clazz);
79+
80+
/**
81+
* Get mapping for a given indexName and type
82+
*
83+
* @param indexName
84+
* @param type
85+
*/
86+
Map getMapping(String indexName, String type);
87+
7288
/**
7389
* Get settings for a given indexName
7490
*

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder;
3636
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
3737
import org.elasticsearch.action.admin.indices.mapping.delete.DeleteMappingRequest;
38+
import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsRequest;
3839
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequestBuilder;
3940
import org.elasticsearch.action.admin.indices.settings.get.GetSettingsRequest;
4041
import org.elasticsearch.action.bulk.BulkItemResponse;
@@ -150,6 +151,25 @@ public <T> boolean putMapping(Class<T> clazz) {
150151
}
151152
}
152153

154+
@Override
155+
public Map getMapping(String indexName, String type) {
156+
Assert.notNull(indexName, "No index defined for putMapping()");
157+
Assert.notNull(type, "No type defined for putMapping()");
158+
Map mappings = null;
159+
try {
160+
mappings = client.admin().indices().getMappings(new GetMappingsRequest().indices(indexName).types(type))
161+
.actionGet().getMappings().get(indexName).get(type).getSourceAsMap();
162+
} catch (Exception e) {
163+
throw new ElasticsearchException("Error while getting mapping for indexName : " + indexName + " type : " + type + " " + e.getMessage());
164+
}
165+
return mappings;
166+
}
167+
168+
@Override
169+
public <T> Map getMapping(Class<T> clazz) {
170+
return getMapping(getPersistentEntityFor(clazz).getIndexName(), getPersistentEntityFor(clazz).getIndexType());
171+
}
172+
153173
@Override
154174
public ElasticsearchConverter getElasticsearchConverter() {
155175
return elasticsearchConverter;

src/test/java/org/springframework/data/elasticsearch/repositories/setting/SettingEntityRepositoryTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,18 @@ public void shouldSearchOnGivenTokenizerUsingGivenDynamicSettingsForGivenIndex()
109109
assertThat(entityList.size(), is(1));
110110
assertThat(entityList.get(0).getEmail(), is(settingEntity1.getEmail()));
111111
}
112+
113+
@Test
114+
public void shouldGetMappingForGivenIndexAndType() {
115+
//given
116+
//delete , create and apply mapping in before method
117+
//when
118+
Map mapping = elasticsearchTemplate.getMapping(SettingEntity.class);
119+
//then
120+
Map properties = (Map) mapping.get("properties");
121+
assertThat(mapping, is(notNullValue()));
122+
assertThat(properties, is(notNullValue()));
123+
assertThat(((String) ((Map) properties.get("email")).get("type")), is("string"));
124+
assertThat((String) ((Map)properties.get("email")).get("analyzer"), is("emailAnalyzer"));
125+
}
112126
}

0 commit comments

Comments
 (0)