Multiple MATCH clauses?

1,014 views
Skip to first unread message

Johnny Weng Luu

unread,
May 19, 2012, 6:51:55 PM5/19/12
to ne...@googlegroups.com
It seems not possible to have more than one MATCH clause.

            START ref = node(*)
            MATCH ref-[:HAS_DEPARTMENTS]->departments<-[:IS_DEPARTMENT]-department
            MATCH ref-[:HAS_CATEGORIES]->categories
            WHERE ref.name = "App" and department.name = "Multimedia"
            CREATE category = {name: "Keyboard"}
            CREATE department-[:HAS_CATEGORY]->category
            CREATE categories<-[:IS_CATEGORY]-category

However, I need the second MATCH clause to fetch "categories" for the last CREATE clause.

Is there a way around this?

Johnny

Johnny Weng Luu

unread,
May 19, 2012, 7:06:19 PM5/19/12
to ne...@googlegroups.com
This worked pretty great!

            START ref = node(*)
            MATCH ref-[:HAS_DEPARTMENTS]->departments<-[:IS_DEPARTMENT]-department
            WHERE ref.name = "App" and department.name = "Multimedia"
            WITH ref, department
            MATCH ref-[:HAS_CATEGORIES]->categories
            CREATE category = {name: "Keyboard"}
            CREATE department-[:HAS_CATEGORY]->category
            CREATE categories<-[:IS_CATEGORY]-category

Worked great when I added a WITH clause.

1. Is this the preferred way?

2. In my query you can see that I am fetch all nodes (ref = node(*)) and getting the reference node with ref.name = "App". Is this the recommended way to get a specific node since you cannot pick one from the START clause directly?

Alistair Jones

unread,
May 19, 2012, 8:36:03 PM5/19/12
to ne...@googlegroups.com
Hi Johnny,

The MATCH clause can accept multiple patterns, separated by commas.  So you could write:

MATCH ref-[:HAS_DEPARTMENTS]->departments<-[:IS_DEPARTMENT]-department, ref-[:HAS_CATEGORIES]->categories

When you use the WITH clause, you're effectively starting a new chained query, passing the ref and department variables along.

On 20 May 2012 00:06, Johnny Weng Luu <johnny....@gmail.com> wrote:

1. Is this the preferred way?

I think it would be preferable to just use a single MATCH clause, with multiple patterns. Only use WITH if you can't achieve what you want with a simpler query.

2. In my query you can see that I am fetch all nodes (ref = node(*)) and getting the reference node with ref.name = "App". Is this the recommended way to get a specific node since you cannot pick one from the START clause directly?

I don't know your full graph, but it doesn't look like you need to start from node(*) (all nodes in the graph).  Can you share some more details about how you are modelling your domain?  What does "ref" represent?

cheers,

-Alistair

Michael Hunger

unread,
May 20, 2012, 5:11:09 AM5/20/12
to ne...@googlegroups.com
yes this is the preferred way, WITH allows you to chain queries.

each of the queries can be a read-or a modifying query.

with two read-queries you can create constructs like having.

#2 
- you would normally index start-nodes with the properties that you want to look up, then you can query them directly in the start-clause
- other ways would be category nodes that e.g. an apps-category linked to your "app-nodes" from which you then iterate the attached nodes and find the app-node
- if you need this specific app-node very often it might be sensible to store it in your app and use it directly in app=node({appNode})

Please try to use parameters {param} wherever possible so that you can enjoy cypher's query caching and you create reusable queries anyway.

Michael
Reply all
Reply to author
Forward
0 new messages