过滤neo4j结果,返回节点ID的不同组合

我有一个具有Airport个节点和Flight关系的图,我想从特定的节点中找到三角形,这些三角形的边缘之间的距离都在10%以内。

MATCH path = (first:Airport{ID: 12953})-[f1:Flight]->
             (second:Airport)-[f2:Flight]->
             (third:Airport)-[f3:Flight]->
             (last:Airport{ID: 12953})
WHERE second.ID <>first.ID AND 
      third.ID <>first.ID AND 
      f1.Distance<=(1.1*f2.Distance) AND 
      f1.Distance<=(1.1*f3.Distance) AND 
      f2.Distance<=(1.1*f1.Distance) AND 
      f2.Distance<=(1.1*f3.Distance) AND 
      f3.Distance<=(1.1*f1.Distance) AND 
      f3.Distance<=(1.1*f2.Distance)
WITH (first.ID,second.ID,third.ID) as triplet
return count(DISTINCT triplet)

我只想返回一次节点集(无论它们之间有多少不同的飞行),但是with行不起作用。基本上,我要创建的是一种新型的变量“对象”,它具有三个ID作为其属性,并在其上运行。在neo4j中有可能吗?如果没有,有什么解决方法吗?

ttdrhfiaid 回答:过滤neo4j结果,返回节点ID的不同组合

您可以返回带有键或数组的对象。例如:

UNWIND range(1,10000) AS i
WITH 
  { 
    id1: toInteger(rand()*3),id2: toInteger(rand()*3),id3: toInteger(rand()*3) 
  } AS triplet
RETURN DISTINCT triplet

UNWIND range(1,10000) AS i
WITH 
  [ toInteger(rand()*3),toInteger(rand()*3),toInteger(rand()*3) ] AS triplet
RETURN DISTINCT triplet

更新。您可以通过在查询中重用变量,指定路径的长度并使用列表函数来简化查询:

MATCH ps = (A:Airport {ID: 12953})-[:Flight*3]->(A)
WITH ps 
WHERE reduce(
  total = 0,rel1 IN relationships(ps) | 
  total + reduce(
    acc = 0,rel2 IN relationships(ps) | 
    acc + CASE WHEN rel1.Distance <= 1.1 * rel2.Distance THEN 0 ELSE 1 END
  )) = 0
RETURN count(DISTINCT [n IN nodes(ps) | n.ID][0..3])
,

您可以使用APOC函数apoc.coll.sort对3个ID的每个列表进行排序,以便DISTINCT选项将正确对待与ID相同的列表一样。

这是使用APOC函数的简化查询:

MATCH path = (first:Airport{ID: 12953})-[f1:Flight]->
             (second:Airport)-[f2:Flight]->
             (third:Airport)-[f3:Flight]->
             (first)
WHERE second <> first <> third AND 
      f2.Distance<=(1.1*f1.Distance)>=f3.Distance AND
      f1.Distance<=(1.1*f2.Distance)>=f3.Distance AND 
      f1.Distance<=(1.1*f3.Distance)>=f2.Distance
RETURN COUNT(DISTINCT apoc.coll.sort([first.ID,second.ID,third.ID]]))

注意:可能不需要进行second <> first测试,因为不应有任何从机场飞回自身的航班(如果“航班”与“航段”相同)。

本文链接:https://www.f2er.com/3169929.html

大家都在问