Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
03-15-2019 09:16 PM
Dear Everyone,
I am having issues understanding the "with" clause.
I have read the manual entry more than once, but it isn't detailed in away for me as a new user to see the why. Take a look at the following example. I would like to know what is the difference between the two out comes.
I don't quite understand the effect/importance of the with statement
MATCH (n {name: 'Anders'})--(m)
WITH m
ORDER BY m.name DESC LIMIT 1MATCH (m)--(o)RETURN o.name
MATCH (n {name: 'Anders'})--(m)
ORDER BY m.name DESC LIMIT 1MATCH (m)--(o)RETURN o.name
03-16-2019 08:57 AM
It is very easy.
WITH is just like RETURN just within a query.
it can select, aggregate, limit, sort it's arguments.
The only difference is that you need to name each expression.
Any after the WITH statement only the values / variables that are passed along are accessible/visible.
03-17-2019 07:27 AM
Hello Michael,
Thank you for your explanation. Then I have another/same question.
In my example the person used with in the query.
What made it so important in this case to use it? It seem with or without it they could have gotten the same result.
Kind regards,
Jeffrey
03-17-2019 02:49 PM
EDIT I removed the remarks here because they weren't accurate. The remaining part of my comment is still true though.
You would instead use WITH as Michael has mentioned, as a means of projecting out interim results to work on later, to aggregate, limit, and sort, and to control what's in scope during query execution (instead of doing this all in the RETURN, which often isn't as efficient, or may even be impossible if you have aggregations that depend upon other aggregations).
03-17-2019 03:12 PM
Hello Andrew,
First off excuse any typos
That example is in your manual https://neo4j.com/docs/cypher-manual/current/clauses/with/
I couldn't understand why it was being used that way, and you confirmed that without it, the query execute the same.
The way you just explained it would be very valuable to add to that page and also if you could give some concrete examples on the page also of the scope. I don't see any explanation anywhere as of yet explaining the idea of scope and how it works. You hinted to it. I could understand what you typed, but yet don't understand the use cases as you wrote. It is still a bit abstract to me as of now.
As a new user to Neo4j and the cypher language, I have noticed that there is very little that is going into what happens when you add certain things together. An example of a post that does explain it pretty well is the one about merge, there needs to be more written from a "consequence of things". That is how the Merge Understanding how MERGE works
It first tells you how it normally works, but then explains you how it reacts if you go off script (by having a constraint in place and using "ON CREATE SET" because you had a constraint.
It is this type of explanations that allow for insight into the language. It helps to understand the behavior of the language and not only staying focused on the syntax of the language.
Learning the behavior of the language allows for expected results to realized without having used a certain command. It is the way we interpret the world.
When we get to know someone we get to know in advance how they would react without having interacted with them on the specific thing we are thinking of. And that way we achieve our end goal with the person quicker.
Cypher is nothing more than the attitude of the programmers and each language has an attitude. Python has a style, Ruby has a style and so do Javascript and many others.
So my request to you is if you can add more behavioral / contextual explanations in your manual and videos so that we as new user understand the language better and see the use cases more easily.
Thank you in advance,
Jeffrey
03-17-2019 03:25 PM
Ah, excuse me, I just skimmed the queries and didn't read carefully enough, you can ignore my previous comments about the queries behaving identically, or WITH being useless, because that's not true. It's actually pretty important here.
The second query will encounter a compiler error because ORDER BY and LIMIT can only follow a WITH or a RETURN. You need WITH m
there for you to be able to do that.
Also, by performing your ORDER BY and LIMIT at this point, you have a more efficient query, since the following MATCH will only expand off of the single node you limited to just before. This avoids expanding off of nodes where the results will only be pruned later. We prevent ourselves from executing unnecessary work.
That's often much of what you need to do when performing query tuning: ensuring you're adequately filtering, limiting, and aggregating as early in the query as it makes sense (and before property projection in most cases), that way you end up working with a more relevant result set as much as possible. You want to always avoid performing operations on results that will only get pruned away, if possible.
03-17-2019 03:34 PM
Andrew,
You really have to add this type of stuff to the manual. This made it so clear. I now know so much more things that I was having issue with. This is what I mean with the behavior.
Thank you for that more detailed explanation. This will be so helpful to other if you add this to the with page.
This is the consequence. The "why" you do it explanation. I have been watching and reading hours of stuff on cypher, but no one was explaining this type of stuff. Basically you must use the with because....
So as a new user I would like to ask if you could take some time to read thru the manual and add these types of behind the scenes / gotchas. As you saw i was struggling to grasp the why.
As you know I read you merge post. Is there anything simular you can explain about the merge? As is your post really helped, but I do wonder what you could say from the compiler point of view on all these different clauses / queries.
Is there anywhere I can read about the use of scope in cypher?
Greetings,
Jeffrey
All the sessions of the conference are now available online