mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-06-22 20:12:11 +00:00
DATAES-33 : Mapping of Parent-Child Relationships
This commit is contained in:
parent
bc1abd96e8
commit
5922695e34
@ -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();
|
||||||
|
}
|
@ -122,7 +122,7 @@ public class ElasticsearchTemplate implements ElasticsearchOperations {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
XContentBuilder xContentBuilder = buildMapping(clazz, persistentEntity.getIndexType(), persistentEntity
|
XContentBuilder xContentBuilder = buildMapping(clazz, persistentEntity.getIndexType(), persistentEntity
|
||||||
.getIdProperty().getFieldName());
|
.getIdProperty().getFieldName(), persistentEntity.getParentType());
|
||||||
return requestBuilder.setSource(xContentBuilder).execute().actionGet().isAcknowledged();
|
return requestBuilder.setSource(xContentBuilder).execute().actionGet().isAcknowledged();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new ElasticsearchException("Failed to build mapping for " + clazz.getSimpleName(), 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.setVersion(query.getVersion());
|
||||||
indexRequestBuilder.setVersionType(EXTERNAL);
|
indexRequestBuilder.setVersionType(EXTERNAL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (query.getParentId() != null) {
|
||||||
|
indexRequestBuilder.setParent(query.getParentId());
|
||||||
|
}
|
||||||
|
|
||||||
return indexRequestBuilder;
|
return indexRequestBuilder;
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new ElasticsearchException("failed to index the document [id: " + query.getId() + "]", e);
|
throw new ElasticsearchException("failed to index the document [id: " + query.getId() + "]", e);
|
||||||
|
@ -32,6 +32,7 @@ import java.util.Map;
|
|||||||
import static org.apache.commons.lang.StringUtils.EMPTY;
|
import static org.apache.commons.lang.StringUtils.EMPTY;
|
||||||
import static org.apache.commons.lang.StringUtils.isNotBlank;
|
import static org.apache.commons.lang.StringUtils.isNotBlank;
|
||||||
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
|
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
|
||||||
|
import static org.springframework.util.StringUtils.hasText;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Rizwan Idrees
|
* @author Rizwan Idrees
|
||||||
@ -48,6 +49,7 @@ class MappingBuilder {
|
|||||||
public static final String FIELD_SEARCH_ANALYZER = "search_analyzer";
|
public static final String FIELD_SEARCH_ANALYZER = "search_analyzer";
|
||||||
public static final String FIELD_INDEX_ANALYZER = "index_analyzer";
|
public static final String FIELD_INDEX_ANALYZER = "index_analyzer";
|
||||||
public static final String FIELD_PROPERTIES = "properties";
|
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 INDEX_VALUE_NOT_ANALYZED = "not_analyzed";
|
||||||
public static final String TYPE_VALUE_STRING = "string";
|
public static final String TYPE_VALUE_STRING = "string";
|
||||||
@ -55,8 +57,16 @@ class MappingBuilder {
|
|||||||
|
|
||||||
private static SimpleTypeHolder SIMPLE_TYPE_HOLDER = new SimpleTypeHolder();
|
private static SimpleTypeHolder SIMPLE_TYPE_HOLDER = new SimpleTypeHolder();
|
||||||
|
|
||||||
static XContentBuilder buildMapping(Class clazz, String indexType, String idFieldName) throws IOException {
|
static XContentBuilder buildMapping(Class clazz, String indexType, String idFieldName, String parentType) throws IOException {
|
||||||
XContentBuilder xContentBuilder = jsonBuilder().startObject().startObject(indexType).startObject(FIELD_PROPERTIES);
|
|
||||||
|
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);
|
mapEntity(xContentBuilder, clazz, true, idFieldName, EMPTY, false);
|
||||||
|
|
||||||
|
@ -39,4 +39,8 @@ public interface ElasticsearchPersistentEntity<T> extends PersistentEntity<T, El
|
|||||||
String getIndexStoreType();
|
String getIndexStoreType();
|
||||||
|
|
||||||
ElasticsearchPersistentProperty getVersionProperty();
|
ElasticsearchPersistentProperty getVersionProperty();
|
||||||
|
|
||||||
|
String getParentType();
|
||||||
|
|
||||||
|
ElasticsearchPersistentProperty getParentIdProperty();
|
||||||
}
|
}
|
||||||
|
@ -21,8 +21,8 @@ import org.springframework.context.ApplicationContextAware;
|
|||||||
import org.springframework.context.expression.BeanFactoryAccessor;
|
import org.springframework.context.expression.BeanFactoryAccessor;
|
||||||
import org.springframework.context.expression.BeanFactoryResolver;
|
import org.springframework.context.expression.BeanFactoryResolver;
|
||||||
import org.springframework.data.elasticsearch.annotations.Document;
|
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.BasicPersistentEntity;
|
||||||
import org.springframework.data.mapping.model.MappingException;
|
|
||||||
import org.springframework.data.util.TypeInformation;
|
import org.springframework.data.util.TypeInformation;
|
||||||
import org.springframework.expression.spel.support.StandardEvaluationContext;
|
import org.springframework.expression.spel.support.StandardEvaluationContext;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
@ -49,6 +49,8 @@ public class SimpleElasticsearchPersistentEntity<T> extends BasicPersistentEntit
|
|||||||
private short replicas;
|
private short replicas;
|
||||||
private String refreshInterval;
|
private String refreshInterval;
|
||||||
private String indexStoreType;
|
private String indexStoreType;
|
||||||
|
private String parentType;
|
||||||
|
private ElasticsearchPersistentProperty parentIdProperty;
|
||||||
|
|
||||||
public SimpleElasticsearchPersistentEntity(TypeInformation<T> typeInformation) {
|
public SimpleElasticsearchPersistentEntity(TypeInformation<T> typeInformation) {
|
||||||
super(typeInformation);
|
super(typeInformation);
|
||||||
@ -104,9 +106,29 @@ public class SimpleElasticsearchPersistentEntity<T> extends BasicPersistentEntit
|
|||||||
return refreshInterval;
|
return refreshInterval;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getParentType() {
|
||||||
|
return parentType;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ElasticsearchPersistentProperty getParentIdProperty() {
|
||||||
|
return parentIdProperty;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void addPersistentProperty(ElasticsearchPersistentProperty property) {
|
public void addPersistentProperty(ElasticsearchPersistentProperty property) {
|
||||||
super.addPersistentProperty(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()) {
|
if (property.isVersionProperty()) {
|
||||||
Assert.isTrue(property.getType() == Long.class, "Version property should be Long");
|
Assert.isTrue(property.getType() == Long.class, "Version property should be Long");
|
||||||
}
|
}
|
||||||
|
@ -30,6 +30,7 @@ public class IndexQuery {
|
|||||||
private String indexName;
|
private String indexName;
|
||||||
private String type;
|
private String type;
|
||||||
private String source;
|
private String source;
|
||||||
|
private String parentId;
|
||||||
|
|
||||||
public String getId() {
|
public String getId() {
|
||||||
return id;
|
return id;
|
||||||
@ -78,4 +79,12 @@ public class IndexQuery {
|
|||||||
public void setSource(String source) {
|
public void setSource(String source) {
|
||||||
this.source = source;
|
this.source = source;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getParentId() {
|
||||||
|
return parentId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setParentId(String parentId) {
|
||||||
|
this.parentId = parentId;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -240,6 +240,7 @@ public abstract class AbstractElasticsearchRepository<T, ID extends Serializable
|
|||||||
query.setObject(entity);
|
query.setObject(entity);
|
||||||
query.setId(stringIdRepresentation(extractIdFromBean(entity)));
|
query.setId(stringIdRepresentation(extractIdFromBean(entity)));
|
||||||
query.setVersion(extractVersionFromBean(entity));
|
query.setVersion(extractVersionFromBean(entity));
|
||||||
|
query.setParentId(extractParentIdFromBean(entity));
|
||||||
return query;
|
return query;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -302,4 +303,10 @@ public abstract class AbstractElasticsearchRepository<T, ID extends Serializable
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String extractParentIdFromBean(T entity) {
|
||||||
|
if (entityInformation != null) {
|
||||||
|
return entityInformation.getParentId(entity);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -35,4 +35,6 @@ public interface ElasticsearchEntityInformation<T, ID extends Serializable> exte
|
|||||||
String getType();
|
String getType();
|
||||||
|
|
||||||
Long getVersion(T entity);
|
Long getVersion(T entity);
|
||||||
|
|
||||||
|
String getParentId(T entity);
|
||||||
}
|
}
|
||||||
|
@ -104,4 +104,17 @@ public class MappingElasticsearchEntityInformation<T, ID extends Serializable> e
|
|||||||
}
|
}
|
||||||
return null;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,29 @@
|
|||||||
|
/*
|
||||||
|
* 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;
|
||||||
|
|
||||||
|
import org.springframework.data.annotation.Id;
|
||||||
|
import org.springframework.data.elasticsearch.annotations.Document;
|
||||||
|
/**
|
||||||
|
* MinimalEntity
|
||||||
|
*
|
||||||
|
* @author Philipp Jardas
|
||||||
|
*/
|
||||||
|
@Document(indexName = "index", type = "type")
|
||||||
|
public class MinimalEntity {
|
||||||
|
@Id
|
||||||
|
private String id;
|
||||||
|
}
|
@ -0,0 +1,95 @@
|
|||||||
|
/*
|
||||||
|
* 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;
|
||||||
|
|
||||||
|
import org.springframework.core.style.ToStringCreator;
|
||||||
|
import org.springframework.data.annotation.Id;
|
||||||
|
import org.springframework.data.annotation.PersistenceConstructor;
|
||||||
|
import org.springframework.data.elasticsearch.annotations.*;
|
||||||
|
/**
|
||||||
|
* ParentEntity
|
||||||
|
*
|
||||||
|
* @author Philipp Jardas
|
||||||
|
*/
|
||||||
|
@Document(indexName = ParentEntity.INDEX, type = ParentEntity.PARENT_TYPE, indexStoreType = "memory", shards = 1, replicas = 0, refreshInterval = "-1")
|
||||||
|
public class ParentEntity {
|
||||||
|
public static final String INDEX = "parent-child";
|
||||||
|
public static final String PARENT_TYPE = "parent-entity";
|
||||||
|
public static final String CHILD_TYPE = "child-entity";
|
||||||
|
|
||||||
|
@Id
|
||||||
|
private String id;
|
||||||
|
@Field(type = FieldType.String, index = FieldIndex.analyzed, store = true)
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
public ParentEntity() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public ParentEntity(String id, String name) {
|
||||||
|
this.id = id;
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return new ToStringCreator(this).append("id", id).append("name", name).toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Document(indexName = INDEX, type = CHILD_TYPE, indexStoreType = "memory", shards = 1, replicas = 0, refreshInterval = "-1")
|
||||||
|
public static class ChildEntity {
|
||||||
|
@Id
|
||||||
|
private String id;
|
||||||
|
@Field(type = FieldType.String, store = true)
|
||||||
|
@Parent(type = PARENT_TYPE)
|
||||||
|
private String parentId;
|
||||||
|
@Field(type = FieldType.String, index = FieldIndex.analyzed, store = true)
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
public ChildEntity() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public ChildEntity(String id, String parentId, String name) {
|
||||||
|
this.id = id;
|
||||||
|
this.parentId = parentId;
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getParentId() {
|
||||||
|
return parentId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return new ToStringCreator(this).append("id", id).append("parentId", parentId).append("name", name).toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,126 @@
|
|||||||
|
/*
|
||||||
|
* 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.core;
|
||||||
|
|
||||||
|
import static org.elasticsearch.index.query.QueryBuilders.hasChildQuery;
|
||||||
|
import static org.elasticsearch.index.query.QueryBuilders.topChildrenQuery;
|
||||||
|
import static org.hamcrest.Matchers.*;
|
||||||
|
import static org.junit.Assert.assertThat;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.elasticsearch.index.query.QueryBuilder;
|
||||||
|
import org.elasticsearch.index.query.QueryBuilders;
|
||||||
|
import org.junit.*;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.data.elasticsearch.ParentEntity;
|
||||||
|
import org.springframework.data.elasticsearch.ParentEntity.ChildEntity;
|
||||||
|
import org.springframework.data.elasticsearch.core.query.IndexQuery;
|
||||||
|
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
|
||||||
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Philipp Jardas
|
||||||
|
*/
|
||||||
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
|
@ContextConfiguration("classpath:elasticsearch-template-test.xml")
|
||||||
|
public class ElasticsearchTemplateParentChildTests {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ElasticsearchTemplate elasticsearchTemplate;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void before() {
|
||||||
|
clean();
|
||||||
|
elasticsearchTemplate.createIndex(ParentEntity.class);
|
||||||
|
elasticsearchTemplate.createIndex(ChildEntity.class);
|
||||||
|
elasticsearchTemplate.putMapping(ParentEntity.class);
|
||||||
|
elasticsearchTemplate.putMapping(ChildEntity.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@After
|
||||||
|
public void clean() {
|
||||||
|
elasticsearchTemplate.deleteIndex(ChildEntity.class);
|
||||||
|
elasticsearchTemplate.deleteIndex(ParentEntity.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldIndexParentChildEntity() {
|
||||||
|
// index two parents
|
||||||
|
ParentEntity parent1 = index("parent1", "First Parent");
|
||||||
|
ParentEntity parent2 = index("parent2", "Second Parent");
|
||||||
|
|
||||||
|
// index a child for each parent
|
||||||
|
String child1name = "First";
|
||||||
|
index("child1", parent1.getId(), child1name);
|
||||||
|
index("child2", parent2.getId(), "Second");
|
||||||
|
|
||||||
|
elasticsearchTemplate.refresh(ParentEntity.class, true);
|
||||||
|
elasticsearchTemplate.refresh(ChildEntity.class, true);
|
||||||
|
|
||||||
|
// find all parents that have the first child
|
||||||
|
QueryBuilder query = hasChildQuery(ParentEntity.CHILD_TYPE, QueryBuilders.fieldQuery("name", child1name));
|
||||||
|
List<ParentEntity> parents = elasticsearchTemplate.queryForList(new NativeSearchQuery(query), ParentEntity.class);
|
||||||
|
|
||||||
|
// we're expecting only the first parent as result
|
||||||
|
assertThat("parents", parents, contains(hasProperty("id", is(parent1.getId()))));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldSearchTopChildrenForGivenParent(){
|
||||||
|
// index two parents
|
||||||
|
ParentEntity parent1 = index("parent1", "First Parent");
|
||||||
|
ParentEntity parent2 = index("parent2", "Second Parent");
|
||||||
|
|
||||||
|
// index a child for each parent
|
||||||
|
String child1name = "First";
|
||||||
|
index("child1", parent1.getId(), child1name);
|
||||||
|
index("child2", parent2.getId(), "Second");
|
||||||
|
|
||||||
|
elasticsearchTemplate.refresh(ParentEntity.class, true);
|
||||||
|
elasticsearchTemplate.refresh(ChildEntity.class, true);
|
||||||
|
|
||||||
|
// find all parents that have the first child using topChildren Query
|
||||||
|
QueryBuilder query = topChildrenQuery(ParentEntity.CHILD_TYPE, QueryBuilders.fieldQuery("name", child1name));
|
||||||
|
List<ParentEntity> parents = elasticsearchTemplate.queryForList(new NativeSearchQuery(query), ParentEntity.class);
|
||||||
|
|
||||||
|
// we're expecting only the first parent as result
|
||||||
|
assertThat("parents", parents, contains(hasProperty("id", is(parent1.getId()))));
|
||||||
|
}
|
||||||
|
|
||||||
|
private ParentEntity index(String parentId, String name) {
|
||||||
|
ParentEntity parent = new ParentEntity(parentId, name);
|
||||||
|
IndexQuery index = new IndexQuery();
|
||||||
|
index.setId(parent.getId());
|
||||||
|
index.setObject(parent);
|
||||||
|
elasticsearchTemplate.index(index);
|
||||||
|
|
||||||
|
return parent;
|
||||||
|
}
|
||||||
|
|
||||||
|
private ChildEntity index(String childId, String parentId, String name) {
|
||||||
|
ChildEntity child = new ChildEntity(childId, parentId, name);
|
||||||
|
IndexQuery index = new IndexQuery();
|
||||||
|
index.setId(child.getId());
|
||||||
|
index.setObject(child);
|
||||||
|
index.setParentId(child.getParentId());
|
||||||
|
elasticsearchTemplate.index(index);
|
||||||
|
|
||||||
|
return child;
|
||||||
|
}
|
||||||
|
}
|
@ -8,6 +8,7 @@ import org.springframework.data.elasticsearch.SampleTransientEntity;
|
|||||||
import org.springframework.data.elasticsearch.SimpleRecursiveEntity;
|
import org.springframework.data.elasticsearch.SimpleRecursiveEntity;
|
||||||
import org.springframework.data.elasticsearch.StockPrice;
|
import org.springframework.data.elasticsearch.StockPrice;
|
||||||
import org.springframework.data.elasticsearch.StockPriceBuilder;
|
import org.springframework.data.elasticsearch.StockPriceBuilder;
|
||||||
|
import org.springframework.data.elasticsearch.MinimalEntity;
|
||||||
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
|
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
|
||||||
import org.springframework.data.elasticsearch.core.query.SearchQuery;
|
import org.springframework.data.elasticsearch.core.query.SearchQuery;
|
||||||
import org.springframework.test.context.ContextConfiguration;
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
@ -44,7 +45,7 @@ public class MappingBuilderTests {
|
|||||||
"type\":\"string\",\"index\":\"not_analyzed\",\"search_analyzer\":\"standard\"," +
|
"type\":\"string\",\"index\":\"not_analyzed\",\"search_analyzer\":\"standard\"," +
|
||||||
"\"index_analyzer\":\"standard\"}}}}";
|
"\"index_analyzer\":\"standard\"}}}}";
|
||||||
|
|
||||||
XContentBuilder xContentBuilder = MappingBuilder.buildMapping(SampleTransientEntity.class, "mapping", "id");
|
XContentBuilder xContentBuilder = MappingBuilder.buildMapping(SampleTransientEntity.class, "mapping", "id", null);
|
||||||
assertThat(xContentBuilder.string(), is(expected));
|
assertThat(xContentBuilder.string(), is(expected));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -54,7 +55,7 @@ public class MappingBuilderTests {
|
|||||||
final String expected = "{\"mapping\":{\"properties\":{\"price\":{\"store\":false,\"type\":\"double\"}}}}";
|
final String expected = "{\"mapping\":{\"properties\":{\"price\":{\"store\":false,\"type\":\"double\"}}}}";
|
||||||
|
|
||||||
//When
|
//When
|
||||||
XContentBuilder xContentBuilder = MappingBuilder.buildMapping(StockPrice.class, "mapping", "id");
|
XContentBuilder xContentBuilder = MappingBuilder.buildMapping(StockPrice.class, "mapping", "id", null);
|
||||||
|
|
||||||
//Then
|
//Then
|
||||||
assertThat(xContentBuilder.string(), is(expected));
|
assertThat(xContentBuilder.string(), is(expected));
|
||||||
@ -83,4 +84,10 @@ public class MappingBuilderTests {
|
|||||||
assertThat(entry.getPrice(), is(new BigDecimal(price)));
|
assertThat(entry.getPrice(), is(new BigDecimal(price)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldCreateMappingForSpecifiedParentType() throws IOException {
|
||||||
|
final String expected = "{\"mapping\":{\"_parent\":{\"type\":\"parentType\"},\"properties\":{}}}";
|
||||||
|
XContentBuilder xContentBuilder = MappingBuilder.buildMapping(MinimalEntity.class, "mapping", "id", "parentType");
|
||||||
|
assertThat(xContentBuilder.string(), is(expected));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -20,7 +20,7 @@ public class SimpleElasticsearchDateMappingTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testCorrectDateMappings() throws NoSuchFieldException, IntrospectionException, IOException {
|
public void testCorrectDateMappings() throws NoSuchFieldException, IntrospectionException, IOException {
|
||||||
XContentBuilder xContentBuilder = MappingBuilder.buildMapping(SampleDateMappingEntity.class, "mapping", "id");
|
XContentBuilder xContentBuilder = MappingBuilder.buildMapping(SampleDateMappingEntity.class, "mapping", "id", null);
|
||||||
Assert.assertEquals(EXPECTED_MAPPING, xContentBuilder.string());
|
Assert.assertEquals(EXPECTED_MAPPING, xContentBuilder.string());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user