0

I am a newer to Cypher and I came across a problem that really bothered me. If I code like this

MATCH (r:Researcher)
OPTIONAL MATCH (r)-[:SUPERVISES]->(s:Student)
WITH r, count(s) AS studentsSupervised
MATCH (r)-[:AUTHORS]->(p1:Publication)
OPTIONAL MATCH (p1)<-[:CITES*]-(p2:Publication)
RETURN r.name, studentsSupervised, count(DISTINCT p2) AS citedCount` 

It will return the correct answer. But if I do like that

MATCH (r:Researcher)
MATCH (r)-[:AUTHORS]->(p1:Publication)
OPTIONAL MATCH (r)-[:SUPERVISES]->(s:Student)
OPTIONAL MATCH (p1)<-[:CITES*]-(p2:Publication)
WITH r, count(s) AS studentsSupervised
RETURN r.name, studentsSupervised, count(DISTINCT p2) AS citedCount

I got a Error Neo.ClientError.Statement.SyntaxError which referred to Variable p2 not defined (line6, column 51) in "RETURN r.name, students Supervised, count(DISTINCT p2) AS citedCount"

I am puzzled about it and it seems WITH cuts the relation in the context. I will appreciate it if someone knows, please let me know the internal reasons!!!

1 Answer 1

3

When you use WITH you must pass all the variables that you intend to use in further queries. See the documentation for details.

Also be careful that a join is created with all the variables you are passing with WITH. Hence be careful here or else the output might not be what you desire.

See the cypher below where I just added p2 after WITH

MATCH (r:Researcher)
MATCH (r)-[:AUTHORS]->(p1:Publication)
OPTIONAL MATCH (r)-[:SUPERVISES]->(s:Student)
OPTIONAL MATCH (p1)<-[:CITES*]-(p2:Publication)
WITH r, count(s) AS studentsSupervised, p2
RETURN r.name, studentsSupervised, count(DISTINCT p2) AS citedCount
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks a lot! I have another question that where I can find the source code of Cypher
What do you mean by source code of cypher? The source code of neo4j or the final query generated by neo4j after processing your cypher?
The source code of neo4j. Like Hadoop is constructed by Java, I wanna know the source code of neo4j you said or just "cypher".
@Frank the documentation for Cypher is fairly extensive and should suit most of your needs; that said, if you want to look at the source code, it is available on GitHub.
Note that to avoid cardinality issues, it's best to try to keep cardinality low and if you need to aggregate try to do so after your match. For this query, it's better to match and collect students of researchers first, then do the match to cited publications. Currently the count of students supervised will not be correct. While you could correct it with count(DISTINCT s), it's better performance-wise to match to and collect them earlier in the query. You can see this yourself if you compare PROFILEd queries, and check both db hits and the flow of rows between operations.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.