根据此处的模型,我正在尝试根据此模型中房间的距离找到最近的会议室。我想得到这样的结果,
+-------+----------+--+
| Room | Distance | |
+-------+----------+--+
| room1 | 3 | |
| room2 | 3 | |
| room3 | 4 | |
| room4 | 4 | |
+-------+----------+--+
我的模特:
我试过这个问题:
MATCH (p:Person {name:"test"})-[r*2..]->(f:Floor)<-[:ROOM_LOCATED_IN_FLOOR]-(r:Room)
RETURN p, f, r
它只是给出了这个人所在的会议室。但我想穿越不同楼层的其他房间。
答案 0 :(得分:2)
这里有一个测试样本数据:
CREATE (p:Person)
CREATE (d:Desk)
CREATE (f1:Floor)
CREATE (f2:Floor)
CREATE (r1:Room {name : 'room1'})
CREATE (r2:Room {name : 'room2'})
CREATE (r3:Room {name : 'room3'})
CREATE (r4:Room {name : 'room4'})
CREATE (p)-[:SEATED_AT]->(d)-[:LOCATED_IN]->(f1)-[:HAS_NEXT]->(f2)
CREATE (f1)<-[:PART_OF]-(r1)
CREATE (f1)<-[:PART_OF]-(r2)
CREATE (f2)<-[:PART_OF]-(r3)
CREATE (f2)<-[:PART_OF]-(r4)
然后,您可以使用size()和relationships()函数获得所需的结果:
MATCH p = (:Person)-[*]-(r:Room)
RETURN r.name as Room, size( relationships(p) ) as Distance
ORDER BY Distance
输出将是:
╒═══════╤══════════╕
│"Room" │"Distance"│
╞═══════╪══════════╡
│"room1"│3 │
├───────┼──────────┤
│"room2"│3 │
├───────┼──────────┤
│"room3"│4 │
├───────┼──────────┤
│"room4"│4 │
└───────┴──────────┘