使用 cypher/neo4j 比较数组值 [英] Comparing array values in with cypher / neo4j

查看:77
本文介绍了使用 cypher/neo4j 比较数组值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张会员和他们查看过的项目的图表.

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?

推荐答案

给定两个节点,n 和 m,看起来像:

Given two nodes, n and m, that look something like:

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

你可以这样做:

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

FILTER 函数遍历 n.color 数组,将当前值绑定到 x(我任意选择,可能不同).为每个 x 值检查谓词(x in m.color),如果它的值为真,则将该元素推入 FILTER 的新数组中 返回.您可以将其保留在那里以查看两个数组(在本例中为红色、蓝色和绿色)的交集,或者将其包装在 length 函数中以查看两者之间共享的颜色数量节点(本例中为 3 个).

The FILTER function iterates through the n.color array, binding the current value to x(arbitrarily chosen by me, could be different). The predicate (x in m.color) is checked for each x value, and if it evaluates to true, that element is pushed into the new array that FILTER returns. You can leave it at that to see the intersection of the two arrays (red, blue, and green in this case), or wrap it in the length function to see the number of colors shared between the two nodes (3 in this case).

在此处查看完整的 FILTER 文档:http://docs.neo4j.org/chunked/milestone/query-functions-collection.html#functions-filter

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

这篇关于使用 cypher/neo4j 比较数组值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆