Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Neo4J Cypher:根据条件筛选出可变长度路径_Neo4j_Cypher_Variable Length - Fatal编程技术网

Neo4J Cypher:根据条件筛选出可变长度路径

Neo4J Cypher:根据条件筛选出可变长度路径,neo4j,cypher,variable-length,Neo4j,Cypher,Variable Length,我如何暗示对可变长度路径的限制 我有一些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; 在节点(id)上创建索引 匹配所有路径\u从\u开始=(开始

我如何暗示对可变长度路径的限制

我有一些
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;
在节点(id)上创建索引
匹配所有路径\u从\u开始=(开始:人)-[:友谊*1..20]->(人:人)
其中start.id=128,start.country为“乌干达”
返回路径;

否我希望筛选出至少有两个人具有相同
国家/地区的所有路径。我怎么能做到这一点呢?

我能想到的一个解决方案是获取路径的值,对于路径上的每个人,来自同一个国家的人数的值(我们通过对同一国家进行ing来确定。如果路径中没有来自同一国家的人员,则路径中有来自唯一国家的人员,即对于所有人员,只有来自该国家的一个人(此人本人)

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
MATCH p=(开始:Person{id:128})-[:友谊*1..20]->(Person:Person)
从哪里开始。国家“乌干达”
使用p,节点(p)作为人
使用p,从同一个国家提取(p1以个人为单位|大小(过滤(p2以个人为单位,其中p1.country=p2.country)),作为个人
其中长度(过滤器(p3在p3>1的同一国家/地区的人员中为p3))=0
返回p
该查询在语法上是正确的,但我没有对任何数据进行测试

请注意,我将
id=128
条件移动到模式中,并将
所有路径从\u Start
变量缩短为
p

1)获取一组可能重复的国家/地区路径:
REDUCE

2) 删除重复项并比较数组的大小:
展开
+
收集(不同的…)

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..20]->(个人:个人)
其中start.id=128,start.country为“乌干达”
带路径
以个人身份展开节点(路径)
有了路径,
收集(不同的人。国家)作为不同的国家
其中长度(路径)+1=大小(距离国家)
返回路径

谢谢您的回答!从性能角度来看,它是否是最佳选择?我将有一个非常大的数据集,有多达百万个节点。有没有办法让查询更快?@VolodymyrBakhmatiuk查询最重的部分是第一个
匹配
匹配路径=(开始:个人)-[:友谊*1..20]->(个人:个人),其中start.id=128和start.country“乌干达”
。目前还不太了解如何改进它……至少我可以索引
id
coutny
。请让我知道你是否会有任何ideas@stdob--我想我们可能不需要
提取
将节点(路径)作为节点与路径展开,将节点(node.country)作为国家/地区收集,将节点(DISTINCT node.country)作为DISTINCT国家/地区收集
我不确定它是否会对性能产生可测量的影响,但它可能更容易阅读。@Gaborszarnya是的,你是对的。根据您的想法添加了一个选项。
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
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