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

@@ -0,0 +1,10 @@
package org.springframework.data.elasticsearch.annotations;
import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@Documented
public @interface Version {
}

View File

@@ -216,8 +216,12 @@ public class ElasticsearchTemplate implements ElasticsearchOperations {
private IndexRequestBuilder prepareIndex(IndexQuery query){
try {
ElasticsearchPersistentEntity persistentEntity = getPersistentEntityFor(query.getObject().getClass());
return client.prepareIndex(persistentEntity.getIndexName(), persistentEntity.getIndexType(), query.getId())
IndexRequestBuilder indexRequestBuilder = client.prepareIndex(persistentEntity.getIndexName(), persistentEntity.getIndexType(), query.getId())
.setSource(objectMapper.writeValueAsString(query.getObject()));
if(query.getVersion() != null){
indexRequestBuilder.setVersion(query.getVersion());
}
return indexRequestBuilder;
} catch (IOException e) {
throw new ElasticsearchException("failed to index the document [id: " + query.getId() +"]",e);
}

View File

@@ -21,5 +21,5 @@ public interface ElasticsearchPersistentEntity<T> extends PersistentEntity<T, El
String getIndexName();
String getIndexType();
ElasticsearchPersistentProperty getVersionProperty();
}

View File

@@ -22,6 +22,8 @@ public interface ElasticsearchPersistentProperty extends PersistentProperty<Elas
String getFieldName();
boolean isVersionProperty();
public enum PropertyToFieldNameConverter implements Converter<ElasticsearchPersistentProperty, String> {
INSTANCE;

View File

@@ -22,6 +22,7 @@ import org.springframework.context.expression.BeanFactoryAccessor;
import org.springframework.context.expression.BeanFactoryResolver;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.mapping.model.BasicPersistentEntity;
import org.springframework.data.mapping.model.MappingException;
import org.springframework.data.util.TypeInformation;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.util.Assert;
@@ -41,6 +42,7 @@ public class SimpleElasticsearchPersistentEntity<T> extends BasicPersistentEntit
private final StandardEvaluationContext context;
private String indexName;
private String indexType;
private ElasticsearchPersistentProperty versionProperty;
public SimpleElasticsearchPersistentEntity(TypeInformation<T> typeInformation) {
super(typeInformation);
@@ -70,4 +72,23 @@ public class SimpleElasticsearchPersistentEntity<T> extends BasicPersistentEntit
public String getIndexType() {
return indexType;
}
@Override
public ElasticsearchPersistentProperty getVersionProperty() {
return this.versionProperty;
}
@Override
public void addPersistentProperty(ElasticsearchPersistentProperty property) {
super.addPersistentProperty(property);
if(property.isVersionProperty()){
if (this.versionProperty != null) {
throw new MappingException(String.format(
"Attempt to add version property %s but already have property %s registered "
+ "as version. Check your mapping configuration!", property.getField(), versionProperty.getField()));
}
Assert.isTrue(property.getType() == Long.class, "Version property should be Long");
this.versionProperty = property;
}
}
}

View File

@@ -15,6 +15,7 @@
*/
package org.springframework.data.elasticsearch.core.mapping;
import org.springframework.data.elasticsearch.annotations.Version;
import org.springframework.data.mapping.Association;
import org.springframework.data.mapping.PersistentEntity;
import org.springframework.data.mapping.model.AnnotationBasedPersistentProperty;
@@ -55,7 +56,12 @@ public class SimpleElasticsearchPersistentProperty extends AnnotationBasedPersis
return super.isIdProperty() || SUPPORTED_ID_PROPERTY_NAMES.contains(getFieldName());
}
@Override
@Override
public boolean isVersionProperty(){
return field.isAnnotationPresent(Version.class);
}
@Override
protected Association<ElasticsearchPersistentProperty> createAssociation() {
return null;
}

View File

@@ -5,6 +5,7 @@ public class IndexQuery{
private String id;
private Object object;
private Long version;
public String getId() {
return id;
@@ -21,4 +22,12 @@ public class IndexQuery{
public void setObject(Object object) {
this.object = object;
}
public Long getVersion() {
return version;
}
public void setVersion(Long version) {
this.version = version;
}
}

View File

@@ -28,5 +28,6 @@ public interface ElasticsearchEntityInformation<T, ID extends Serializable> exte
String getIdAttribute();
String getIndexName();
String getType();
String getVersionAttribute();
Long getVersion(T entity);
}

View File

@@ -15,6 +15,8 @@
*/
package org.springframework.data.elasticsearch.repository.support;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity;
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty;
import org.springframework.data.mapping.model.BeanWrapper;
@@ -32,6 +34,7 @@ import java.io.Serializable;
public class MappingElasticsearchEntityInformation<T, ID extends Serializable> extends AbstractEntityInformation<T, ID>
implements ElasticsearchEntityInformation<T, ID> {
private static final Logger logger = LoggerFactory.getLogger(MappingElasticsearchEntityInformation.class);
private final ElasticsearchPersistentEntity<T> entityMetadata;
private final String indexName;
private final String type;
@@ -79,4 +82,24 @@ public class MappingElasticsearchEntityInformation<T, ID extends Serializable> e
public String getType() {
return type != null? type : entityMetadata.getIndexType();
}
@Override
public String getVersionAttribute() {
return entityMetadata.getVersionProperty().getFieldName();
}
@Override
public Long getVersion(T entity) {
ElasticsearchPersistentProperty versionProperty = entityMetadata.getIdProperty();
try {
if(versionProperty != null){
return (Long) BeanWrapper.create(entity, null).getProperty(versionProperty);
}
} catch (Exception e) {
logger.debug("failed to retrieve version", e);
}
return null;
}
}

View File

@@ -212,6 +212,7 @@ public class SimpleElasticsearchRepository<T> implements ElasticsearchRepository
IndexQuery query = new IndexQuery();
query.setObject(entity);
query.setId(extractIdFromBean(entity));
query.setVersion(extractVersionFromBean(entity));
return query;
}
@@ -266,4 +267,11 @@ public class SimpleElasticsearchRepository<T> implements ElasticsearchRepository
return null;
}
private Long extractVersionFromBean(T entity){
if (entityInformation != null) {
return entityInformation.getVersion(entity);
}
return null;
}
}