1
votes

Neo4j v2.0.3.

In a graph with ~30000 nodes when trying to get path for a node with command:

MATCH p = (n {id: 'x2116500'})-[*]-(m) RETURN nodes(p) as nodes;

Cypher Web app returns "Unknown error". On a database with 15 nodes this request works as expected though!

Yet request:

MATCH p = (n {id: 'x2116500'})-[r]-(m) RETURN nodes(p) as nodes

returns some nodes, but not all nodes in the path. The same error happens with similar request for getting relations from the path, this one fails:

MATCH p = (n {id: 'x2116500'})-[*]-(m) RETURN relationships(p) as rels ;

and this returns some relations, but not all relations in the path:

MATCH p = (n {id: 'x2116500'})-[r]-(m) RETURN relationships(p) as rels ;
2

2 Answers

1
votes

The [r] syntax (without an asterisk) means that you want just a single relationship in each matched path, so the 2 alternate queries using [r] should not return all relationships (in multi-relationship paths). Therefore, those 2 queries are working as expected.

As for the queries with [*], make sure that you do not have a cycle in the path. Here is an example of a path with a cycle:

(a)-[r1]->(b)-[r2]->(c)-[r3]->(d)-[r4]->(c)

The above path has 3 acyclic relationships. If you do not expect any acyclic path to have more than, say, 4 relationships, you can use the following query to get the distinct nodes in that path, even if a cycle is possible:

MATCH p = ({id: 'x2116500'})-[r*1..4]-()
RETURN DISTINCT nodes(p) as nodes;

This query is also more likely to finish (instead of running forever or running out of memory).

Also, to make this query faster, you should label your start nodes and create an index using that label and the id property.

0
votes

your first query will potentially produce many many million paths. Which take a while to compute. And the browser currently times out after 60s.

First of all, you should use a Label and have an index or constraint on :Label(id)

Try to limit the max-steps or max paths to a sensible value.

//max steps
MATCH p = (n:Label {id: 'x2116500'})-[*..5]-(m) RETURN nodes(p) as nodes;

//max-paths
MATCH p = (n:Label {id: 'x2116500'})-[*]-(m) RETURN nodes(p) as nodes limit 1000;

same for the relationships.

Please note that this query, only goes a single step from node n to m.

If you have known nodes n and m you can also use a function like shortestPath between the two.

MATCH p = shortestPath(n:Label {id: 'x2116500'})-[*]-(m:Label {id:'x3116600'}) 
RETURN nodes(p) as nodes;