cypheragens-graph

How do I search for nodes without incoming edges/relations in AgensGraph?


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.


Solution

  • 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.