Not clear where I wanna use WITH

27 views
Skip to first unread message

Johnny Weng Luu

unread,
May 20, 2012, 3:45:09 AM5/20/12
to ne...@googlegroups.com
In this query I have a:

            START root = node(*)
            MATCH root-[:HAS_DEPARTMENTS]->()<-[:IS_DEPARTMENT]-department-[relationship1:HAS_CATEGORY_SUGGESTION]->categorySuggestion-[relationship2:IS_CATEGORY_SUGGESTION]->()<-[:HAS_CATEGORY_SUGGESTIONS]-root
            WHERE root.name = "Store" and department.name = "foo" and categorySuggestion.name = "bar"
            DELETE relationship1, relationship2

            WITH root, department, categorySuggestion
            MATCH root-[:HAS_CATEGORIES]->categories
            CREATE department-[:HAS_CATEGORY]->categorySuggestion
            CREATE categories<-[:IS_CATEGORY]-categorySuggestion

I have a WITH clause separating two parts.

If I move the DELETE down below WITH I will get an error.

I just wonder, how will I know where I should use WITH.

Are there some golden rules?

Michael Hunger

unread,
May 20, 2012, 5:18:21 AM5/20/12
to ne...@googlegroups.com
you get an error, b/c WITH creates a boundary which declares which variables are available in the next query part.

If you leave off the WITH cypher adds an implicit WITH between two query parts that it can identify (e.g. read followed by write) that carries over _ALL_ the context to the next part which is often much more than you need.

If you use with you can declare explicitly what you need, making the query easier to understand and the execution more efficient.

As you don't declare relationship1, relationship2 in your WITH they are not available in the next query part, so DELETE cannot see them.

As WITH is very similar to return, it is also possible to use aggregates there which allows you to create new intermediate results that look differently from the subgraphs you queried first (which allows you to work on the aggregates, e.g. create a HAVING using WITH and WHERE)

Michael

Johnny Luu

unread,
May 20, 2012, 5:54:44 PM5/20/12
to ne...@googlegroups.com
This is very good information!

Could be very useful in the doc!

Johnny

Sent from my iPad

Michael Hunger

unread,
May 20, 2012, 6:07:34 PM5/20/12
to ne...@googlegroups.com
Johnny,

do you mind reposting your question on SO, so that we can put the answer there too?

will make it easier for people to find.

Michael

And we'll improve the docs too!

Johnny Weng Luu

unread,
May 20, 2012, 9:59:21 PM5/20/12
to ne...@googlegroups.com
Np.

Here is the question in Stackoverflow:

http://stackoverflow.com/questions/10678761/where-to-use-with

Johnny
Reply all
Reply to author
Forward
0 new messages