0
votes

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

1
I guess more info would be useful, like your schema indexes, neo4j configuration (especially heap settings) and the output of the execution plan.Christophe Willemsen
This bit here: stops = (a)-[:NEXT*]->(d) would seem to enumerate a huge range of paths through your data, considering that it's not specific to any bus route, but just asks which stations can be reached from which other stations. If you have a highly interconnected bus route system, that's going to be a lot of stuff. Some paths might be direct, other paths might traverse most of your system (through connection points).FrobberOfBits
yes it seems that is the problem.so what are my options? im new to cypher and would really appreciate some guidance.SamWiicks

1 Answers

1
votes

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