0
votes

I have a graph database where Airports are connected with each other by Flights using relations :ORIGIN and :DESTINATION. To each flight there's a ticket connected by relation :ASSIGN. The price is saved on a ticket.

Now I need to find all paths starting in Airport LAX, but with price less than 3000. I was able to find all paths, but I don't know how to connect a tickets to it.

My query for finding paths:

MATCH (origin:Airport { name:"LAX" }),(destination:Airport)
WITH origin,destination
MATCH path = (origin)-[r*..5]-(destination)
RETURN path

One path that is returned from that query:

[{"name":"LAX"},{},{"date":"11/29/2015 15:18:22","duration":269,"dista nce":2611,"airline":"19977"},{"date":"11/29/2015 15:18:22","duration": 269,"distance":2611,"airline":"19977"},{},{"name":"BOS"},{"name":"BOS" },{},{"date":"11/29/2015 01:20:17","duration":289,"distance":2704,"air line":"19977"},{"date":"11/29/2015 01:20:17","duration":289,"distance" :2704,"airline":"19977"},{},{"name":"SFO"}]

How to assign ticket to it and make a where to have only those with price less than 3000?

1

1 Answers

2
votes

Using this query to produce sample data:

CREATE (origin:Airport { name:"LAX" })<-[:ORIGIN]-(f1:Flight {date:"11/29/2015 15:18:22",duration:269,distance:2611,airline:"19977"})-[:DESTINATION]->(d1:Airport {name:"BOS"})<-[:ORIGIN]-(f2:Flight {date:"11/29/2015 01:20:17",duration:289,distance :2704,airline:"19977"})-[:DESTINATION]->(d2:Airport {name:"SFO"}),
(t:Ticket {id: 1, price: 1234})-[:ASSIGN]->(f1),
(t1:Ticket {id: 2, price: 555})-[:ASSIGN]->(f2)

this query should produce all paths up to length 5 in which each leg's price is less than 3000:

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

The results will contain 2 paths. If you changed the price of t1 to 3000, then the results will only contain 1 path (the shorter one).

[UPDATE]

If you only want paths where the total ticket price is less than 3000, then this would work:

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