Using the Where clause in several ways in Neo4j

It seems that the following WHERE clause will not work, because we have two relationships (WorksAt and ResponsibleFor) in our request. If there was only one relationship, then it would work like magic. Here, in the query below, the query returns all courses in the science of the department, but it does not filter out courses not taught by Maria Smith. All I want to do is get only the courses taught by Maria Smith that work in the Science Department. I came across WITH and Start Clause, which seem to be potential offers, make work where you can filter one part of the request before sending it to another.
 http://neo4j.com/docs/stable/query-with.html
but I still could not understand the concept. Anyone for help?

MATCH (d:Department)<-[w:WorksAt]-(t:Tutor)-[r:ResponsibleFor]->(c:Courses) 
WHERE d.name='Science' 
AND  t.name='Maria Smith'
return  c,r
+4
source share
2 answers

There are several ways to discard this particular cat. Let it break.

Find a tutor with the name "Mary Smith" in the Science Department

MATCH (d:Department)<-[:WorksAt]-(t:Tutor)
WHERE d.name = 'Science' AND t.name = 'Maria Smith'
RETURN t

Find the courses the teacher is teaching

MATCH (t:Tutor)-[:ResponsibleFor]->(c:Courses)
RETURN t.name, c

Bring the two together to get the courses that Mary Smith of Scence teaches.

MATCH (d:Department)<-[:WorksAt]-(t:Tutor)
WHERE d.name = 'Science' AND t.name = 'Maria Smith'
WITH t
MATCH (t)-[r:ResponsibleFor]->(c:Courses)
RETURN t.name, r, c

It can also be written as

MATCH (d:Department { name : 'Science' })<-[:WorksAt]-(t:Tutor { name : 'Maria Smith' })
WITH t
MATCH (t)-[r:ResponsibleFor]->(c:Courses)
RETURN t.name, r, c

To maximize query performance, you can use schema indices to quickly find the nodes of your department and teacher. You do it? To create indexes use

CREATE INDEX ON :Department(name)
CREATE INDEX ON :Tutor(name)

Run these lines separately.

, , , .

MATCH (t:Tutor)-[:ResponsibleFor]->(c:Courses)
RETURN t.name as CourseTutor, collect(c.name) as CourseName

, .

+5

. . Wes Freeman . node ( 4 ).

+1

Source: https://habr.com/ru/post/1614712/


All Articles