0
votes

I have a graph with a structure like this:

Sample graph

I need to return all the return all :Treasure which was not :FOUND_BY more than 1 :User, either directly (red path), or via their :Group (blue path).

My cypher

MATCH (t:Treasure)
// WHERE with other conditions
WITH t, SIZE((t)-[:FOUND_BY|MEMBER_OF*1..2]-(:User))) as finders
WHERE finders < 2
RETURN t

returns the nodes I'm looking for, but spends horrendous time on expanding that variable path. How could I optimise this cypher, get rid of the variable path, but keep the same results?

1

1 Answers

0
votes

It seems like you could just do

MATCH (t:Treasure)
WHERE size((t)-[:FOUND_BY]->()) < 2
RETURN t

You can omit the label on the other side of the [:FOUND_BY] relationship so that you take both cases into account. Let me know if I'm misunderstanding.

Edit: Per your comment I think the best way to avoid the variable-length path is:

MATCH (t:Treasure)
WHERE size((t)-[:FOUND_BY]->(:User)) + 
      size((t)-[:FOUND_BY]->(:Group)<-[:MEMBER_OF]-(:User)) < 2
RETURN t