如何在具有共同属性的两个节点之间创建关系?

时间:2016-05-08 18:32:09

标签: neo4j cypher

我想我正确地问这个问题。 我的节点中的数据如下所示:

Car      Speed
---      -----
Farrari  80
Corvette 92
Farrari  135
Corvette 129
Porche   78
Porche   150

我想在两辆相同的汽车之间创建一个名为HIGH_SPEED的关系,速度最快,车速最低。所以基本上关系应该是这样的:

                  ___________                        ___________
                 /           \                      /           \
                /             \    High Speed      /             \
               |  Car: Porche  |_________________\|  Car: Porche  |  
               |  Speed: 78    |                 /|  Speed: 150   |
                \             /                    \             /
                 \___________/                      \___________/

这是我到目前为止所得到的。这会产生一种空洞的关系。

MATCH (c_max:CARS), (c_min:CARS)
with c_max.name as max_name, max(c_max.speed) as max_speed
   , c_min.name as min_name, min(c_min.speed) as min_speed
WHERE c_max.name = c_min.name
FOREACH (x IN max_name |
  FOREACH (y IN min_name |
    CREATE (x)-[:HIGH_SPEED]->(y)))

1 个答案:

答案 0 :(得分:3)

如果您为每种类型的汽车找到minmax,然后直接匹配节点以获得最高和最低速度并连接它们,那该怎么样。

match (c:CARS)
with c.name as type, min(c.score) as min_speed, max(c.score) as max_speed
match (c1:CARS {name: type, score: min_speed}), (c2:CARS {name: type, score: max_speed})
create (c1)-[:HIGH_SPEED]->(c2)
return c1, c2