Search code examples
neo4jlimit

Neo4j limit with non-constant


I want to ask if it is possible to limit nodes returned by one segment of query with non-constant, that mean - I want limit to be dependent on some variable returned by query for example 30% of count of neighbor nodes.


Solution

  • It's a bit ugly, but what about something like this?

    MATCH n-[:type]->(neighbor)
    WITH n, toInt(count(neighbor) * 0.3) AS limit
    MATCH n-[:type]->(neighbor)
    WITH n, collect(neighbor)[1..limit] AS neighbors
    UNWIND neighbors AS neighbor
    RETURN n, neighbor