Cypher: return whether an optional relationship exists?

1,499 views
Skip to first unread message

Aseem Kishore

unread,
Jan 31, 2012, 11:56:12 PM1/31/12
to Neo4j Discussion
I want to execute a Cypher query that'll return every user in our graph, along with whether I follow them or not.

So I first tried a query like this:

START me=node(1), user=[all users] # index query omitted
MATCH (me) -[r?:follows]-> (user)
RETURN user, r?

My intent with the return `r?` was thinking that the `?` operator would evaluate to true or false, like in CoffeeScript. ("Does r exist?") I realize now that's not the case.

Turns out that this was a syntax error; just returning `r` handles the optional case. But it does something I don't want: in cases that match, it returns the whole relationship JSON.

In this case, I just want to know whether the relationship exists or not. How can I do that?

I thought I'd try `r IS NULL` or `r ISNT NULL`, but those are also syntax errors. I guess `IS NULL` etc. is only accepted in the WHERE clause.

I then thought to try returning TYPE(r), so it'd be "follows" or <null> (hopefully). But this fails silently. (BUG)

Finally, I tried returning COUNT(r), which returns 1 or 0, and that works! So I've found a decent option.

But that does feel a bit hacky to me, so I thought I'd ask if there's a better/proper way of just returning true/false. Thanks!

Aseem

Andres Taylor

unread,
Feb 2, 2012, 6:31:49 AM2/2/12
to ne...@googlegroups.com
You can do it by just returning r. If it's null - you are not following the user. If it's non-null - you follow.

We'll have a chat about the question mark, and allowing predicates in the return clause, and see if we need to change anything.

Thanks for bringing this up!

Andrés

Aseem Kishore

unread,
Mar 1, 2012, 12:29:49 AM3/1/12
to ne...@googlegroups.com
Hey Andres,

So sorry for the delayed reply here. Just ran into this same issue again and had to dig through my email to remember how I worked around this.

You're right that I can just return r, but I ran into this in the first place because I didn't want to return the entire relationship JSON for *each* relationship. In this case, all I care about is whether the relationship exists.

It'd be nice if I could thus just return `true` or `false` for each relationship.

Cheers,
Aseem

Andres Taylor

unread,
Mar 1, 2012, 3:46:29 AM3/1/12
to ne...@googlegroups.com


On Thu, Mar 1, 2012 at 6:29 AM, Aseem Kishore <aseem....@gmail.com> wrote:
<snip>
It'd be nice if I could thus just return `true` or `false` for each relationship.
<snip>

Yeah, I agree. I've been thinking of adding predicates as a normal expression, which would let you do something like:

RETURN r IS NULL 

and that would give you exactly what you want.

We'll get there. Thanks for your patience.

Andrés

Aseem Kishore

unread,
Mar 1, 2012, 3:56:17 AM3/1/12
to ne...@googlegroups.com
No problem, and great to hear! Thanks for the continuous improvements. =)
Reply all
Reply to author
Forward
0 new messages