将方法代码移动到 Neo4j Cypher 查询
Move method code to Neo4j Cypher query
我有一个 Spring Neo4j 存储库方法 getAvgVotesWeightForCriterion
@Query("MATCH (d:Decision)<-[:VOTED_FOR]-(v:Vote)-[:VOTED_ON]->(c:Criterion) WHERE id(d) = {0} AND id(c) = {1} RETURN avg(v.weight)")
double getAvgVotesWeightForCriterion(Decision decision, Criterion criterion);
而且我还有另一种方法 calculateWeight
完全用 Java 编写。此方法在循环内部使用 getAvgVotesWeightForCriterion
public double calculateWeight(Decision decision, List<Criterion> criteria) {
double weight = 0;
for (Criterion criterion : criteria) {
weight += getAvgVotesWeightForCriterion(decision, criterion);
}
return weight;
}
是否可以将 calculateWeight
方法内的逻辑完全移动到 Cypher 查询?为了得到这样的东西:
@Query ....
double calculateWeight(Decision decision, List<Criterion> criteria)
这应该做你想做的事。
您按标准对 avg - 聚合进行分组,然后仅对权重求和。
MATCH (d:Decision)<-[:VOTED_FOR]-(v:Vote)-[:VOTED_ON]->(c:Criterion)
WHERE id(d) = {0} AND id(c) IN {1}
WITH c, avg(v.weight) as weight
RETURN sum(weight)
我有一个 Spring Neo4j 存储库方法 getAvgVotesWeightForCriterion
@Query("MATCH (d:Decision)<-[:VOTED_FOR]-(v:Vote)-[:VOTED_ON]->(c:Criterion) WHERE id(d) = {0} AND id(c) = {1} RETURN avg(v.weight)")
double getAvgVotesWeightForCriterion(Decision decision, Criterion criterion);
而且我还有另一种方法 calculateWeight
完全用 Java 编写。此方法在循环内部使用 getAvgVotesWeightForCriterion
public double calculateWeight(Decision decision, List<Criterion> criteria) {
double weight = 0;
for (Criterion criterion : criteria) {
weight += getAvgVotesWeightForCriterion(decision, criterion);
}
return weight;
}
是否可以将 calculateWeight
方法内的逻辑完全移动到 Cypher 查询?为了得到这样的东西:
@Query ....
double calculateWeight(Decision decision, List<Criterion> criteria)
这应该做你想做的事。
您按标准对 avg - 聚合进行分组,然后仅对权重求和。
MATCH (d:Decision)<-[:VOTED_FOR]-(v:Vote)-[:VOTED_ON]->(c:Criterion)
WHERE id(d) = {0} AND id(c) IN {1}
WITH c, avg(v.weight) as weight
RETURN sum(weight)