简体   繁体   中英

Limit results in Neo4j

I have directors who have directed films. Films have genres. I want to get first 5 movies for a director, sorted by release date and for each movie the first 2 genres sorted by name.

MATCH(d {name: "Steven Spielberg"}) -- (f:Film)
WITH f
ORDER BY f.release_date DESC LIMIT 5
MATCH (f) -- (g:Genre)
RETURN f,g

This gives me first 5 movies of Steven Spielberg sorted by release_date. Now for each movie, how do I get first 2 genres sorted by name?

There's a knowledge base article on limiting match results per row that will help you here.

In short, either you can use APOC Procedures for this, or you'll need to COLLECT() results then take the interested slice of the collection.

Your question appears to be a duplicate of How to limit a subquery in cypher? .

However, the accepted self-answer therein is just an approximation to the desired result. In your case it should be something like the following:

MATCH(d {name: "Steven Spielberg"}) -- (f:Film)
WITH f
MATCH (f) -- (g:Genre)
WITH f,g ORDER BY g.name
RETURN f,collect(g)[0..2] ORDER BY f.release_date DESC LIMIT 3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM