我怎么能暗示对可变长度路径的限制?
我有一些start
节点查询的所有可能路径:
CREATE INDEX ON :NODE(id)
MATCH all_paths_from_Start = (start:Person)-[:FRIENDSHIP*1..20]->(person:Person)
WHERE start.id = 128 AND start.country <> "Uganda"
RETURN paths;
不,我想过滤掉至少有两个人具有相同country
的所有路径。 我怎么能这样做?
答案 0 :(得分:3)
1)获取一系列国家/地区的路径,可能有重复项:REDUCE
2)删除重复项并比较数组的大小:UNWIND
+ COLLECT(DISTINCT...)
MATCH path = (start:Person)-[:FRIENDSHIP*1..20]->(person:Person)
WHERE start.id = 128 AND start.country <> "Uganda"
WITH path,
REDUCE(acc=[], n IN NODES(path) | acc + n.country) AS countries
UNWIND countries AS country
WITH path,
countries, COLLECT(DISTINCT country) AS distinctCountries
WHERE SIZE(countries) = SIZE(distinctCountries)
RETURN path
P.S。 REDUCE
可以替换为EXTRACT
(感谢Gabor Szarnyas):
MATCH path = (start:Person)-[:FRIENDSHIP*1..20]->(person:Person)
WHERE start.id = 128 AND start.country <> "Uganda"
WITH path,
EXTRACT(n IN NODES(path) | n.country) AS countries
UNWIND countries AS country
WITH path,
countries, COLLECT(DISTINCT country) AS distinctCountries
WHERE SIZE(countries) = SIZE(distinctCountries)
RETURN path
P.P.S。再次感谢Gabor Szarnyas提出了另一个简化查询的想法:
MATCH path = (start:Person)-[:FRIENDSHIP*1..20]->(person:Person)
WHERE start.id = 128 AND start.country <> "Uganda"
WITH path
UNWIND NODES(path) AS person
WITH path,
COLLECT(DISTINCT person.country) as distinctCountries
WHERE LENGTH(path) + 1 = SIZE(distinctCountries)
RETURN path
答案 1 :(得分:2)
我能想到的一个解决方案是获取路径的nodes
,以及路径中每个人extract
来自同一国家/地区的人数(我们通过filter
确定同一个国家/地区。如果来自同一个国家/地区的人数为零,则路径中包含来自独特国家/地区的人员,即对于所有人员,只有一个人(他/她自己)来自该国家/地区。国家。
MATCH p = (start:Person {id: 128})-[:FRIENDSHIP*1..20]->(person:Person)
WHERE start.country <> "Uganda"
WITH p, nodes(p) AS persons
WITH p, extract(p1 IN persons | size(filter(p2 IN persons WHERE p1.country = p2.country))) AS personsFromSameCountry
WHERE length(filter(p3 IN personsFromSameCountry WHERE p3 > 1)) = 0
RETURN p
查询在语法上是正确的,但我没有对任何数据进行测试。
请注意,我已将id = 128
条件移至模式,并将all_paths_from_Start
变量缩短为p
。