Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is pattern comprehension and custom projection in neo4j cypher

Tags:

neo4j

cypher

I was reading cypher refcard in which I came across following:

Pattern comprehensions may be used to do a custom projection from a match directly into a list:

MATCH (a)
RETURN [(a)-->(b) WHERE b.name = 'Bob' | b.age]

I prepared simple graph and tried similar looking queries on it. But it kept giving error Invalid input 'W': expected whitespace, comment, a relationship pattern on WHERE.

Q1. Whats the meaning of the above cypher, should it return all paths (a)-->(b) with b.name=Bob or return b.age?

Q2. I never saw path specification (a)-->(b) after RETURN. Obviously I am missing some basics here. Whats that?

like image 962
Mahesha999 Avatar asked Feb 16 '17 14:02

Mahesha999


1 Answers

NOTE: Pattern comprehension was only introduced in Neo4j 3.1, versions 3.0.x and below won't have this feature.

Answer to Q1: The meaning in this example is: "Given variable a (since it is in scope from earlier in the query) find an outgoing relation to some node and bind it to the variable b where node b's name property is 'Bob'. Populate a list with the age property of every b node.

The | in this context separates the pattern and where clause from the expression of what values to populate into the resulting list.

Not sure I'm following what you're asking about in Q2.

For your specific usage, why it's giving you an error, we need to be able to see what you're doing with it to figure out the problem. Can you add that to your description?

Though if I was to venture a guess, you might be using a pattern in the pattern comprehension that doesn't have any relationships, something like this:

return [(a:Person) | a.name] as names

Currently usages like this will fail when there is no relationship in the pattern, something I consider a bug, and filed as such to the issues list.

For more info, here's the pattern comprehension entry in the dev guide, and a longer writeup about pattern comprehension (and map projection).

like image 157
InverseFalcon Avatar answered Oct 11 '22 14:10

InverseFalcon