spring-data-elastic-search中的父/子关系

时间:2014-05-19 06:01:08

标签: elasticsearch spring-data spring-data-elasticsearch

我正在使用Spring-Data-Elastic-Search进行搜索/缓存。 我需要执行一个使用child(TermCache)和parent(ConceptCache)属性的查询 并返回子对象的实例(这意味着我不能使用嵌套对象)。

我有以下结构:

@Document(indexName = "termweb" , type = "term")
public class TermCache {

  @Id
  private String id;
  private String name;
  private LanguageDTO language;
  private String status;
  private String definition;

  @Field(type = FieldType.String, store = true)
  @Parent(type = "concept")
  private Long conceptId;

  private String displayId;
  private Map<Long, String> fields = new HashMap<>();
  //todo think about storing it as a collection of nested objects

}


@Document( indexName = "termweb" , type = "concept")
public class ConceptCache implements ConceptDTO{

 @Id
 private String id;

 private String displayId;
 private Long dictionaryId;
 private String dictionaryName;

 private Map<Long, String> fields = new HashMap<>();
}

我需要提示如何处理这类任务;我应该使用两个单独的查询,还是应该以某种方式获取父项的属性或者其他什么?

2 个答案:

答案 0 :(得分:8)

同意,我们缺乏文档,我们将在即将发布的版本中进行改进。

如果您对弹簧数据有任何疑问,弹性搜索stackoverflow可能不是获得答案的最佳方式(因为我们不会收到新线程的通知),我们会为问题/查询提供单独的Google小组https://groups.google.com/forum/#!forum/spring-data-elasticsearch-devs

我不知道你想要用上面的实体实现什么,我可以给你一个样本父子实体的例子,如下所示

    @Document(indexName = "parent-child", type = "parent-entity")
    public class ParentEntity {


     @Id
     private String id;
     @Field(type = FieldType.String, index = FieldIndex.analyzed, store = true)
     private String name;
         // setter/getter

    public ParentEntity() {
    }

    public ParentEntity(String id, String name) {
      this.id = id;
      this.name = name;
    }
    }


   @Document(indexName = "parent-child", type = "child-entity")
   public class ChildEntity {

     @Id
     private String id;
     @Field(type = FieldType.String, store = true)
     @Parent(type = "parent-entity")
     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;
     }
      }

//索引父级(您可以使用许多其他方式来索引,包括使用存储库)

    ParentEntity parent1 = new ParentEntity("parent1", "First Parent");
    IndexQuery parentIndex1 = new IndexQuery();
    parentIndex1.setId(parent1.getId());
    parentIndex1.setObject(parent1);
    elasticsearchTemplate.index(parentIndex1);

    ParentEntity parent2 = new ParentEntity("parent2", "Second Parent");
    IndexQuery parentIndex2 = new IndexQuery();
    parentIndex2.setId(parent2.getId());
    parentIndex2.setObject(parent2);
    elasticsearchTemplate.index(parentIndex2);

//索引子

    ChildEntity child1 = new ChildEntity("child1", parent1.getId(), "First");
    IndexQuery childIndex1 = new IndexQuery();
    childIndex1.setId(child1.getId());
    childIndex1.setObject(child1);
    childIndex1.setParentId(child1.getParentId());
    elasticsearchTemplate.index(childIndex1);

    ChildEntity child2 = new ChildEntity("child2", parent1.getId(), "Second");
    IndexQuery childIndex2 = new IndexQuery();
    childIndex2.setId(child2.getId());
    childIndex2.setObject(child2);
    childIndex2.setParentId(child2.getParentId());
    elasticsearchTemplate.index(childIndex2);

//搜索

在父/子实体上搜索时有几个可用选项,其中包括has childrenhas parenttop children个查询。

   QueryBuilder query = topChildrenQuery("child-entity", QueryBuilders.termQuery("name", child1name.toLowerCase()));
    SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(query).build();

    List<ParentEntity> parents = elasticsearchTemplate.queryForList(searchQuery, ParentEntity.class);

希望这个小例子能让您基本了解如何使用父子。请查看ParentChildTests了解更多内容。

如果您还有其他问题,请随时与我们联系。

答案 1 :(得分:0)

您应该只使用过滤器的hasparent查询:http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-has-parent-filter.html#query-dsl-has-parent-filter

这将在父字段上发出请求,并生成匹配父文档的子文档。然后,您可以对返回的子文档使用过滤器:)