I am trying to create a query in Neo4J (which is something I'm literally learning as I go) but I am struggling. So I am using the Neo4J sandbox and movie data set provided (:play movie-graph). This is what I am looking for...
Create a query to retrieve all the movie directors that worked with actors 55 years or older at the time the movie was released
Display the networks of actors, writers, and movies for these actors
This is what I have but I'm sure it is completely wrong. Any help would be appreciated!
MATCH (directors)<-[:DIRECTED]-(directors)-[:ACTED_IN]->(actors)-(actors: Age) WHERE actors.age >= 55 RETURN directors.name
Here is a query which will return all directors who directed movies wit actors born before 1970. Change the year to any year you need it to be to satisfy your criteria.
MATCH (q)-[r:DIRECTED]->(k)<-[:ACTED_IN]-(s) where s.born<1970 RETURN DISTINCT q;
Few suggestions for you,
. Understand neo4j nodes and relationships. In your query, you have written (directors), (actors) etc.
I dont find these entities in the movie dataset. It shows that you need to understand these concepts and how these are represented in the queries. The browser console shows these entities clearly.
. Understand relationship directions and how they are represented in the queries.
. Modify the above query in small ways and see how the results changes. For ex, instead of q, return s. Remove DISTINCT clause etc.
. Finally create your own custom data set and try out queries.
Good luck.
To find the names of directors who directed movies that had an actor aged 55+ at the time the movie was released, you can take advantage of the new existential subquery in neo4j 4.0:
MATCH (actor:Person)-[:ACTED_IN]->(m)<-[:WROTE]->(w)
WHERE m.released-actor.born >= 55
RETURN actor, COLLECT(DISTINCT m) AS movies, COLLECT(DISTINCT w) AS writers
To get the actors who were 55+ at the time a movie they were in was released, along with a list of those movies and a list of their writers:
MATCH (w)-[:WROTE]->(m)<-[:ACTED_IN]-(actor:Person)
WHERE m.released-actor.born >= 55
RETURN actor, COLLECT(DISTINCT m) AS movies, COLLECT(DISTINCT w) AS writers
Related
I have constructed a query to find the people who follow each other and who have read books in the same genre. Here it is:
MATCH (u1:User)-[:READ]->(b1:Book)
WITH collect(DISTINCT b1.genre) AS genres,u1 AS user1
MATCH (u2:User)-[:READ]->(b2:Book)
WHERE (user1)<-[:FOLLOWS]->(u2) AND b2.genre IN genres
RETURN DISTINCT user1.username AS user1,u2.username AS user2
The idea is that we collect all the book genres for one of them, and if a book read by the other is in that list of genres (and they follow each other), then we return those users. This seems to work: we get a list of distinct pairs of individuals. I wonder, though, if there a quicker way to do this? My solution seems somewhat clumsy, but I found it surprisingly finicky trying to specify that they have read a book in the same genre without getting back all the pairs of books and duplicating individuals. For example, I
first wrote the following:
MATCH (b1:Book)<-[:READ]-(u1:User)-[:FOLLOWS]-(u2:User)-[:READ]->(b2:Book)
WHERE b1.genre = b2.genre
RETURN DISTINCT u1.username AS user1, u2.username AS user2
Which seems simpler, but in fact it returned repeated names for all the books that were read in the same genre. Is my solution the simplest, or is there a simpler one?
This is one way of rewriting the query
MATCH (n1:User)-[:FOLLOWS]-(n2:User)
MATCH (n1)-[:READ]->(book), (n2)-[:READ]->(book2)
WHERE book.genre = book2.genre
RETURN n1.username, n2.username, count(*)
Here is another collecting genres for each user
MATCH (n1:User)-[:FOLLOWS]-(n2:User)
WITH n1, n2,
[(n1)-[:READ]->(book) | book.genre] AS g1,
[(n2)-[:READ]->(book) | book.genre] AS g2
WHERE ANY(x IN g1 WHERE x IN g2)
RETURN n1, n2, count(*)
Note that sometimes longer queries are not especially better in the sense that the ways the data are retrieved need to make sense to yourself.
Your model however clearly shows that you would benefit from a bit of graph refactoring, extracting the genre into its own node, for eg
MATCH (n:Book)
MERGE (g:Genre {name: n.genre})
MERGE (n)-[:HAS_GENRE]->(g)
And this would be the new query which leverages a graph model
PROFILE
MATCH (n1:User)-[:FOLLOWS]-(n2:User)
WHERE (n1)-[:READ]->()-[:HAS_GENRE]->()<-[:HAS_GENRE]-()<-[:READ]-(n2)
RETURN n1.username, n2.username, count(*)
I've recently started learning Cypher. I have a database containing four users and films. Users can have can have [:WATCHED] / [:WATCHLISTED] / [:FAVORITED] relationships with films.
I want to get the films which all four users have watched. Here's a working query I've written:
match (u1)-[:WATCHED]->(f)<-[:WATCHED]-(u2),
(u3)-[:WATCHED]->(f)<-[:WATCHED]-(u4)
return u1, u2, u3, u4, f
I wanted to know if there was a more efficient way to do this. Or any another way, which I can't of. I'm asking this out of curiosity.
You can do this for example :
MATCH (f:Film)
WHERE size((f)<-[:WATCHED]-()) = 4
RETURN f, [(f)<-[:WATCHED]-(u:User) | u] as watchers
Here I assume that there is only one relationship of type WATCHED between a user and a movie, even if the user has watched the movie many times.
To avoid having to hardcode a User node count, this query efficiently gets the count using the DB's internal statistics:
MATCH (u:User)
WITH COUNT(u) AS userCount
MATCH (f:Film)
WHERE SIZE((f)<-[:WATCHED]-()) = userCount
RETURN f;
This query does not return the users that watched the film, since that is literally all the users in the DB, and with a sufficiently large number of them your query can run out of memory -- or it can take a very long time for a client (like the neo4j Browser) to receive and process the results. I think the main point of a query like this is to find the films, not the users. If you really want to get all the users, a separate query will do: MATCH (u:Users) RETURN u.
You can use all:
https://neo4j.com/docs/developer-manual/current/cypher/functions/predicate/
This checks if a predicate is true for all elements.
I use database cineasts(actor and movie). It has relationship (:ACTOR)-[:ACTED_IN]->(:Movie). Now I want to find the actors who has acted in all of the movies that actor "abc" acts in.
My idea is first to get the movie collection of "abc" using WITH COLLECT. Then using ALL() to find the required actors. But I am not sure how to write the filter in ALL(). How to write it?
Take a look at this Neo4j knowledge base article on performing match intersection.
The kind of queries you're looking for are going to be similar.
For example, using the first technique mentioned, we can do something like this:
MATCH (abc:Actor{name:'abc'})-[:ACTED_IN]->(m:Movie)
WITH abc, collect(distinct m) as movies
WITH abc, movies, size(movies) as movieCnt
UNWIND movies as m
MATCH (m)<-[:ACTED_IN]-(a:Actor)
WHERE abc <> a
WITH a, collect(distinct m) as commonMovies, movieCnt
WHERE size(commonMovies) = movieCnt
RETURN a
If you wanted to use the alternate approach with ALL(), it might look like this:
MATCH (abc:Actor{name:'abc'})-[:ACTED_IN]->(m:Movie)
WITH abc, collect(distinct m) as movies
WITH abc, movies, head(movies) as first
MATCH (first)<-[:ACTED_IN]-(a:Actor)
WHERE abc <> a AND ALL(m in movies WHERE (m)<-[:ACTED_IN]-(a))
RETURN a
We start the match from the first of the movies collection so we start from a relevant set of :Actors instead of having to filter starting from all :Actor nodes. That can be improved further if we sort the movies by the number of actors ascending first, since that will lead to the narrowest starting pool of coactors.
I am going through the online course at http://www.neo4j.org/learn/online_course.
In that under the section (Graph LAB) - (Paths), the below query was used to RETURN all of the actors and directors in all of the movies.
MATCH (a)-[:ACTED_IN]->(m)<-[:DIRECTED]-(d)
RETURN a.name, m.title, d.name;
It is perfectly alright.
To the next question "How would you change this query to RETURN only the directors who acted in their own movies?"
They gave the solution as change (d) to (a). So the query is,
MATCH (a)-[:ACTED_IN]->(m)<-[:DIRECTED]-(a)
RETURN a.name, m.title, a.name;
When i execute this query it throws output as "Unforgiven, Clint Eastwood".
But my doubt is when i look at the interactive network diagram, the node "Clint Eastwood" was connected to the "Movie" node only through the relationship "DIRECTED". There was not a separate relationship "ACTED_IN". Then how neo4j selects only "Unforgiven, Clint Eastwood" and discards other directors.
Please clarify this. Is my understanding wrong.
r karthik.
Yes, the DIRECTED relationship is overlaying the ACTED_IN one. You can see the ACTED_IN relationship after deleting the DIRECTED relationship:
MATCH (a)-[:ACTED_IN]->(m)<-[d:DIRECTED]-(a)
DELETE d;
I agree that the visualization should somehow avoid letting overlays like this happen. You should bring this to the attention of the neo4j folks. At the very least, they may want to change the tutorial to avoid this situation.
This works for me:
MATCH (a)-[:ACTED_IN]->(m)<-[:DIRECTED]-(a)
RETURN a.name, m.title;
I'm moving my complex user database where users can be on one of many teams, be friends with each other and more to Neo4j. Doing this in a RDBMS was painful and slow, but is simple and blazing with Neo4j. :)
I was hoping there is a way to query for
a relationship that is 1 hop away and
another relationship that is 2 hops away
from the same query.
START n=node:myIndex(user='345')
MATCH n-[:IS_FRIEND|ON_TEAM*2]-m
RETURN DISTINCT m;
The reason is that users that are friends are one edge from each other, but users linked by teams are linked through that team node, so they are two edges away. This query does IS_FRIEND*2 and ON_TEAM*2, which gets teammates (yeah) and friends of friends (boo).
Is there a succinct way in Cypher to get both differing length relations in a single query?
I rewrote it to return a collection:
start person=node(1)
match person-[:IS_FRIEND]-friend
with person, collect(distinct friend) as friends
match person-[:ON_TEAM*2]-teammate
with person, friends, collect(distinct teammate) as teammates
return person, friends + filter(dupcheck in teammates: not(dupcheck in friends)) as teammates_and_friends
http://console.neo4j.org/r/oo4dvx
thanks for putting together the sample db, Werner.
I have created a small test database at http://console.neo4j.org/?id=sqyz7i
I have also created a query which will work as you described:
START n=node(1)
MATCH n-[:IS_FRIEND]-m
WITH collect(distinct id(m)) as a, n
MATCH n-[:ON_TEAM*2]-m
WITH collect(distinct id(m)) as b, a
START n=node(*)
WHERE id(n) in a + b
RETURN n