File tree Expand file tree Collapse file tree 3 files changed +50
-0
lines changed
main/java/org/springframework/data/elasticsearch/core
test/java/org/springframework/data/elasticsearch/repositories/setting Expand file tree Collapse file tree 3 files changed +50
-0
lines changed Original file line number Diff line number Diff line change @@ -69,6 +69,22 @@ public interface ElasticsearchOperations {
69
69
*/
70
70
<T > boolean putMapping (Class <T > clazz );
71
71
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
+
72
88
/**
73
89
* Get settings for a given indexName
74
90
*
Original file line number Diff line number Diff line change 35
35
import org .elasticsearch .action .admin .indices .create .CreateIndexRequestBuilder ;
36
36
import org .elasticsearch .action .admin .indices .delete .DeleteIndexRequest ;
37
37
import org .elasticsearch .action .admin .indices .mapping .delete .DeleteMappingRequest ;
38
+ import org .elasticsearch .action .admin .indices .mapping .get .GetMappingsRequest ;
38
39
import org .elasticsearch .action .admin .indices .mapping .put .PutMappingRequestBuilder ;
39
40
import org .elasticsearch .action .admin .indices .settings .get .GetSettingsRequest ;
40
41
import org .elasticsearch .action .bulk .BulkItemResponse ;
@@ -150,6 +151,25 @@ public <T> boolean putMapping(Class<T> clazz) {
150
151
}
151
152
}
152
153
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
+
153
173
@ Override
154
174
public ElasticsearchConverter getElasticsearchConverter () {
155
175
return elasticsearchConverter ;
Original file line number Diff line number Diff line change @@ -109,4 +109,18 @@ public void shouldSearchOnGivenTokenizerUsingGivenDynamicSettingsForGivenIndex()
109
109
assertThat (entityList .size (), is (1 ));
110
110
assertThat (entityList .get (0 ).getEmail (), is (settingEntity1 .getEmail ()));
111
111
}
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
+ }
112
126
}
You can’t perform that action at this time.
0 commit comments