cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Variable length pattern not working when specifying label

Hey, hopefully we're just missing something but can someone tell me if this isn't possible?

We're trying to match these two patterns here:

(:User)-[:BELONGS_TO]->(:Crew)-[:BELONGS_TO]->(:Company)
(:User)-[:BELONGS_TO]->(:Company)

Using this query:

MATCH (user:User)-[:BELONGS_TO*0..]->(:Crew)-[:BELONGS_TO]->(:Company)
RETURN DISTINCT user

But we get no results back. However, doing this query works when we don't specify the :Crew label:

MATCH (user:User)-[:BELONGS_TO*0..]->()-[:BELONGS_TO]->(:Company)
RETURN DISTINCT user

Any ideas why you can't specify the label on the intermediate?

3 REPLIES 3

Unless there's a bug, this path may not exist. Can you find in your graph a path in which the pattern matches? You may want to double-check for typos or casing mismatches, as well as checking the directions of the relationships.

Thanks for responding!

If you look at the above picture, you can see 3 scenarios:

  1. Works as intended, path exists:
(:User)-[:BELONGS_TO*0..1]->(:Crew)-[:BELONGS_TO]->(:Company)
  1. Works when the user doesn't belong to a crew and we don't specify the :Crew label in the middle:
(:User)-[:BELONGS_TO*0..1]->()-[:BELONGS_TO]->(:Company)
  1. Does not work when we run against the same path as in example 2 but specifying the :Crew label
(:User)-[:BELONGS_TO*0..1]->(:Crew)-[:BELONGS_TO]->(:Company)

Think it could be a bug?

Doesn't look like a bug to me.

I would guess that the top image doesn't feature the same user as the others (please confirm the uid of the user in the top image (with 3 nodes).

The second image shows that these two nodes fit the pattern, with the :User with the provided uid not having any :Crew node it belongs to. It fits the pattern because it traverses 0 :BELONGS_TO relationships (so the empty node in the pattern is the same node as the :User node), then it traverses another :BELONGS_TO relationships to a :Company node.

The third image shows that there is no such pattern where the :User in question meets the pattern, since there is no :Crew node that is reachable via outgoing :BELONGS_TO relationships.