cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Chaining MATCH expressions

munshine
Node Clone

Hello 🙂

I'm trying to get Project nodes and their status, type, department and tag.

This is my query:

MATCH (p:Project)
MATCH (p)-[IS_STATUS]->(status:ProjectStatus)
MATCH (p)-[MEMBER_OF]->(type:Group)-[IS_TAG]->(GroupTag {slug:"type"})
MATCH (p)-[MEMBER_OF]->(dept:Group)-[IS_TAG]->(GroupTag {slug:"department"})
MATCH (p)-[MEMBER_OF]->(tag:Group)-[IS_TAG]->(GroupTag {slug:"tag"})
RETURN p, status, type, dept, tag

Is this query valid or am I misunderstanding something with Cypher ? Because it returns nothing, but I do get projects returned if I remove two of the three last MATCH, any of them.

Does my problem makes sense?

Thanks 🙂

1 ACCEPTED SOLUTION

First, make sure your relationship types are prefixed by :, so for example, :MEMBER_OF. If the : is missing, then it's treated as a variable for the relationship, and the relationship will match on any rel type.

Likewise the last nodes should be :GroupTag for their label, otherwise it will treat GroupTag as a variable to match to any node.

And more importantly, when you reuse variables across your query, it means that the entity must be the same. This is also why your Cypher isn't returning any results. You are declaring that the GroupTag node must be the same node across those match patterns, but must have differing slug properties. This is impossible, as a single node cannot have multiple values for a specific property.

So do not confuse labels/types with variables, if it's a label or type it must be prefixed with :, otherwise your query will not do what you want it to do, and you'll get incorrect results.

View solution in original post

4 REPLIES 4

One thing to keep in mind in your additional matches, it functions like a SQL inner join would. If there aren't relationships to support all the conditions you're matching on, it's not going to return any data. If you want an equivalent Left Join you can use an optional match clause.

intouch_vivek
Graph Steward

Kindly provide your data model and exact requirement

First, make sure your relationship types are prefixed by :, so for example, :MEMBER_OF. If the : is missing, then it's treated as a variable for the relationship, and the relationship will match on any rel type.

Likewise the last nodes should be :GroupTag for their label, otherwise it will treat GroupTag as a variable to match to any node.

And more importantly, when you reuse variables across your query, it means that the entity must be the same. This is also why your Cypher isn't returning any results. You are declaring that the GroupTag node must be the same node across those match patterns, but must have differing slug properties. This is impossible, as a single node cannot have multiple values for a specific property.

So do not confuse labels/types with variables, if it's a label or type it must be prefixed with :, otherwise your query will not do what you want it to do, and you'll get incorrect results.

munshine
Node Clone

Thanks a lot !

Sorry for the late reply, I got sucked into urgent tasks and forgot to comment. The non-prefixed relationships tip did help A LOT, I was making that mistake in all my queries.

Nodes 2022
Nodes
NODES 2022, Neo4j Online Education Summit

All the sessions of the conference are now available online