cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Constraint based querying in neo4j

I've a graph network of 5 nodes (train stations) as in the image below. Every station has a Code, Name, Division and State and then there are relationships with details of Journey_Date, Distance_KM, Time_Taken in hours.

Presently the relationship I could build is at trip level (row level in csv/excel) details. The cypher code for that is:

Step 1)
LOAD CSV WITH HEADERS FROM "file:///five_stations.csv" AS row WITH row MERGE (:Station {name:row.Origin, code:row.Origin_Code, division:row.Origin_Division, state:row.Origin_State, zone:row.Origin_Zone}) MERGE (:Station {name:row.Destination, code:row.Destination_Code, division:row.Destination_Division, state:row.Destination_State, zone:row.Destination_Zone});

Step 2)
LOAD CSV WITH HEADERS FROM "file:///five_stations.csv" AS row WITH row MATCH (origin:Station {code:row.Origin_Code}), (destination:Station {code:row.Destination_Code}) CREATE (origin)-[:Time_Taken {timeTaken:row.Time, jrnyDate:row.Journey_Date, distance:row.Distance_KM}]->(destination);

I want to find:

  • All combination of 2 hops between Main Station back to Main Station
  • Show only combination of 2 hops which fall within a certain time limit (eg: show all round trips where the journey took less than n hours [addition of each hop's time taken])
  • Show only combination of 2 hops which fall within a certain kilometre limit (eg: show all round trips where the journey took less than x kms [addition of each hop's distance])
  • Start date of journey from Main Station of any particular train should be End date of any previous journey [Journey_Date is in 9/27/2019 format]
  • The above 2 hop is on the same 5 station, but it can be n number of hops when we scale up to more.

Note: I'm new to Cypher and Neo4j, asked a question on the same, if you too are learning, you can go through the QA here.

1 REPLY 1

I suggest to rethink your model and code.
What can detect from your screenshot is a network of stations in relation with travel times. Within your description you speak of different trips in a network to be evaluated.
Please consider to go back to Euler (https://en.wikipedia.org/wiki/Seven_Bridges_of_Königsberg), Dijkstra )https://en.wikipedia.org/wiki/Dijkstra's_algorithm) and Hart / Nilsson, Raphael (https://en.wikipedia.org/wiki/A*_search_algorithm). Carefully study how they modeled the network into the graph.