Neo4j Cypher使用复杂的嵌套QueryResult查询SDN 4

时间:2017-02-08 18:54:50

标签: neo4j cypher spring-data-neo4j spring-data-neo4j-4

我有一个以下Cypher查询/ Spring Data Neo4j 4,它返回一个Criterion列表及其平均权重,这些列表是针对特定Criterion添加到这些childDecisionId上的。

MATCH (parentD:Decision)-[:CONTAINS]->(childD:Decision) 
WHERE id(childD) = {childDecisionId} AND id(parentD) = {parentDecisionId} 
WITH childD, parentD MATCH (parentD)<-[:DEFINED_BY]-(c:Criterion)<-[:VOTED_ON]-(vg:VoteGroup)-[:VOTED_FOR]->(childD) 
RETURN c AS criterion, vg.avgVotesWeight AS weight

@Query("MATCH (parentD:Decision)-[:CONTAINS]->(childD:Decision) WHERE id(childD) = {childDecisionId} AND id(parentD) = {parentDecisionId} WITH childD, parentD MATCH (parentD)<-[:DEFINED_BY]-(c:Criterion)<-[:VOTED_ON]-(vg:VoteGroup)-[:VOTED_FOR]->(childD) RETURN c AS criterion, vg.avgVotesWeight AS weight")
List<WeightedCriterion> getCriteriaWithAvgVotesWeightForChildDecision(@Param("childDecisionId") Long childDecisionId, @Param("parentDecisionId") Long parentDecisionId);

这是WeightedCriterion QueryResult

@QueryResult
public class WeightedCriterion {

    private Criterion criterion;

    private Double weight;

}

我需要更改此查询,以便返回Criterion的列表,其平均权重不仅适用于单个子决策,还适用于父parentDecisionId的所有子决策..换句话说此查询应返回如下内容:

List<DecisionWeightedCriterion>

其中DecisionWeightedCriterion例如:

@QueryResult
public class DecisionWeightedCriterion {

    private Decision decision;

    private List<WeightedCriterion> weightedCriteria;

}

是否有可能,如果可以,请帮助解决这个Cypher查询?

1 个答案:

答案 0 :(得分:0)

我已按以下方式实施此查询:

MATCH (parentD:Decision)-[:CONTAINS]->(childD:Decision) 
WHERE id(parentD) = {parentDecisionId} 
WITH childD, parentD MATCH (parentD)<-[:DEFINED_BY]-(c:Criterion)<-[:VOTED_ON]-(vg:VoteGroup)-[:VOTED_FOR]->(childD) WITH childD, {criterion: c,  weight: vg.avgVotesWeight} AS weightedCriterion 
RETURN childD AS decision, collect(weightedCriterion) as weightedCriteria

如果我错了,请纠正我。