DATAES-209 - Dynamic mapping using @Mapping Annotation at field level

fix test

fix comment in test
This commit is contained in:
Ted Liang 2015-10-31 22:37:50 +11:00 committed by Mohsin Husen
parent 428cac3642
commit 403a85b094
6 changed files with 190 additions and 3 deletions

View File

@ -26,7 +26,7 @@ import org.springframework.data.annotation.Persistent;
@Persistent
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
@Target({ElementType.TYPE, ElementType.FIELD})
public @interface Mapping {
String mappingPath() default "";

View File

@ -1,5 +1,5 @@
/*
* Copyright 2014 the original author or authors.
* Copyright 2014-2016 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.
@ -29,11 +29,11 @@ import org.apache.commons.lang.math.NumberUtils;
import org.elasticsearch.common.lang3.StringUtils;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.springframework.core.GenericCollectionTypeResolver;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.annotation.Transient;
import org.springframework.data.elasticsearch.annotations.*;
import org.springframework.data.elasticsearch.core.completion.Completion;
import org.springframework.data.elasticsearch.core.geo.GeoPoint;
import org.springframework.data.geo.*;
import org.springframework.data.mapping.model.SimpleTypeHolder;
import org.springframework.data.util.ClassTypeInformation;
import org.springframework.data.util.TypeInformation;
@ -112,6 +112,17 @@ class MappingBuilder {
continue;
}
if (field.isAnnotationPresent(Mapping.class)) {
String mappingPath = field.getAnnotation(Mapping.class).mappingPath();
if (isNotBlank(mappingPath)) {
ClassPathResource mappings = new ClassPathResource(mappingPath);
if (mappings.exists()) {
xContentBuilder.rawField(field.getName(), mappings.getInputStream());
continue;
}
}
}
boolean isGeoPointField = isGeoPointField(field);
boolean isCompletionField = isCompletionField(field);

View File

@ -0,0 +1,51 @@
/*
* Copyright 2014 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.entities;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Mapping;
/**
* Sample FieldDynamicMappingEntity for test dynamic mapping using @Mapping Annotation at field level
*
* @author Ted Liang
*/
@Document(indexName = "test-field-mapping-index", type = "test-field-mapping-type")
public class FieldDynamicMappingEntity {
@Id
private String id;
@Mapping(mappingPath = "/mappings/test-field-mappings.json")
private byte[] file;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public byte[] getFile() {
return file;
}
public void setFile(byte[] file) {
this.file = file;
}
}

View File

@ -0,0 +1,28 @@
/*
* Copyright 2014 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.repositories.setting;
import org.springframework.data.elasticsearch.entities.FieldDynamicMappingEntity;
import org.springframework.data.elasticsearch.repository.ElasticsearchCrudRepository;
/**
* FieldDynamicMappingEntityRepository
*
* @author Ted Liang
*/
public interface FieldDynamicMappingEntityRepository extends ElasticsearchCrudRepository<FieldDynamicMappingEntity, String> {
}

View File

@ -0,0 +1,87 @@
/*
* Copyright 2014 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.repositories.setting;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertThat;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.entities.FieldDynamicMappingEntity;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* FieldDynamicMappingEntityRepositoryTests
*
* @author Ted Liang
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:dynamic-settings-test.xml")
public class FieldDynamicMappingEntityRepositoryTests {
@Autowired
private FieldDynamicMappingEntityRepository repository;
@Autowired
private ElasticsearchTemplate elasticsearchTemplate;
@Before
public void before() {
elasticsearchTemplate.deleteIndex(FieldDynamicMappingEntity.class);
elasticsearchTemplate.createIndex(FieldDynamicMappingEntity.class);
elasticsearchTemplate.putMapping(FieldDynamicMappingEntity.class);
elasticsearchTemplate.refresh(FieldDynamicMappingEntity.class, true);
}
/*
DATAES-209
*/
@Test
public void shouldCreateMappingWithMappingAnnotationAtFieldLevel() {
//given
//then
Map mapping = elasticsearchTemplate.getMapping(FieldDynamicMappingEntity.class);
assertThat(mapping, is(notNullValue()));
Map properties = (Map) mapping.get("properties");
assertThat(properties, is(notNullValue()));
assertThat(properties.containsKey("file"), is(true));
Map file = (Map) properties.get("file");
assertThat(file, is(notNullValue()));
assertThat(((String) file.get("type")), is("string"));
assertThat(file.containsKey("fields"), is(true));
Map fields = (Map) file.get("fields");
assertThat(fields, is(notNullValue()));
assertThat(fields.containsKey("content"), is(true));
Map content = (Map) fields.get("content");
assertThat(content, is(notNullValue()));
assertThat((String)content.get("type"), is("string"));
assertThat((String)content.get("term_vector"), is("with_positions_offsets"));
assertThat((Boolean)content.get("store"), is(Boolean.TRUE));
}
}

View File

@ -0,0 +1,10 @@
{
"type": "string",
"fields": {
"content": {
"type": "string",
"term_vector":"with_positions_offsets",
"store": true
}
}
}