Fixed issue related to nested objects

This commit is contained in:
Mohsin Husen 2013-02-01 12:37:03 +00:00
parent 8d65d92b4a
commit 92a18bd0fb
14 changed files with 184 additions and 12 deletions

View File

@ -101,7 +101,7 @@ class CriteriaQueryProcessor {
((BoolQueryBuilder) query).should(fieldQuery(fieldName, item));
}
break;
}
}
return query;
}

View File

@ -21,6 +21,7 @@ import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.elasticsearch.ElasticsearchException;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter;
import org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter;
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity;
@ -234,6 +235,8 @@ public class ElasticsearchTemplate implements ElasticsearchOperations {
}
private ElasticsearchPersistentEntity getPersistentEntityFor(Class clazz){
Assert.isTrue(clazz.isAnnotationPresent(Document.class), "Unable to identify index name. " +
clazz.getSimpleName() + " is not a Document. Make sure the document class is annotated with @Document(indexName=\"foo\")");
return elasticsearchConverter.getMappingContext().getPersistentEntity(clazz);
}

View File

@ -46,12 +46,12 @@ public class SimpleElasticsearchPersistentEntity<T> extends BasicPersistentEntit
super(typeInformation);
this.context = new StandardEvaluationContext();
Class<T> clazz = typeInformation.getType();
Assert.isTrue(clazz.isAnnotationPresent(Document.class),
clazz.getSimpleName() + " is not a Document. Make sure the document class is annotated with @Document(indexName=\"foo\")");
Document document = clazz.getAnnotation(Document.class);
Assert.hasText(document.indexName(), " Unknown indexName. Make sure the indexName is defined. e.g @Document(indexName=\"foo\")");
this.indexName = typeInformation.getType().getAnnotation(Document.class).indexName();
this.indexType = hasText(document.type())? document.type() : clazz.getSimpleName().toLowerCase(Locale.ENGLISH);
if(clazz.isAnnotationPresent(Document.class)){
Document document = clazz.getAnnotation(Document.class);
Assert.hasText(document.indexName(), " Unknown indexName. Make sure the indexName is defined. e.g @Document(indexName=\"foo\")");
this.indexName = typeInformation.getType().getAnnotation(Document.class).indexName();
this.indexType = hasText(document.type())? document.type() : clazz.getSimpleName().toLowerCase(Locale.ENGLISH);
}
}
@Override

View File

@ -0,0 +1,22 @@
package org.springframework.data.elasticsearch;
public class Author {
private String name;
private String id;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}

View File

@ -0,0 +1,34 @@
package org.springframework.data.elasticsearch;
import org.springframework.data.elasticsearch.annotations.Document;
@Document(indexName = "book",type = "book")
public class Book {
private String id;
private String name;
private Author author;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Author getAuthor() {
return author;
}
public void setAuthor(Author author) {
this.author = author;
}
}

View File

@ -0,0 +1,43 @@
package org.springframework.data.elasticsearch;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.data.elasticsearch.repositories.SampleElasticSearchBookRepository;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;
import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertThat;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:/repository-test-nested-object.xml")
public class NestedObjectTest{
@Resource
private SampleElasticSearchBookRepository repository;
@Test
public void shouldIndexNestedObject(){
//given
String id = randomAlphanumeric(5);
Book book = new Book();
book.setId(id);
book.setName("xyz");
Author author = new Author();
author.setId("1");
author.setName("ABC");
book.setAuthor(author);
//when
repository.save(book);
//then
assertThat(repository.findOne(id), is(notNullValue()));
}
}

View File

@ -1,11 +1,12 @@
package org.springframework.data.elasticsearch;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.annotation.Id;
@Document(indexName = "Foo")
public class NonDocumentEntity {
@Id
private String someId;
private String someField1;
private String someField2;

View File

@ -0,0 +1,24 @@
package org.springframework.data.elasticsearch;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.data.elasticsearch.repositories.NonDocumentEntityRepository;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:/repository-non-document-entity.xml")
public class NonDocumentEntityTest {
@Resource
private NonDocumentEntityRepository nonDocumentEntityRepository;
@Test(expected = IllegalArgumentException.class)
public void shouldNotIndexEntitiesWhichAreNotADocument(){
//when
nonDocumentEntityRepository.save(new NonDocumentEntity());
}
}

View File

@ -333,6 +333,4 @@ public class ElasticsearchTemplateTest {
assertThat(sampleEntity1, is(notNullValue()));
}
}

View File

@ -16,7 +16,6 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

View File

@ -0,0 +1,8 @@
package org.springframework.data.elasticsearch.repositories;
import org.springframework.data.elasticsearch.NonDocumentEntity;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
public interface NonDocumentEntityRepository extends ElasticsearchRepository<NonDocumentEntity,String> {
}

View File

@ -0,0 +1,7 @@
package org.springframework.data.elasticsearch.repositories;
import org.springframework.data.elasticsearch.Book;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
public interface SampleElasticSearchBookRepository extends ElasticsearchRepository<Book,String> {
}

View File

@ -0,0 +1,16 @@
<?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.2.xsd">
<elasticsearch:node-client id="client" local="true"/>
<bean name="elasticsearchTemplate" class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">
<constructor-arg name="client" ref="client"/>
</bean>
<elasticsearch:repositories base-package="org.springframework.data.elasticsearch"/>
</beans>

View File

@ -0,0 +1,17 @@
<?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.2.xsd">
<elasticsearch:node-client id="client" local="true"/>
<bean name="elasticsearchTemplate" class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">
<constructor-arg name="client" ref="client"/>
</bean>
<elasticsearch:repositories base-package="org.springframework.data.elasticsearch"/>
</beans>