If you have a lot of nodes, try creating an index so that the neo4j DB engine does not have to search through every node to find the ones with the right Name
property values.
I am presuming that, in your example, the n
and m
nodes are really the same "type" of node. If this is true, then:
Add a label (I'll call it 'X') to every node (of the same type as n
and m
). You can use the following to add the 'X' label to node(s) represented by the variable n
. You'd want to precede it with the appropriate MATCH clause:
SET n:X
Create an index on the Name
property of nodes with the X
label like this:
CREATE INDEX ON :X(Name);
Modify your query to:
MATCH p=((n:X {Name:"Node1"}) -[*1..4]-> (m:X {Name:"Node2"}))
RETURN p;
If you do the above, then your query should be faster.
LIMIT 1
when returning. – MarcoL