DATAES-33 : Mapping of Parent-Child Relationships

This commit is contained in:
mohsin.husen
2014-01-04 14:35:53 +00:00
parent bc1abd96e8
commit 5922695e34
14 changed files with 370 additions and 7 deletions

View File

@@ -0,0 +1,34 @@
/*
* 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.annotations;
import java.lang.annotation.*;
import org.springframework.data.annotation.Persistent;
/**
* Parent
*
* @author Philipp Jardas
*/
@Persistent
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Parent {
String type();
}

View File

@@ -122,7 +122,7 @@ public class ElasticsearchTemplate implements ElasticsearchOperations {
try {
XContentBuilder xContentBuilder = buildMapping(clazz, persistentEntity.getIndexType(), persistentEntity
.getIdProperty().getFieldName());
.getIdProperty().getFieldName(), persistentEntity.getParentType());
return requestBuilder.setSource(xContentBuilder).execute().actionGet().isAcknowledged();
} catch (Exception e) {
throw new ElasticsearchException("Failed to build mapping for " + clazz.getSimpleName(), e);
@@ -544,6 +544,11 @@ public class ElasticsearchTemplate implements ElasticsearchOperations {
indexRequestBuilder.setVersion(query.getVersion());
indexRequestBuilder.setVersionType(EXTERNAL);
}
if (query.getParentId() != null) {
indexRequestBuilder.setParent(query.getParentId());
}
return indexRequestBuilder;
} catch (IOException e) {
throw new ElasticsearchException("failed to index the document [id: " + query.getId() + "]", e);

View File

@@ -32,6 +32,7 @@ import java.util.Map;
import static org.apache.commons.lang.StringUtils.EMPTY;
import static org.apache.commons.lang.StringUtils.isNotBlank;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.springframework.util.StringUtils.hasText;
/**
* @author Rizwan Idrees
@@ -48,6 +49,7 @@ class MappingBuilder {
public static final String FIELD_SEARCH_ANALYZER = "search_analyzer";
public static final String FIELD_INDEX_ANALYZER = "index_analyzer";
public static final String FIELD_PROPERTIES = "properties";
public static final String FIELD_PARENT = "_parent";
public static final String INDEX_VALUE_NOT_ANALYZED = "not_analyzed";
public static final String TYPE_VALUE_STRING = "string";
@@ -55,8 +57,16 @@ class MappingBuilder {
private static SimpleTypeHolder SIMPLE_TYPE_HOLDER = new SimpleTypeHolder();
static XContentBuilder buildMapping(Class clazz, String indexType, String idFieldName) throws IOException {
XContentBuilder xContentBuilder = jsonBuilder().startObject().startObject(indexType).startObject(FIELD_PROPERTIES);
static XContentBuilder buildMapping(Class clazz, String indexType, String idFieldName, String parentType) throws IOException {
XContentBuilder mapping = jsonBuilder().startObject().startObject(indexType);
// Parent
if (hasText(parentType)) {
mapping.startObject(FIELD_PARENT).field(FIELD_TYPE,parentType).endObject();
}
// Properties
XContentBuilder xContentBuilder = mapping.startObject(FIELD_PROPERTIES);
mapEntity(xContentBuilder, clazz, true, idFieldName, EMPTY, false);

View File

@@ -39,4 +39,8 @@ public interface ElasticsearchPersistentEntity<T> extends PersistentEntity<T, El
String getIndexStoreType();
ElasticsearchPersistentProperty getVersionProperty();
String getParentType();
ElasticsearchPersistentProperty getParentIdProperty();
}

View File

@@ -21,8 +21,8 @@ import org.springframework.context.ApplicationContextAware;
import org.springframework.context.expression.BeanFactoryAccessor;
import org.springframework.context.expression.BeanFactoryResolver;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Parent;
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;
@@ -49,6 +49,8 @@ public class SimpleElasticsearchPersistentEntity<T> extends BasicPersistentEntit
private short replicas;
private String refreshInterval;
private String indexStoreType;
private String parentType;
private ElasticsearchPersistentProperty parentIdProperty;
public SimpleElasticsearchPersistentEntity(TypeInformation<T> typeInformation) {
super(typeInformation);
@@ -104,9 +106,29 @@ public class SimpleElasticsearchPersistentEntity<T> extends BasicPersistentEntit
return refreshInterval;
}
@Override
public String getParentType() {
return parentType;
}
@Override
public ElasticsearchPersistentProperty getParentIdProperty() {
return parentIdProperty;
}
@Override
public void addPersistentProperty(ElasticsearchPersistentProperty property) {
super.addPersistentProperty(property);
Parent parent = property.getField().getAnnotation(Parent.class);
if (parent != null) {
Assert.isNull(this.parentIdProperty, "Only one field can hold a @Parent annotation");
Assert.isNull(this.parentType, "Only one field can hold a @Parent annotation");
Assert.isTrue(property.getType() == String.class, "Parent ID property should be String");
this.parentIdProperty = property;
this.parentType = parent.type();
}
if (property.isVersionProperty()) {
Assert.isTrue(property.getType() == Long.class, "Version property should be Long");
}

View File

@@ -30,6 +30,7 @@ public class IndexQuery {
private String indexName;
private String type;
private String source;
private String parentId;
public String getId() {
return id;
@@ -78,4 +79,12 @@ public class IndexQuery {
public void setSource(String source) {
this.source = source;
}
public String getParentId() {
return parentId;
}
public void setParentId(String parentId) {
this.parentId = parentId;
}
}

View File

@@ -240,6 +240,7 @@ public abstract class AbstractElasticsearchRepository<T, ID extends Serializable
query.setObject(entity);
query.setId(stringIdRepresentation(extractIdFromBean(entity)));
query.setVersion(extractVersionFromBean(entity));
query.setParentId(extractParentIdFromBean(entity));
return query;
}
@@ -302,4 +303,10 @@ public abstract class AbstractElasticsearchRepository<T, ID extends Serializable
return null;
}
private String extractParentIdFromBean(T entity) {
if (entityInformation != null) {
return entityInformation.getParentId(entity);
}
return null;
}
}

View File

@@ -35,4 +35,6 @@ public interface ElasticsearchEntityInformation<T, ID extends Serializable> exte
String getType();
Long getVersion(T entity);
String getParentId(T entity);
}

View File

@@ -104,4 +104,17 @@ public class MappingElasticsearchEntityInformation<T, ID extends Serializable> e
}
return null;
}
@Override
public String getParentId(T entity) {
ElasticsearchPersistentProperty parentProperty = entityMetadata.getParentIdProperty();
try {
if (parentProperty != null) {
return (String) BeanWrapper.create(entity, null).getProperty(parentProperty);
}
} catch (Exception e) {
throw new IllegalStateException("failed to load parent ID: " + e, e);
}
return null;
}
}