DATAES-29, ISSUE #16 - added interface EntityMapper to allow for custom implementation and configuration for ObjectMapper

This commit is contained in:
Artur Konczak
2013-11-10 18:51:01 +00:00
parent d3add69f4c
commit 7f91413591
6 changed files with 154 additions and 14 deletions

View File

@@ -0,0 +1,31 @@
package org.springframework.data.elasticsearch.core;
import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.ObjectMapper;
import java.io.IOException;
/**
* DocumentMapper using jackson
*
* @author Artur Konczak
*/
public class DefaultEntityMapper implements EntityMapper {
private ObjectMapper objectMapper;
public DefaultEntityMapper() {
objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}
@Override
public String mapToString(Object object) throws IOException {
return objectMapper.writeValueAsString(object);
}
@Override
public <T> T mapToObject(String source, Class<T> clazz) throws IOException {
return objectMapper.readValue(source, clazz);
}
}

View File

@@ -16,8 +16,6 @@
package org.springframework.data.elasticsearch.core;
import org.apache.commons.collections.CollectionUtils;
import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.ObjectMapper;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.admin.indices.mapping.delete.DeleteMappingRequest;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequestBuilder;
@@ -81,24 +79,30 @@ import static org.springframework.data.elasticsearch.core.MappingBuilder.buildMa
*
* @author Rizwan Idrees
* @author Mohsin Husen
* @author Artur Konczak
*/
public class ElasticsearchTemplate implements ElasticsearchOperations {
private Client client;
private ElasticsearchConverter elasticsearchConverter;
private ObjectMapper objectMapper = new ObjectMapper();
{
objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}
private EntityMapper entityMapper;
public ElasticsearchTemplate(Client client) {
this(client, null);
this(client, null, null);
}
public ElasticsearchTemplate(Client client, EntityMapper entityMapper) {
this(client, null, entityMapper);
}
public ElasticsearchTemplate(Client client, ElasticsearchConverter elasticsearchConverter) {
this(client, elasticsearchConverter, null);
}
public ElasticsearchTemplate(Client client, ElasticsearchConverter elasticsearchConverter, EntityMapper entityMapper) {
this.client = client;
this.entityMapper = (entityMapper == null) ? new DefaultEntityMapper() : entityMapper;
this.elasticsearchConverter = (elasticsearchConverter == null) ? new MappingElasticsearchConverter(
new SimpleElasticsearchMappingContext()) : elasticsearchConverter;
}
@@ -239,7 +243,7 @@ public class ElasticsearchTemplate implements ElasticsearchOperations {
Assert.notNull(query.getId(), "No Id define for Query");
Assert.notNull(query.getIndexRequest(), "No IndexRequest define for Query");
UpdateRequestBuilder updateRequestBuilder = client.prepareUpdate(indexName, type, query.getId());
if(query.DoUpsert()){
if (query.DoUpsert()) {
updateRequestBuilder.setDocAsUpsert(true)
.setUpsert(query.getIndexRequest()).setDoc(query.getIndexRequest());
} else {
@@ -289,7 +293,7 @@ public class ElasticsearchTemplate implements ElasticsearchOperations {
}
@Override
public void deleteType(String index, String type){
public void deleteType(String index, String type) {
Map mappings = client.admin().cluster().prepareState().execute().actionGet()
.getState().metaData().index(index).mappings();
if (mappings.containsKey(type)) {
@@ -433,8 +437,8 @@ public class ElasticsearchTemplate implements ElasticsearchOperations {
}
}
if(searchQuery.getHighlightFields() != null) {
for(HighlightBuilder.Field highlightField : searchQuery.getHighlightFields()){
if (searchQuery.getHighlightFields() != null) {
for (HighlightBuilder.Field highlightField : searchQuery.getHighlightFields()) {
searchRequest.addHighlightedField(highlightField);
}
}
@@ -509,7 +513,7 @@ public class ElasticsearchTemplate implements ElasticsearchOperations {
: query.getType();
IndexRequestBuilder indexRequestBuilder = client.prepareIndex(indexName, type, query.getId()).setSource(
objectMapper.writeValueAsString(query.getObject()));
entityMapper.mapToString(query.getObject()));
if (query.getVersion() != null) {
indexRequestBuilder.setVersion(query.getVersion());
@@ -587,7 +591,7 @@ public class ElasticsearchTemplate implements ElasticsearchOperations {
return null;
}
try {
return objectMapper.readValue(source, clazz);
return entityMapper.mapToObject(source, clazz);
} catch (IOException e) {
throw new ElasticsearchException("failed to map source [ " + source + "] to class " + clazz.getSimpleName(), e);
}
@@ -599,4 +603,7 @@ public class ElasticsearchTemplate implements ElasticsearchOperations {
}
protected EntityMapper getEntityMapper() {
return entityMapper;
}
}

View File

@@ -0,0 +1,17 @@
package org.springframework.data.elasticsearch.core;
import java.io.IOException;
/**
* DocumentMapper interface, it will allow to customize how we mapping object to json
*
* @author Artur Konczak
* @author Rizwan Idrees
* @author Mohsin Husen
*/
public interface EntityMapper {
public String mapToString(Object object) throws IOException;
public <T> T mapToObject(String source, Class<T> clazz) throws IOException;
}