我有这张图:
A-[:X]->B-> a whole tree of badness
A-[:Y]->C-> a whole tree of goodness
我想知道如何指定一个以A开头的路径,它排除了:X关系。
在这种情况下,“Y”可以是许多不同边缘类型中的任何一种。我不想明确指定它们。
如何编写包含A - [*] - B的路径语句,其中*不是:X但可以是其他任何内容?
答案 0 :(得分:4)
您可以通过匹配A
到B
之间的所有关系来排除关系类型,然后使用WHERE NOT
过滤掉特定类型
MATCH p = (a:Label1)-[]-(b:Label2)
WHERE NOT (a)-[:X]-(b)
RETURN p
如果A
和B
之间的路径长度可变,则无法在WHERE NOT
中添加确切的模式。相反,您可以在路径上使用NONE
谓词:
MATCH p = (a:Label1)-[*]-(b:Label2)
// this WHERE makes sure that none of the relationships in the
// returned path fulfill the criterion type(relationship) = 'X'
WHERE NONE (r in relationships(p) WHERE type(r) = 'X')
RETURN p
答案 1 :(得分:3)
This Cypher query is simpler than the variable-length path query from @MartinPreusse, as it avoids using the RELATIONSHIPS
function. Profiling shows that its execution plan is also a bit simpler, so it might be faster.
MATCH p=(a:Label1)-[rels*]-(b:Label2)
WHERE NONE (r IN rels WHERE type(r)= 'X')
RETURN p