Search code examples
neo4jcypherspring-data-neo4jspring-data-neo4j-4

Neo4j Cypher query SDN 4 with a complex nested QueryResult


I have a following Cypher query/Spring Data Neo4j 4 that returns a list of Criterion with their average weight which were added on these Criterion for a specific 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);

This is WeightedCriterion QueryResult:

@QueryResult
public class WeightedCriterion {

    private Criterion criterion;

    private Double weight;

}

I need to change this query in order to return a list of Criterion with their average weight not only for a single child decision but for all child decisions of a parent parentDecisionId.. in other word this query should return something like this:

List<DecisionWeightedCriterion>

where DecisionWeightedCriterion looks like for example:

@QueryResult
public class DecisionWeightedCriterion {

    private Decision decision;

    private List<WeightedCriterion> weightedCriteria;

}

Is it possible and if so, could you please help with this Cypher query ?


Solution

  • I have implemented this query in a following way:

    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
    

    Please correct me if I'm wrong.