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;
}

View File

@ -0,0 +1,19 @@
package org.springframework.data.elasticsearch.core;
import java.io.IOException;
/**
* @author Artur Konczak
*/
public class CustomEntityMapper implements EntityMapper {
@Override
public String mapToString(Object object) throws IOException {
return null;
}
@Override
public <T> T mapToObject(String source, Class<T> clazz) throws IOException {
return null;
}
}

View File

@ -0,0 +1,48 @@
/*
* Copyright 2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.elasticsearch.core;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
/**
* @author Artur Konczak
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:elasticsearch-template-custom-mapper.xml")
public class ElasticsearchTemplateCustomMapperTests {
@Autowired
private ElasticsearchTemplate elasticsearchTemplate;
@Autowired
private EntityMapper entityMapper;
@Test
public void shouldUseCustomMapper() {
//given
//when
//them
assertThat(elasticsearchTemplate.getEntityMapper(), is(entityMapper));
}
}

View File

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:elasticsearch="http://www.springframework.org/schema/data/elasticsearch"
xsi:schemaLocation="http://www.springframework.org/schema/data/elasticsearch http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch-1.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<import resource="infrastructure.xml" />
<bean name="elasticsearchTemplate"
class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">
<constructor-arg name="client" ref="client" />
<constructor-arg name="entityMapper" ref="entityMapper"/>
</bean>
<bean name="entityMapper" class="org.springframework.data.elasticsearch.core.CustomEntityMapper"/>
</beans>