1

I've tried a bunch of cypher queries, most coming from this question, but none worked.

E.g.:

postgres=# match (a)<-[r]-() where r is null return *;
 a | r
---+---
(0 rows)

The last one I tried is this:

match (n) where not (n)<-[]-() return *

obtaining a syntax error:

postgres=# match (n) where not (n)<-[]-() return *;
ERROR:  syntax error at or near ")"
LINE 1: match (n) where not (n)<-[]-() return *;

I finally fired up Neo4j and found that the above mentioned cypher query works there.

What's the equivalent in AgensGraph (2.1.3) Cypher?

NOTE

While waiting for the correct solution, I worked around the issue with the following sequence of queries:

  1. mark all nodes having an outgoing relation as children match (a)<-[]-(b) SET b.child=true;
  2. find all non-childen nodes match(a) where a.child is null return a;
  3. remove the child marking match(a) where a.child is not null remove a.child;

Eventually wrapped within a transaction so not to alter the graph properties.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
watery
  • 5,026
  • 9
  • 52
  • 92

1 Answers1

3

You're query match (n) where not (n)<-[]-() return *; is close however you need to add 2 more elements to get the query to work.

  1. Your pattern (n)<-[]-() needs to be surrounded by parenthesis.
  2. You need to prefix your pattern with EXISTS

So I ran this query: MATCH (a) WHERE NOT EXISTS ((a)<-[]-()) RETURN *; and it worked.

JoshInnis
  • 108
  • 6
  • Thank you. Though this comes after I almost decided to move to Neo4j (and meanwhile I had [another query](https://stackoverflow.com/q/67007556/3127111) to write that was already hard for me in Neo4j, I didn't try to write it for AgensGraph). Moreover, is this syntax documented in the docs? I can't find anything about `EXISTS` (that probably led me - at that time - to think it was not supported). – watery Apr 26 '21 at 19:16