Spring Data ElasticSearch parnt/child search

http://stackoverflow.com/questions/23730641/parent-child-relationships-in-spring-data-elastic-search

 

父实体类及子实体类的搜索方法如下:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. @Document(indexName = "parent-child", type = "parent-entity")  
  2.    public class ParentEntity {  
  3.   
  4.   
  5.     @Id  
  6.     private String id;  
  7.     @Field(type = FieldType.String, index = FieldIndex.analyzed, store = true)  
  8.     private String name;  
  9.         // setter/getter  
  10.   
  11.    public ParentEntity() {  
  12.    }  
  13.   
  14.    public ParentEntity(String id, String name) {  
  15.      this.id = id;  
  16.      this.name = name;  
  17.    }  
  18.    }  
  19.   
  20.   
  21.   @Document(indexName = "parent-child", type = "child-entity")  
  22.   public class ChildEntity {  
  23.   
  24.     @Id  
  25.     private String id;  
  26.     @Field(type = FieldType.String, store = true)  
  27.     @Parent(type = "parent-entity")  
  28.     private String parentId;  
  29.     @Field(type = FieldType.String, index = FieldIndex.analyzed, store = true)  
  30.     private String name;  
  31.   
  32.     public ChildEntity() {  
  33.     }  
  34.   
  35.     public ChildEntity(String id, String parentId, String name) {  
  36.      this.id = id;  
  37.      this.parentId = parentId;  
  38.      this.name = name;  
  39.     }  
  40.      }  

// 为父实体类做索引(你还可以使用许多其它的方法做索引 例如: repositories)

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. ParentEntity parent1 = new ParentEntity("parent1", "First Parent");  
  2.     IndexQuery parentIndex1 = new IndexQuery();  
  3.     parentIndex1.setId(parent1.getId());  
  4.     parentIndex1.setObject(parent1);  
  5.     elasticsearchTemplate.index(parentIndex1);  
  6.   
  7.     ParentEntity parent2 = new ParentEntity("parent2", "Second Parent");  
  8.     IndexQuery parentIndex2 = new IndexQuery();  
  9.     parentIndex2.setId(parent2.getId());  
  10.     parentIndex2.setObject(parent2);  
  11.     elasticsearchTemplate.index(parentIndex2);  

// 为子实体类做索引

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. ChildEntity child1 = new ChildEntity("child1", parent1.getId(), "First");  
  2.     IndexQuery childIndex1 = new IndexQuery();  
  3.     childIndex1.setId(child1.getId());  
  4.     childIndex1.setObject(child1);  
  5.     childIndex1.setParentId(child1.getParentId());  
  6.     elasticsearchTemplate.index(childIndex1);  
  7.   
  8.     ChildEntity child2 = new ChildEntity("child2", parent1.getId(), "Second");  
  9.     IndexQuery childIndex2 = new IndexQuery();  
  10.     childIndex2.setId(child2.getId());  
  11.     childIndex2.setObject(child2);  
  12.     childIndex2.setParentId(child2.getParentId());  
  13.     elasticsearchTemplate.index(childIndex2);  

//搜索

对于主从结构的实体类,有三种可选的搜索方法:

1.has children 点击打开链接

2.has parent  点击打开链接

3.top children  点击打开链接

 

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. QueryBuilder query = topChildrenQuery("child-entity", QueryBuilders.termQuery("name", child1name.toLowerCase()));  
  2.     SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(query).build();  
  3.   
  4.     List<ParentEntity> parents = elasticsearchTemplate.queryForList(searchQuery, ParentEntity.class);  

 

http://blog.csdn.net/wilsonke/article/details/44113785

猜你喜欢

转载自m635674608.iteye.com/blog/2255932
今日推荐