RMorrisey
RMorrisey

Reputation: 7739

Why can't my cypher query use multiple variable-length paths?

In the following graph, how can I match both Rule 1 and Rule 2, using the green nodes "Qualification" and "New York" as conditions?

Help me understand why the following Cypher query does not work:

MATCH 
(r:Rule),
(r)-[w1:When*]->(c:State{instanceName:"New York"}),
(r)-[w2:When*]->(c2:Service{instanceName:"Qualification"})
RETURN * LIMIT 50

This query returns only Rule 2, not Rule 1.

Each of these queries works individually and returns the expected results:

MATCH 
(r:Rule),
(r)-[w2:When*]->(c2:Service{instanceName:"Qualification"})
RETURN * LIMIT 50

MATCH 
(r:Rule),
(r)-[w1:When*]->(c:State{instanceName:"New York"})
RETURN * LIMIT 50

Sample graph

Upvotes: 2

Views: 116

Answers (2)

SylvainRoussy
SylvainRoussy

Reputation: 359

Somtehing like that with separate patterns :

MATCH (r:Rule)
MATCH (r)-[w1:When*]->(c:State{instanceName:"New York"})
MATCH (r)-[w2:When*]->(c2:Service{instanceName:"Qualification"})
RETURN * LIMIT 50

Upvotes: 0

Gabor Szarnyas
Gabor Szarnyas

Reputation: 5047

The query does not return rule 1 due to the uniqueness of relationships in a single MATCH clause. In your example, the When relationship between Rule 1 and Instance is the same for both paths, and only one of them is returned.

To work around this, use two separate MATCH clauses.

MATCH 
  (r:Rule),
  (r)-[w1:When*]->(c:State {instanceName: "New York"}),
MATCH
  (r)-[w2:When*]->(c2:Service {instanceName: "Qualification"})
RETURN *
LIMIT 50

Upvotes: 1

Related Questions