Christos Grigoriadis
Christos Grigoriadis

Reputation: 61

Cypher. How do we get the intersection of multiple lists?

I have the following code:

match (n) where apoc.coll.isEqualCollection(["Person","Actor","Old"], labels(n))
with distinct apoc.coll.sort(keys(n)) as keys

Now keys is a list of lists. I want the intersection of the items in the lists. Is there an apoc function for that? apoc.coll.intersection works only for two lists and it requires both arguments.

Upvotes: 2

Views: 332

Answers (1)

Charchit Kapoor
Charchit Kapoor

Reputation: 9284

You can use apoc.coll.intersection function like this:

MATCH (n) WHERE apoc.coll.isEqualCollection(["Person","Actor","Old"], labels(n))
WITH collect(n) as nodesList
RETURN reduce(keyList = keys(nodesList[0]), n IN nodesList | apoc.coll.intersectiob(keyList, keys(n)))

Here, we first match and collect nodes in a list, then we reduce that list, to get the distinct keys, the thing to note here is we are providing the keys of the first node in the list as the initial value to the reduce function.

Upvotes: 1

Related Questions