Skip to content

Commit ad0409c

Browse files
committed
DATAES-63 - Ability to add mapping using arbitrary JSON and XContentBuilder using ElasticsearchTemplate
1 parent 6b6f3be commit ad0409c

File tree

3 files changed

+67
-5
lines changed

3 files changed

+67
-5
lines changed

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

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

72+
/**
73+
* Create mapping for a given indexName and type
74+
*
75+
* @param indexName
76+
* @param type
77+
* @param mappings
78+
*/
79+
boolean putMapping(String indexName, String type, Object mappings);
80+
81+
/**
82+
* Create mapping for a class
83+
*
84+
* @param clazz
85+
* @param mappings
86+
*/
87+
<T> boolean putMapping(Class<T> clazz, Object mappings);
88+
89+
7290
/**
7391
* Get mapping for a class
7492
*

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

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,16 +139,35 @@ public boolean createIndex(String indexName) {
139139
@Override
140140
public <T> boolean putMapping(Class<T> clazz) {
141141
ElasticsearchPersistentEntity<T> persistentEntity = getPersistentEntityFor(clazz);
142-
PutMappingRequestBuilder requestBuilder = client.admin().indices()
143-
.preparePutMapping(persistentEntity.getIndexName()).setType(persistentEntity.getIndexType());
144-
142+
XContentBuilder xContentBuilder = null;
145143
try {
146-
XContentBuilder xContentBuilder = buildMapping(clazz, persistentEntity.getIndexType(), persistentEntity
144+
xContentBuilder = buildMapping(clazz, persistentEntity.getIndexType(), persistentEntity
147145
.getIdProperty().getFieldName(), persistentEntity.getParentType());
148-
return requestBuilder.setSource(xContentBuilder).execute().actionGet().isAcknowledged();
149146
} catch (Exception e) {
150147
throw new ElasticsearchException("Failed to build mapping for " + clazz.getSimpleName(), e);
151148
}
149+
return putMapping(clazz, xContentBuilder);
150+
}
151+
152+
@Override
153+
public <T> boolean putMapping(Class<T> clazz, Object mapping) {
154+
return putMapping(getPersistentEntityFor(clazz).getIndexName(), getPersistentEntityFor(clazz).getIndexType(), mapping);
155+
}
156+
157+
@Override
158+
public boolean putMapping(String indexName, String type, Object mapping) {
159+
Assert.notNull(indexName, "No index defined for putMapping()");
160+
Assert.notNull(type, "No type defined for putMapping()");
161+
PutMappingRequestBuilder requestBuilder = client.admin().indices()
162+
.preparePutMapping(indexName).setType(type);
163+
if (mapping instanceof String) {
164+
requestBuilder.setSource(String.valueOf(mapping));
165+
} else if (mapping instanceof Map) {
166+
requestBuilder.setSource((Map) mapping);
167+
} else if (mapping instanceof XContentBuilder) {
168+
requestBuilder.setSource((XContentBuilder) mapping);
169+
}
170+
return requestBuilder.execute().actionGet().isAcknowledged();
152171
}
153172

154173
@Override

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,29 @@ public void shouldGetMappingForGivenIndexAndType() {
123123
assertThat(((String) ((Map) properties.get("email")).get("type")), is("string"));
124124
assertThat((String) ((Map)properties.get("email")).get("analyzer"), is("emailAnalyzer"));
125125
}
126+
127+
@Test
128+
public void shouldCreateMappingWithSpecifiedMappings() {
129+
//given
130+
elasticsearchTemplate.deleteIndex(SettingEntity.class);
131+
elasticsearchTemplate.createIndex(SettingEntity.class);
132+
elasticsearchTemplate.refresh(SettingEntity.class, true);
133+
//when
134+
String mappings = "{\n" +
135+
" \"test-setting-type\" : {\n" +
136+
" \"properties\" : {\n" +
137+
" \"email\" : {\"type\" : \"string\", \"analyzer\" : \"emailAnalyzer\" }\n" +
138+
" }\n" +
139+
" }\n" +
140+
"}";
141+
elasticsearchTemplate.putMapping(SettingEntity.class, mappings);
142+
elasticsearchTemplate.refresh(SettingEntity.class, true);
143+
//then
144+
Map mapping = elasticsearchTemplate.getMapping(SettingEntity.class);
145+
Map properties = (Map) mapping.get("properties");
146+
assertThat(mapping, is(notNullValue()));
147+
assertThat(properties, is(notNullValue()));
148+
assertThat(((String) ((Map) properties.get("email")).get("type")), is("string"));
149+
assertThat((String) ((Map)properties.get("email")).get("analyzer"), is("emailAnalyzer"));
150+
}
126151
}

0 commit comments

Comments
 (0)