added version support and upgraded the version of elasticsearch to 0.20.4
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
package org.springframework.data.elasticsearch.annotations;
|
||||
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.FIELD)
|
||||
@Documented
|
||||
public @interface Version {
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -21,5 +21,5 @@ public interface ElasticsearchPersistentEntity<T> extends PersistentEntity<T, El
|
||||
|
||||
String getIndexName();
|
||||
String getIndexType();
|
||||
|
||||
ElasticsearchPersistentProperty getVersionProperty();
|
||||
}
|
||||
|
||||
@@ -22,6 +22,8 @@ public interface ElasticsearchPersistentProperty extends PersistentProperty<Elas
|
||||
|
||||
String getFieldName();
|
||||
|
||||
boolean isVersionProperty();
|
||||
|
||||
public enum PropertyToFieldNameConverter implements Converter<ElasticsearchPersistentProperty, String> {
|
||||
|
||||
INSTANCE;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,5 +28,6 @@ public interface ElasticsearchEntityInformation<T, ID extends Serializable> exte
|
||||
String getIdAttribute();
|
||||
String getIndexName();
|
||||
String getType();
|
||||
|
||||
String getVersionAttribute();
|
||||
Long getVersion(T entity);
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user