spring-data-elastic-search 中的父子关系 [英] Parent/Child relationships in spring-data-elastic-search

查看:28
本文介绍了spring-data-elastic-search 中的父子关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using Spring-Data-Elastic-Search for searching/caching purposes. I need to execute a query which uses child(TermCache) and parent(ConceptCache) properties and return instances of child objects(this means i can't use nested objects).

我有以下结构:

@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<>();
}

我需要有关如何处理此类任务的提示;我应该使用两个单独的查询,还是应该以某种方式获取父级的属性或其他内容?

I need a hint on how to handle this type of tasks; should i use two separate queries or should i somehow fetch properties of a parent or maybe something else?

推荐答案

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

Agreed, We are lacking on documentation which we will be improving with upcoming release.

如果您对 Spring Data elasticsearch stackoverflow 有任何疑问,这可能不是获得答案的最佳方式(因为我们不会收到新线程的通知),我们有单独的 google 组用于问题/查询 https://groups.google.com/forum/#!forum/spring-data-elasticsearch-devs

If you have any question about spring data elasticsearch stackoverflow probably is not best way to get answer(as we wont be notified for new thread), we have separate google group for question/queries https://groups.google.com/forum/#!forum/spring-data-elasticsearch-devs

在不知道您要使用上述实体实现什么目标的情况下,我可以给您一个示例父子实体示例,如下所示

Without having any idea about what exactly you are trying to achieve with above entities, i can give you an example of sample parent child entities as below

@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;
   }
}

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

// indexing parent (you can use many other ways to index that includes using repositories)

   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);

//搜索

在搜索父/子实体时有几个可用选项,包括 有孩子有父级顶级儿童 查询.

there are several available option while searching on Parent/Child entities, that includes has children, has parent and top children queries.

   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 了解更多.

Hope this small example will give you basic understanding how to use parent child. have a look at ParentChildTests for more.

如果您还有更多问题,请随时与我们联系.

If you still have more question please feel free to contact us.

这篇关于spring-data-elastic-search 中的父子关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆