在我的Neo4j / SDN4项目中,我有一个以下节点实体:
@NodeEntity
public class Nomination extends Commentable {
private final static String CONTAINS = "CONTAINS";
private final static String DEFINED_BY = "DEFINED_BY";
private String name;
@Relationship(type = CONTAINS, direction = Relationship.OUTGOING)
private Set<Criterion> criteria = new HashSet<>();
...
}
我需要实现一个Cypher查询,该查询将尝试通过精确的相关标准集合(按标准ID)查找Nomination
。
现在我有以下查询:
MATCH (n:Nomination)-[:CONTAINS]->(c:Criterion) WHERE id(n) = {nominationId} AND id(c) IN {criterionIds} RETURN n
但这还不够,因为Nomination
可以包含{criterionIds}
中提供的较少条件,但我需要检查完全匹配(条件顺序无关紧要)
如何重新实现此查询才能执行此操作?
答案 0 :(得分:2)
使用COLLECT然后使用ALL功能。
https://neo4j.com/docs/developer-manual/current/cypher/functions/predicates/#functions-all
MATCH (n:Nomination)-[:CONTAINS]->(c:Criterion)
WHERE id(n) = {nominationId}
WITH n,COLLECT(id(c)) AS foundCritIds
WHERE ALL (id IN {criterionIds} WHERE id in foundCritIds)
RETURN n
答案 1 :(得分:0)
这是另一种方法,您可能希望每个人都能查看最适合您的方法:
MATCH (c:Criterion)
WHERE id(c) in {criterionIds}
WITH COLLECT(c) as criterion
WITH criterion, head(criterion) as firstC
MATCH (firstC)<-[:CONTAINS]-(n:Nomination)
WHERE SIZE((n)-[:CONTAINS]->(:Criterion)) = SIZE(criterion)
AND ALL(crit in criterion[1..] WHERE (n)-[:CONTAINS]->(crit))
RETURN n