Reputation: 95
I have modeled my neo4j database according to this answer by Nicole White in this link
and I also successfully tested the cypher query
MATCH (a:Stop {name:'A'}), (d:Stop {name:'D'})
MATCH route = allShortestPaths((a)-[:STOPS_AT*]-(d)),
stops = (a)-[:NEXT*]->(d)
RETURN EXTRACT(x IN NODES(route) | CASE WHEN x:Stop THEN 'Stop ' + x.name
WHEN x:Bus THEN 'Bus ' + x.id
ELSE '' END) AS itinerary,
REDUCE(d = 0, x IN RELATIONSHIPS(stops) | d + x.distance) AS distance
against a small test graph with 10 nodes.
But my original graph which contains about 2k nodes and 6k relationships causes trouble with the query. The query simply stops and I get an error:
java.lang.OutOfMemoryError: Java heap space
Can you help me to optimize my query or any other solution?
Thank you
Upvotes: 0
Views: 720
Reputation: 39926
try to introduce a WITH
to limit the calculation of :NEXT
paths to only those pairs of a, d that are known to be a shortestpath. It's also a good practice to supply an upper limit for variable path length matches - im using 100 here as an example:
MATCH route = allShortestPaths(
(a:Stop {name:'A'})-[:STOPS_AT*100]-(d:Stop {name:'D'})
)
WITH route, a, d
MATCH stops = (a)-[:NEXT*100]->(d)
RETURN EXTRACT(x IN NODES(route) | CASE WHEN x:Stop THEN 'Stop ' + x.name
WHEN x:Bus THEN 'Bus ' + x.id
ELSE '' END) AS itinerary,
REDUCE(d = 0, x IN RELATIONSHIPS(stops) | d + x.distance) AS distance
Upvotes: 1