Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to understand identifiers and collections in MATCH and WHERE

Tags:

neo4j

cypher

I am trying to understand to what kind of cypher "data structure" certain identifiers or expressions correspond to, depending on how and where they are used. Below I list examples I encountered. Please tell me if I got it right (in the comments) or if I am missing something.

MATCH (a:MYTYPE { label:'l_a' })
// a corresponds to a collection of nodes

MATCH (b:MYTYPE { label:'l_b' })
// so does b

MATCH p=(a)-[sp1:CF*]->(b)-[sp12:CF]->(c)
// p corresponds to a collection of paths
// a and b correspond to a collection of nodes 
// (or does the previous MATCH of a and b change something?)
// sp1 corresponds to a collection of collections of relationships
// sp12 corresponds to a collection of relationships
// c corresponds to a collection of nodes

WHERE ( p = ... )
// Here, the p corresponds to a path, i.e. there must be a path or (I don't know) on the right side of the =
WHERE ( a = ... )
// a corresponds to a node, i.e. there must be a node on the right side of the =
WHERE ( sp1 = ... )
// sp1 corresponds to a collection of nodes, i.e. there must be a collection of relationships on the right side

//BONUS:
WHERE ( (e)-[sp2:CF*]->(f) ) = ...
// there must be a collection of collections of paths on the right side of the =
like image 370
langlauf.io Avatar asked Nov 11 '15 17:11

langlauf.io


1 Answers

I think the easiest way to answer all these questions is by passing identifiers to functions that will throw an error telling you what it expected and what it actually received. I think you should also be careful about how you use the word collection, as it is not correct.

Nodes, Relationships, Paths

MATCH (n) RETURN n;

n is a Node.

MATCH ()-[r]-() RETURN r;

r is a Relationship.

MATCH p = ()-[]-()

p is a Path.

Collections

MATCH (n) WITH COLLECT(n) AS c RETURN c;

c is a Collection<Node>.

MATCH ()-[r]-() WITH COLLECT(r) AS c RETURN c;

c is a Collection<Relationship>.

MATCH p = ()-[]-() WITH COLLECT(p) AS c RETURN c;

c is a Collection<Path>.

Variable Length Paths

MATCH p = ()-[r*..2]-() RETURN p, r;

p is a Path.

r is a Collection<Relationship>.

And to refer to your specific example:

MATCH p = (a)-[sp1:CF*]->(b)-[sp12:CF]->(c)

p is a Path.

a is a Node.

sp1 is a Collection<Relationship>.

b is a Node.

sp12 is a Relationship.

c is a Node.

And I'm not sure what you're asking about the WHERE clauses. Perhaps you can clarify with an edit to your question.

like image 193
Nicole White Avatar answered Oct 17 '22 09:10

Nicole White