spring数据弹性搜索中的父/子关系

spring数据弹性搜索中的父/子关系,
Warning: implode(): Invalid arguments passed in /data/phpspider/zhask/webroot/tpl/detail.html on line 45
,,我使用SpringDataElasticSearch进行搜索/缓存。 我需要执行一个使用child(TermCache)和parent(ConceptCache)属性的查询 并返回子对象的实例(这意味着我不能使用嵌套对象) 我的结构如下: @Document(indexName = "termweb" , type = "term") public class TermCache { @Id private String id; private String name; pri

我使用SpringDataElasticSearch进行搜索/缓存。 我需要执行一个使用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<>();
}
@文档(indexName=“termweb”,type=“term”)
公共类TermCache{
@身份证
私有字符串id;
私有字符串名称;
私人语言对语言;
私有字符串状态;
私有字符串定义;
@字段(type=FieldType.String,store=true)
@父项(type=“concept”)
私有长概念ID;
私有字符串displayId;
私有映射字段=新HashMap();
//要考虑将其存储为嵌套对象的集合吗
}
@文件(indexName=“termweb”,type=“concept”)
公共类ConceptCache实现了ConceptDTO{
@身份证
私有字符串id;
私有字符串displayId;
私人长字典;
私有字符串字典名;
私有映射字段=新HashMap();
}

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

您只需使用过滤器的hasparent查询:


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

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

如果您对spring数据有任何疑问,elasticsearch stackoverflow可能不是获得答案的最佳方式(因为我们不会收到关于新线程的通知),我们有单独的google问题/查询组

在不知道使用上述实体究竟要实现什么的情况下,我可以为您提供一个示例父子实体,如下所示

@文档(indexName=“parent-child”,type=“parent-entity”)
公共类父实体{
@身份证
私有字符串id;
@字段(type=FieldType.String,index=FieldIndex.analysed,store=true)
私有字符串名称;
//二传手
公共父实体(){
}
公共父实体(字符串id、字符串名称){
this.id=id;
this.name=名称;
}
}
@文档(indexName=“父-子”,type=“子实体”)
公共类子实体{
@身份证
私有字符串id;
@字段(type=FieldType.String,store=true)
@母公司(类型=“母公司实体”)
私有字符串parentId;
@字段(type=FieldType.String,index=FieldIndex.analysed,store=true)
私有字符串名称;
公共儿童实体(){
}
公共子实体(字符串id、字符串parentId、字符串名称){
this.id=id;
this.parentId=parentId;
this.name=名称;
}
}
//索引父级(您可以使用许多其他方法来索引,包括使用存储库)

ParentEntity parent1=新的ParentEntity(“parent1”,“第一个家长”);
IndexQuery parentIndex1=新IndexQuery();
parentIndex1.setId(parent1.getId());
parentIndex1.setObject(parent1);
elasticsearchTemplate.index(parentIndex1);
ParentEntity parent2=新的ParentEntity(“parent2”、“第二父”);
IndexQuery parentIndex2=新IndexQuery();
parentIndex2.setId(parent2.getId());
parentIndex2.setObject(parent2);
elasticsearchTemplate.index(parentIndex2);
//索引子项

ChildEntity child1=新的ChildEntity(“child1”,parent1.getId(),“First”);
IndexQuery childIndex1=新的IndexQuery();
childIndex1.setId(child1.getId());
childIndex1.setObject(child1);
childIndex1.setParentId(child1.getParentId());
elasticsearchTemplate.index(childIndex1);
ChildEntity child2=新的ChildEntity(“child2”,parent1.getId(),“Second”);
IndexQuery childIndex2=新的IndexQuery();
childIndex2.setId(child2.getId());
childIndex2.setObject(child2);
childIndex2.setParentId(child2.getParentId());
elasticsearchTemplate.index(childIndex2);
//搜寻

在父/子实体上搜索时,有几个可用选项,包括、和查询

QueryBuilder query=topChildrenQuery(“子实体”,QueryBuilders.termQuery(“name”,child1name.toLowerCase()); SearchQuery SearchQuery=new NativeSearchQueryBuilder().withQuery(query).build(); List parents=elasticsearchTemplate.queryForList(searchQuery,ParentEntity.class); 希望这个小例子能让你基本了解如何使用父子关系。更多信息,请查看


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

我希望能从spring data wrapper library for elastic search中得到答案。您提供的答案很好,但它需要使用较低级别的工具(在我的例子中是ElasticSearchJavaAPI)。我想,目前,SpringDataElasticSearch项目还不够成熟——文档中存在漏洞,社区支持不足。看看上面的答案,如果您还有问题,请与我们联系。乐于帮助:-)上面的一个只返回过滤查询的父数据。如何使用父数据检索子数据?@Mohsin Husen我尝试使用\@parent注释,但它似乎没有添加任何符合此处规范的内容。有没有关于如何使用springdata elasticsearch实现家长加入的建议?