3
votes

I am currently working with neo4j and I need to find path between 2 nodes in large graph. I am using this cypher query:

MATCH p=(acq:Acquisition {id:'1'})-[r*]->(ecs:ExternalCommunicationService {id:'1'})
RETURN p

Everything works as expected (query returns path of any length between nodes) but I am getting warning message displayed bellow:

Warning: This feature is deprecated and will be removed in future versions. Binding relationships to a list in a variable length pattern is deprecated.

In official documentation is used same pattern with *.

What is the correct way of finding paths of any lengths between nodes without getting any warning(without using deprecated syntax) ?

2

2 Answers

6
votes

Binding relationships to a list in a variable length pattern is deprecated since 3.2.0-rc1.

According this pull request Cypher queries like:

MATCH (n)-[rs*]-() RETURN rs

will generate a warning and the canonical way to write the same query is:

MATCH p=(n)-[*]-() RETURN relationships(p) AS rs

Since you are not using the r variable in your query you can simply remove it from the query and the warning will disappear. This way:

MATCH p=(acq:Acquisition {id:'1'})-[*]->(ecs:ExternalCommunicationService {id:'1'})
RETURN p
2
votes

All that means is that is a future release, your use of r in your pattern will no longer be permitted. Your query will need to look like this or it will break in a future release. Since you are not directly trying to use r in your results it should no problem for you to remove it.

MATCH p=(acq:Acquisition {id:'1'})-[*]->(ecs:ExternalCommunicationService {id:'1'})
RETURN p

You could always get the relationships using the relationships(p) to return a collection of relationships from the path if you needed to do something with them after the match.

Depending on the nature of your graph (size and complexity) though your query may become unwieldy because it is virtually unconstrained except for the ending node and the direction. There are a number of ways you could make it safer.

1 - Use shortestPath

You could use shortestPath or allShortestPaths

MATCH p=allShortestPaths((acq:Acquisition {id:'1'})-[*]->(ecs:ExternalCommunicationService {id:'1'}))
RETURN p

2 - Limit the Depth

You could add a limit on the depth of the match. It could be fixed

MATCH p=(acq:Acquisition {id:'1'})-[*10]->(ecs:ExternalCommunicationService {id:'1'})
RETURN p

or a range

MATCH p=(acq:Acquisition {id:'1'})-[*5..10]->(ecs:ExternalCommunicationService {id:'1'})
RETURN p

3 - Add Relationship TYPE

You could add one or more labels to reduce the potential number of paths you match. That can be used in conjunction with the depth parameters.

MATCH p=(acq:Acquisition {id:'1'})-[:TYPE_A|TYPE_B*5..10]->(ecs:ExternalCommunicationService {id:'1'})
RETURN p

4 - Use APOC

You could use APOC procedures.