Neo4j Cypher总线路由选择

时间:2013-05-21 17:56:58

标签: neo4j cypher

我是Cypher的新手,我想知道Neo4j Cypher是否有以下案例:

bus route example

当我想查询从1号站到4号站的总线时,输出应该是(包括最少的交换次数):

  1. 站1 - >路线1 - >站3 - >路线3 - >第4站
  2. 站1 - >路线2 - >站3 - >路线3 - >第4站
  3. 但并非所有可能的组合:

    1. 站1 - >路线1 - >站2 - >路线1 - >站3 - >路线3 - >第4站
    2. 站1 - >路线1 - >站2 - >路线2 - >站3 - >路线3 - >第4站
    3. 站1 - >路线2 - >站2 - >路线1 - >站3 - >路线3 - >第4站
    4. 站1 - >路线2 - >站2 - >路线2 - >站3 - >路线3 - >第4站
    5. 谢谢!

1 个答案:

答案 0 :(得分:3)

没有条件表达式就很困难(CASE / WHEN现在是2.0)。这与我在几分钟尝试中得到的一样接近。您必须从生成的关系集合中提取起始节点。

start st1=node:node_auto_index(name="station1"), st4=node:node_auto_index(name="station4") 
match p=st1-[r*]->st4 
with reduce(acc=[], route in rels(p): 
  case 
    when length(acc) > 0 and last(extract(a in acc: a.name)) = route.name then acc 
    else acc + route 
  end) as reducedRoutes 
return reducedRoutes, length(reducedRoutes) as len 
order by len;

http://console.neo4j.org/r/koe6fo

相关问题