Search code examples
neo4jcyphergraph-databasesneo4j-apoccypher-3.1

Cypher: Query to check if node is part of top 10 results


I'm trying to make a cypher query which does the following

  1. Find the top 10 largest USA organisations (in terms of revenue)
  2. Return "True" if an organisation part of the top 10, if not return "False"

My attempted code looks like this to extract list of top 10 organisations is:

MATCH (org)
WHERE org.revenueCurrency = 'USD'
WITH org as topCompany
ORDER by topCompany.revenue desc LIMIT 10
RETURN topCompany

however not sure how to return to True or False if a company is in the top 10


Solution

  • This is one way:

    MATCH (org)
    WHERE org.revenueCurrency = 'USD'
    WITH org
    ORDER by org.revenue DESC
    WITH COLLECT(org) AS orgs
    UNWIND [i IN RANGE(0, SIZE(orgs)-1) | {org: orgs[i], top10: i < 10}] AS result
    RETURN result
    

    The top10 property of each result row will be a boolean.

    [UPDATE]

    If you want each result row to be "flat" instead of a map, use this altered RETURN clause:

    RETURN result.org AS org, result.top10 AS top10