Comparing array values ​​in cypher / neo4j

I have a graph of the members and elements that they were looking at.

This data will be used to recommend items based on items that similar members have looked at. I would like to sort the elements depending on how similar the colors of the elements are. Colors are saved on elements in the array (["red", "blue", "green"]). Is there a way in cypher to compare arrays to find out how many elements they have?

+6
source share
1 answer

Given two nodes, n and m, they look something like this:

CREATE ({id: 1, color: ["red", "blue", "green", "yellow"]}) CREATE ({id: 2, color: ["red", "blue", "green", "white"]}) 

You can do something like this:

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

The FILTER function iterates through the n.color array, binding the current value to x (may be different by my own choice). The predicate ( x in m.color ) is checked for each value of x , and if it evaluates to true, this element is inserted into a new array returned by FILTER . You can leave this on this to see the intersection of two arrays (red, blue and green in this case) or wrap it in the length function to see the number of colors shared between two nodes (3 in this case).

Check out the full FILTER document here: http://docs.neo4j.org/chunked/milestone/query-functions-collection.html#functions-filter

+8
source

All Articles