2016-12-25 118 views
0

我想获得一个密码,可以给我的路线和可用的座位来源到目的地。以下是我的图表设置Neo4j路径与过滤器

create (t1:Trip{id:"red"}), (t2:Trip{id:"blue"}), (a:City{id:"A"}), (b:City{id:"B"}), (c:City{id:"C"}), (d:City{id:"D"}) 
create (t1)-[:stop_at]->(a),(t1)-[:stop_at]->(b),(t1)-[:stop_at]->(c),(t1)-[:stop_at]->(d),(t2)-[:stop_at]->(a),(t2)-[:stop_at]->(b),(t2)-[:stop_at]->(c),(t2)-[:stop_at]->(d) 
create (a)-[:red]->(b),(b)-[:red]->(c),(c)-[:red]->(d) create (a)-[:blue]->(b),(b)-[:blue]->(c),(c)-[:blue]->(d) 
create (b)-[:red_01{seat:40}]->(c),(c)-[:red_01{seat:40}]->(d) 

我有两个“旅行”,“红色”和“蓝色”。使用“旅行”ID在节点之间创建的关系(停止)只是抽象链接。我想寻找停在“A”和“D”的旅行,我用下面的查询返回旅行“红色”和“蓝色”。

match (t:Trip)-[:stop_at]->(c:City) where c.id = "A" or c.id = "D" return distinct t.id 

然后,我想要得到的路径,我正在使用以下查询。

match (t:Trip)-[:stop_at]->(c:City) where c.id = "A" or c.id = "D" with distinct t.id as id 
match (source:City{id:"A"})-[rel*]->(dest:City{id:"D"}) where all(item in rel where starts with id) return rel 

它返回结合了每条可能路径的36行。例如:

[(A)-[:blue]->(B), (B)-[:blue]->(C), (C)-[:blue]->(D)] 
[(A)-[:red]->(B), (B)-[:red]->(C), (C)-[:red]->(D)] 
[(A)-[:red]->(B), (B)-[:red]->(C), (C)-[:red_01 {seat:40}]->(D)] 
[(A)-[:red]->(B), (B)-[:red_01 {seat:40}]->(C), (C)-[:red]->(D)] 
[(A)-[:red]->(B), (B)-[:red_01 {seat:40}]->(C), (C)-[:red_01 {seat:40}]->(D)] 

我想获得情况如下:

[(A)-[:blue]->(B), (B)-[:blue]->(C), (C)-[:blue]->(D)] 
[(A)-[:red]->(B), (B)-[:red_01 {seat:40}]->(C), (C)-[:red_01 {seat:40}]->(D)] 

后缀“_01”是指有两个站之间的日期/时间之旅出售。对于所有的“红色”旅行,我希望只获得包含最大销售额的路径。如果我们看一下上面的结果,它将是第5行。“蓝色”之旅当天没有任何销售,所以我们将保留结果。

这里是neo4j控制台供您参考。非常感谢您的帮助。

http://console.neo4j.org/?id=2sho3j

瑞安

+0

好问题 - 感谢提供样本数据集,并详细描述你的问题。但是,您的第三个查询片段不会编译:'where all(rel in以id开头的项目)'缺少某些内容。正确的表达应该像'WHERE ... STARTS WITH ...'一样阅读 –

回答

0

这个查询是朝着回答您的解决方案了一步:

match (t:Trip)-[:stop_at]->(c:City) 
where c.id = "A" or c.id = "D" 
with distinct t.id as id 
match (source:City{id:"A"})-[rel*]->(dest:City{id:"D"}) 
where all(item in rel where type(item) starts with id) 
return rel, extract(r in rel | type(r)) as reltypes 

它返回:

╒════════════════════════════╤═════════════════════╕ 
│rel       │reltypes    │ 
╞════════════════════════════╪═════════════════════╡ 
│[{}, {}, {}]    │[blue, blue, blue] │ 
├────────────────────────────┼─────────────────────┤ 
│[{}, {seat: 40}, {}]  │[red, red_01, red] │ 
├────────────────────────────┼─────────────────────┤ 
│[{}, {seat: 40}, {seat: 40}]│[red, red_01, red_01]│ 
├────────────────────────────┼─────────────────────┤ 
│[{}, {}, {}]    │[red, red, red]  │ 
├────────────────────────────┼─────────────────────┤ 
│[{}, {}, {seat: 40}]  │[red, red, red_01] │ 
└────────────────────────────┴─────────────────────┘ 

不过,我认为我们应该提高数据模型第一:

  • 在关系类型中使用后缀来表示可能经常发生变化的信息(这里用于指示是否有销售)是一个坏主意。我建议使用关系属性,例如sale: true/false或将销售日期添加为列表(存储日期可能非常棘手:使用时间戳,字符串,APOC date/time support - 无论哪种情况最适合您的用例)。
  • 您也可以考虑将路线的颜色存储为属性,例如, (a)-[:ROUTE {colour: 'red'}]->(b)ROUTE应尽可能描述,例如TRAIN或可能CONNECTED_BY_TRAIN)。

所以,回到你的问题(使用原来的数据模型):

reduce

match (t:Trip)-[:stop_at]->(c:City) 
where c.id = "A" or c.id = "D" 
with distinct t.id as id 
match (source:City{id:"A"})-[rel*]->(dest:City{id:"D"}) 
where all(item in rel where type(item) starts with id) 
return rel, extract(r in rel | type(r)) AS reltypes, 
    reduce(
    noSales = 0, 
    item IN rel | noSales + 
     case type(item) ends with '_01' 
     when true then 1 
     else 0 
     end) AS noSales 

或者简单一点,你可以使用各种方法计算的特定路径上销售宽度filterlength

match (t:Trip)-[:stop_at]->(c:City) 
where c.id = "A" or c.id = "D" 
with distinct t.id as id 
match (source:City{id:"A"})-[rel*]->(dest:City{id:"D"}) 
where all(item in rel where type(item) starts with id) 
return rel, extract(r in rel | type(r)) AS reltypes, 
    length(filter(item in rel where type(item) ends with '_01')) AS noSales 

两个返回以下RESU LT:

╒════════════════════════════╤═════════════════════╤═══════╕ 
│rel       │reltypes    │noSales│ 
╞════════════════════════════╪═════════════════════╪═══════╡ 
│[{}, {}, {}]    │[blue, blue, blue] │0  │ 
├────────────────────────────┼─────────────────────┼───────┤ 
│[{}, {seat: 40}, {}]  │[red, red_01, red] │1  │ 
├────────────────────────────┼─────────────────────┼───────┤ 
│[{}, {seat: 40}, {seat: 40}]│[red, red_01, red_01]│2  │ 
├────────────────────────────┼─────────────────────┼───────┤ 
│[{}, {}, {}]    │[red, red, red]  │0  │ 
├────────────────────────────┼─────────────────────┼───────┤ 
│[{}, {}, {seat: 40}]  │[red, red, red_01] │1  │ 
└────────────────────────────┴─────────────────────┴───────┘ 

为了获得最大的销售,你只需要排序,并获得最高的结果:

order by noSales desc 
limit 1