added version support and upgraded the version of elasticsearch to 0.20.4

This commit is contained in:
Rizwan Idrees
2013-02-05 12:58:33 +00:00
parent 0818015434
commit ad24967b53
16 changed files with 233 additions and 47 deletions

View File

@@ -1,16 +1,9 @@
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;
}
private String id;
private String name;
public String getId() {
return id;
@@ -19,4 +12,12 @@ public class Author {
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

View File

@@ -4,9 +4,10 @@ import org.springframework.data.elasticsearch.annotations.Document;
@Document(indexName = "book",type = "book")
public class Book {
private String id;
private String name;
private Author author;
private String id;
private String name;
private Author author;
public String getId() {
return id;

View File

@@ -4,6 +4,7 @@ import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Version;
@Document(indexName = "test-index", type = "test-type")
public class SampleEntity {
@@ -14,6 +15,8 @@ public class SampleEntity {
private String message;
private int rate;
private boolean available;
@Version
private Long version;
public String getId() {
return id;
@@ -55,6 +58,14 @@ public class SampleEntity {
this.available = available;
}
public Long getVersion() {
return version;
}
public void setVersion(Long version) {
this.version = version;
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof SampleEntity)) {
@@ -67,6 +78,9 @@ public class SampleEntity {
return new EqualsBuilder().append(this.id, rhs.id)
.append(this.type, rhs.type)
.append(this.message, rhs.message)
.append(this.rate,rhs.rate)
.append(this.available,rhs.available)
.append(this.version,rhs.version)
.isEquals();
}
@@ -76,6 +90,9 @@ public class SampleEntity {
.append(id)
.append(type)
.append(message)
.append(rate)
.append(available)
.append(version)
.toHashCode();
}
}

View File

@@ -0,0 +1,90 @@
package org.springframework.data.elasticsearch.core.mapping;
import org.junit.Test;
import org.springframework.data.elasticsearch.annotations.Version;
import org.springframework.data.mapping.model.MappingException;
import org.springframework.data.mapping.model.SimpleTypeHolder;
import org.springframework.data.util.ClassTypeInformation;
import org.springframework.data.util.TypeInformation;
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
public class SimpleElasticsearchPersistentEntityTest {
@Test(expected = IllegalArgumentException.class)
public void shouldThrowExceptionGivenVersionPropertyIsNotLong() throws NoSuchFieldException, IntrospectionException {
//given
TypeInformation typeInformation = ClassTypeInformation.from(EntityWithWrongVersionType.class);
SimpleElasticsearchPersistentProperty persistentProperty =
new SimpleElasticsearchPersistentProperty(EntityWithWrongVersionType.class.getDeclaredField("version"),
new PropertyDescriptor("version", EntityWithWrongVersionType.class),
new SimpleElasticsearchPersistentEntity<EntityWithWrongVersionType>(typeInformation),
new SimpleTypeHolder());
//when
new SimpleElasticsearchPersistentEntity(typeInformation).addPersistentProperty(persistentProperty);
}
@Test(expected = MappingException.class)
public void shouldThrowExceptionGivenMultipleVersionPropertiesArePresent() throws NoSuchFieldException, IntrospectionException {
//given
TypeInformation typeInformation = ClassTypeInformation.from(EntityWithMultipleVersionField.class);
SimpleElasticsearchPersistentProperty persistentProperty1 =
new SimpleElasticsearchPersistentProperty(EntityWithMultipleVersionField.class.getDeclaredField("version1"),
new PropertyDescriptor("version1", EntityWithMultipleVersionField.class),
new SimpleElasticsearchPersistentEntity<EntityWithMultipleVersionField>(typeInformation),
new SimpleTypeHolder());
SimpleElasticsearchPersistentProperty persistentProperty2 =
new SimpleElasticsearchPersistentProperty(EntityWithMultipleVersionField.class.getDeclaredField("version2"),
new PropertyDescriptor("version2", EntityWithMultipleVersionField.class),
new SimpleElasticsearchPersistentEntity<EntityWithMultipleVersionField>(typeInformation),
new SimpleTypeHolder());
SimpleElasticsearchPersistentEntity simpleElasticsearchPersistentEntity = new SimpleElasticsearchPersistentEntity(typeInformation);
simpleElasticsearchPersistentEntity.addPersistentProperty(persistentProperty1);
//when
simpleElasticsearchPersistentEntity.addPersistentProperty(persistentProperty2);
}
private class EntityWithWrongVersionType {
@Version
private String version;
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
}
private class EntityWithMultipleVersionField{
@Version
private Long version1;
@Version
private Long version2;
public Long getVersion1() {
return version1;
}
public void setVersion1(Long version1) {
this.version1 = version1;
}
public Long getVersion2() {
return version2;
}
public void setVersion2(Long version2) {
this.version2 = version2;
}
}
}

View File

@@ -27,7 +27,6 @@ public class RepositoryTest {
@Resource
private SampleElasticsearchRepository repository;
@Test
public void shouldDoBulkIndexDocument(){
//given
@@ -148,7 +147,7 @@ public class RepositoryTest {
@Test
@Ignore
public void testFindAllByIdQuery(){
public void shouldFindAllByIdQuery(){
//todo : find solution for findAll(Iterable<Ids> ids)
//given
String documentId = randomNumeric(5);
@@ -171,31 +170,27 @@ public class RepositoryTest {
}
@Test
public void testSaveIterableEntities(){
//given
public void shouldSaveIterableEntities(){
//given
String documentId = randomNumeric(5);
SampleEntity sampleEntity = new SampleEntity();
sampleEntity.setId(documentId);
sampleEntity.setMessage("hello world.");
SampleEntity sampleEntity1 = new SampleEntity();
sampleEntity1.setId(documentId);
sampleEntity1.setMessage("hello world.");
String documentId2 = randomNumeric(5);
SampleEntity sampleEntity2 = new SampleEntity();
sampleEntity2.setId(documentId2);
sampleEntity2.setMessage("hello world.");
Iterable<SampleEntity> sampleEntities = Arrays.asList(sampleEntity,sampleEntity2);
Iterable<SampleEntity> sampleEntities = Arrays.asList(sampleEntity1,sampleEntity2);
//when
repository.save(sampleEntities);
//then
Page<SampleEntity> entities = repository.search(fieldQuery("id", documentId), new PageRequest(0, 50));
assertNotNull(entities);
}
@Test
public void testDocumentExistById(){
public void shouldReturnTrueGivenDocumentWithIdExists(){
//given
String documentId = randomNumeric(5);
SampleEntity sampleEntity = new SampleEntity();
@@ -211,7 +206,7 @@ public class RepositoryTest {
}
@Test
public void testSearchForGivenSearchQuery(){
public void shouldReturnResultsForGivenSearchQuery(){
//given
String documentId = randomNumeric(5);
SampleEntity sampleEntity = new SampleEntity();
@@ -227,7 +222,7 @@ public class RepositoryTest {
}
@Test
public void testDeleteAll(){
public void shouldDeleteAll(){
//when
repository.deleteAll();
//then
@@ -238,7 +233,7 @@ public class RepositoryTest {
}
@Test
public void testDeleteByEntity(){
public void shouldDeleteEntity(){
//given
String documentId = randomNumeric(5);
SampleEntity sampleEntity = new SampleEntity();
@@ -255,28 +250,30 @@ public class RepositoryTest {
}
@Test
public void testSearchForReturnIterableEntities(){
public void shouldReturnIterableEntities(){
//given
String documentId = randomNumeric(5);
SampleEntity sampleEntity = new SampleEntity();
sampleEntity.setId(documentId);
sampleEntity.setMessage("hello world.");
repository.save(sampleEntity);
SampleEntity sampleEntity1 = new SampleEntity();
sampleEntity1.setId(documentId);
sampleEntity1.setMessage("hello world.");
sampleEntity1.setVersion(System.currentTimeMillis());
repository.save(sampleEntity1);
String documentId2 = randomNumeric(5);
SampleEntity sampleEntity2 = new SampleEntity();
sampleEntity2.setId(documentId2);
sampleEntity2.setMessage("hello world.");
sampleEntity2.setVersion(System.currentTimeMillis());
repository.save(sampleEntity2);
//when
Iterable<SampleEntity> sampleEntities=repository.search(fieldQuery("id",documentId));
//then
//then
assertNotNull("sample entities cant be null..", sampleEntities);
}
@Test
public void testDeleteIterableEntities(){
public void shouldDeleteIterableEntities(){
//given
String documentId = randomNumeric(5);
SampleEntity sampleEntity = new SampleEntity();
@@ -291,10 +288,8 @@ public class RepositoryTest {
repository.save(sampleEntity);
Iterable<SampleEntity> sampleEntities = Arrays.asList(sampleEntity,sampleEntity2);
//when
repository.delete(sampleEntities);
//then
Page<SampleEntity> entities = repository.search(fieldQuery("id", documentId), new PageRequest(0,50));
assertThat(entities.getTotalElements(),equalTo(0L));
@@ -303,11 +298,12 @@ public class RepositoryTest {
}
@Test
public void testIndexEntity(){
public void shouldIndexEntity(){
//given
String documentId = randomNumeric(5);
SampleEntity sampleEntity = new SampleEntity();
sampleEntity.setId(documentId);
sampleEntity.setVersion(System.currentTimeMillis());
sampleEntity.setMessage("some message");
//when
repository.index(sampleEntity);
@@ -318,7 +314,7 @@ public class RepositoryTest {
@Test
@Ignore("By default, the search request will fail if there is no mapping associated with a field. The ignore_unmapped option allows to ignore fields that have no mapping and not sort by them")
public void testFindBySort(){
public void shouldSortByGivenField(){
//todo
//given
String documentId = randomNumeric(5);
@@ -332,12 +328,9 @@ public class RepositoryTest {
sampleEntity2.setId(documentId2);
sampleEntity2.setMessage("B.hello world.");
repository.save(sampleEntity2);
//when
Iterable<SampleEntity> sampleEntities=repository.findAll(new Sort(new Sort.Order(Sort.Direction.ASC,"message")));
//then
assertThat(sampleEntities,is(notNullValue()));
}
}