简体   繁体   中英

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.

Eg:

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM