简体   繁体   English

在 Cypher Neo4j 中找到正确的路径

[英]Finding right path in Cypher Neo4j

I'm working with Flight Analyzer database ( https://neo4j.com/graphgist/flight-analyzer ).我正在使用 Flight Analyzer 数据库( https://neo4j.com/graphgist/flight-analyzer )。 We have there few nodes and relationships types.我们的节点和关系类型很少。 Nodes: Airport节点:机场

(SEA:Airport { name:'SEA' })

Flight航班

(f0:Flight { date:'11/30/2015 04:24:12', duration:218, distance:1721, airline:'19977' })

Ticket

(t1f0:Ticket { class:'economy', price:1344.75 })

Relationships Destination关系目的地

(f0)-[:DESTINATION]->(ORD)

Origin起源

(f0)-[:ORIGIN]->(SEA)

Assign分配

(t1f0)-[:ASSIGN]->(f0)

Now I need to find some path and I have problem with that connection ORIGIN - FLIGHT - DESTINATION.现在我需要找到一些路径,但我对连接 ORIGIN - FLIGHT - DESTINATION 有疑问。 I need to find all airports that are connected to LAX airport with sum of ticket prices < 3000.我需要找到与 LAX 机场相连且票价总和 < 3000 的所有机场。

I tried我试过了

MATCH path = (origin:Airport { name:"LAX" })<-[r:ORIGIN|DESTINATION*..5]->(destination:Airport)
WHERE REDUCE(s = 0, n IN [x IN NODES(path) WHERE 'Flight' IN LABELS(x)] |
s + [(n)<-[:ASSIGN]-(ticket) | ticket.price][0]
) < 3000
RETURN path

but in this solution LAX can be ORIGIN and DESTINATION too.但在这个解决方案中,LAX 也可以是 ORIGIN 和 DESTINATION。 I only want to chose paths that always have the same order aiport1 <- origin - flight1 - destination -> airport2 <- origin - flight2 - destination -> aiport etc..我只想选择始终具有相同顺序的路径 aiport1 <- origin - flight1 - destination -> airport2 <- origin - flight2 - destination -> aiport 等。

I need to include departure and arrive time so flight1 date + duration < flight2 date then flight2 date + duration < flight3 date etc...我需要包括出发和到达时间,所以航班 1 日期 + 持续时间 < 航班 2 日期然后航班 2 日期 + 持续时间 < 航班 3 日期等...

[UPDATED] [更新]

This query should check that:此查询应检查:

  • matched paths have alternating ORIGIN/DESTINATION relationships, and匹配的路径具有交替的ORIGIN/DESTINATION关系,并且
  • every departing flight lands at least 30 minutes before the next departing flight (if any), and每个出发航班至少在下一个出发航班(如果有)前 30 分钟降落,并且
  • the sum of the ticket prices of the Flight nodes (which are every other node starting at the second one) < 3000 Flight节点的票价总和(从第二个节点开始每隔一个节点)< 3000
MATCH p = (origin:Airport {name: 'LAX'})-[:ORIGIN|DESTINATION*..5]-(destination:Airport)
WHERE
  ALL(i IN RANGE(0, LENGTH(p)-1) WHERE
    TYPE(RELATIONSHIPS(p)[i]) = ['ORIGIN', 'DESTINATION'][i] AND
    (i%4 <> 1 OR (i + 2) > LENGTH(p) OR
      (apoc.date.parse(NODES(p)[i].date,'m','MM/dd/yyyy hh:mm:ss') + NODES(p)[i].duration + 30) < apoc.date.parse(NODES(p)[i+2].date,'m','MM/dd/yyyy hh:mm:ss'))
  ) AND
  REDUCE(s = 0, n IN [k IN RANGE(1, LENGTH(p), 2) | NODES(p)[k]] |
    s + [(n)<-[:ASSIGN]-(ticket) | ticket.price][0]
  ) < 3000
RETURN p

The query uses the apoc.date.parse function to convert each date into the number of epoch minutes, so that a duration (assumed to also be in minutes) can be added to it.该查询使用apoc.date.parse function 将每个date转换为纪元分钟数,以便可以向其添加duration (假设也是以分钟为单位)。

I believe, you should create new relationships like flyto from an airport to an airport with ticket price and ticket class.我相信,您应该创建新的关系,例如从机场飞往机场的机票价格和机票 class。 it can be useful.它可能很有用。 then you can find flights easier.然后您可以更轻松地找到航班。

 match 
(a:Airport )<-[:ORIGIN]-(f:Flight)-[:DESTINATION ]->(b:Airport ),
(f)-[:ASSIGN]-(t:Ticket)
CREATE (a)-[r:FLY_TO {price:t.price,Class:t.class} ]->(b)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM