I have the graph below (try it in the neo4j console). Starting in the middle, how can I write a cypher-query that limits the results to only include one from each 2-level node, e.g. returning only the red nodes?
1 Answers
3
votes
I'm assuming you want choose by random on the second level. In this case the following Cypher statement does the job:
START n=node:node_auto_index("name:start")
MATCH (n)-[:link]->(first)
WITH first
MATCH first-[:link]->(second)
WITH first, collect(second) AS coll
RETURN first, coll[toInt(length(coll)*rand())]
We use collect
to put 2nd degree nodes into a collection for each first
node. Using the array subscript operator we pick one of the elements by random. rand()
returns a value between 0 and <1, so we need to multiply with collection length.