Comparing array values in with cypher / neo4j

Viewed 4010

I have a graph of members and the items that they've looked at.

This data is going to be used to recommend items based on items that similar members have looked at. I'd like to sort the items based on how similar the colors of the items are. The colors are stored on the items in an array (["red", "blue", "green"]). Is there any way in cypher to compare arrays to see how many elements they have in common?

2 Answers

The solution given above is good. But in the new version(4.0), they have made some changes. The Filter() method is deprecated and list() is

Restricted to only work on paths

in Version 4.0

They replaced the filter with List comprehension so instead of using the above code

MATCH n, m
WHERE n.id = 1 AND m.id = 2
RETURN length(FILTER(x in n.color WHERE x in m.color))

I recommend you to use this one.

MATCH n, m
WHERE n.id = 1 AND m.id = 2
RETURN size([x in n.color WHERE x in m.color])
Related