diff --git a/src/main/java/org/springframework/data/elasticsearch/core/DefaultEntityMapper.java b/src/main/java/org/springframework/data/elasticsearch/core/DefaultEntityMapper.java new file mode 100644 index 000000000..bdfb6e33e --- /dev/null +++ b/src/main/java/org/springframework/data/elasticsearch/core/DefaultEntityMapper.java @@ -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 mapToObject(String source, Class clazz) throws IOException { + return objectMapper.readValue(source, clazz); + } +} diff --git a/src/main/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplate.java b/src/main/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplate.java index fd0936b54..fa9791a5b 100644 --- a/src/main/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplate.java +++ b/src/main/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplate.java @@ -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; + } } diff --git a/src/main/java/org/springframework/data/elasticsearch/core/EntityMapper.java b/src/main/java/org/springframework/data/elasticsearch/core/EntityMapper.java new file mode 100644 index 000000000..898383642 --- /dev/null +++ b/src/main/java/org/springframework/data/elasticsearch/core/EntityMapper.java @@ -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 mapToObject(String source, Class clazz) throws IOException; +} diff --git a/src/test/java/org/springframework/data/elasticsearch/core/CustomEntityMapper.java b/src/test/java/org/springframework/data/elasticsearch/core/CustomEntityMapper.java new file mode 100644 index 000000000..38266fd66 --- /dev/null +++ b/src/test/java/org/springframework/data/elasticsearch/core/CustomEntityMapper.java @@ -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 mapToObject(String source, Class clazz) throws IOException { + return null; + } +} diff --git a/src/test/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplateCustomMapperTests.java b/src/test/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplateCustomMapperTests.java new file mode 100644 index 000000000..7bade55b1 --- /dev/null +++ b/src/test/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplateCustomMapperTests.java @@ -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)); + } + +} diff --git a/src/test/resources/elasticsearch-template-custom-mapper.xml b/src/test/resources/elasticsearch-template-custom-mapper.xml new file mode 100644 index 000000000..98ab707f9 --- /dev/null +++ b/src/test/resources/elasticsearch-template-custom-mapper.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + \ No newline at end of file