Elasticsearch QueryBuilders获取所有拥有子节点的父节点

iklwldmw  于 2023-10-17  发布在  ElasticSearch
关注(0)|答案(1)|浏览(180)

我在Elastic SearchMap中有既是父项又是子项的文档。而且由于子项经常更新,因此选择了这种连接而不是嵌套文档。我现在想找到每一个有孩子和没有父母的父母。

HasChildQueryBuilder hQuery = QueryBuilders
       .hasChildQuery("instance", QueryBuilders
             .hasChildQuery("instance_permission", terms));

我在互联网上发现了这一点,但它似乎已经改变,不再有效。

Query query = new NativeSearchQueryBuilder()
       .withQuery(QueryBuilders.boolQuery().
        must.(QueryBuilders.matchQuery("relation_type", "parent))
        must. ****  // parents that has children query  ****
       .build();

有人能帮我完成这个查询吗?

gk7wooem

gk7wooem1#

// JoinQueryBuilders.hasChildQuery

Query query = new NativeSearchQueryBuilder()
       .withQuery(QueryBuilders.boolQuery()
          .must(QueryBuilders.matchQuery("relation_type", "parent"))
          .must(JoinQueryBuilders.hasChildQuery("child", QueryBuilders.matchAllQuery(), ScoreMode.None)))
       .withPageable(pageRequest)
       .build();
SearchHits<Asset> searchHits = elasticsearchRestTemplate.search(query, Asset.class);

相关问题