将节点与具有关系的公共节点匹配 - Neo4j Cypher [英] Match nodes with common nodes with a relationship - Neo4j Cypher

查看:21
本文介绍了将节点与具有关系的公共节点匹配 - Neo4j Cypher的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有许多用户节点和技能节点.关系是技能和用户之间的关系.

使用定期提交 1000从文件:/xyz.csv"中加载带有标题的 CSV 作为行 FIELDTERMINATOR '|'带行限制 15创建(u:用户{公司:row.company,薪水:row.salary_float,指定:row.designation,经验:row.experience_float})FOREACH (s IN split(row.tag_skill, "@") |合并(技能:技能{名称:s})ON CREATE SET Skill.name = sCREATE (u)-[:KNOWS]->(skill))

我还需要用户节点之间的关系,其中如果用户 A 连接到技能节点的编号 [s1,s2,s3,s4,s5,s6]如果用户 B 连接到 [s1,s3,s4,s6]

如果用户 A 和用户 B 至少 50% 的技能匹配,则用户 A 和用户 B 处于关系中(相似).

在这个例子中,A 与 B 有关系,因为它们有 s1,s3,s4,s6 的共同点,匹配度超过 50%

似乎无法弄清楚这个密码查询.

解决方案

这是我对 类似问题:

MATCH (u1:User)-[:KNOWS]->(:Skill)<-[:KNOWS]-(u2:User)//(1)比赛(u1)-[:KNOWS]->(s1:Skill),(u2)-[:KNOWS]->(s2:Skill)//(2)和u1, u2,COUNT(DISTINCT s1) AS s1Count, COUNT(DISTINCT s2) AS s2Count//(3)MATCH (u1)-[:KNOWS]->(s:Skill)<-[:KNOWS]-(u2)//(4)和u1, u2,s1Count, s2Count, COUNT(s) AS commonSkillsCount//(5)在哪里//我们只需要每个 u1-u2 对一次ID(u1)<ID(u2) AND//(6)//相似度commonSkillsCount/0.5 >= s1Count ANDcommonSkillsCount/0.5 >= s2Count//(7)返回 u1, u2按 u1.name, u2.name 排序

我们寻找u1u2 至少有一个共同技能(1) 的用户.然后我们分别收集他们的个人技能(2)并计算他们(3),并收集他们的共同技能(4)并计算他们(5).然后我们删除 (u1, u2)(u2, u1) 对之一(例如从 (Alice, Bob)(Bob, Alice) 我们只保留前 (6) 并检查他们的共同技能的数量是否高于阈值 (7).(浮点运算有时在 Cypher - IIRC 中很棘手,如果我们移动/0.5 到不等式右侧的 sXCount * 0.5,我们必须使用 toFloat() 函数).

I have number of User nodes and Skills nodes. The relationship is between skills and users.

USING PERIODIC COMMIT 1000
LOAD CSV WITH HEADERS FROM "file:/xyz.csv" AS row FIELDTERMINATOR '|'
WITH row
LIMIT 15

CREATE (u:User {company: row.company, salary: row.salary_float,         designation: row.designation, experience: row.experience_float})

FOREACH (s IN split(row.tag_skill, "@") |
MERGE (skill:SKILL {name: s})
ON CREATE SET skill.name = s
CREATE (u)-[:KNOWS]->(skill))

I also need a relationship between user nodes where if User A is connected to a number for Skill nodes [s1,s2,s3,s4,s5,s6] and if User B is connected to [s1,s3,s4,s6]

User A and User B are in relationship(similar) if atleast 50% of their skills match.

in this example, A is in a relationship with B since they have s1,s3,s4,s6 in common which is more than 50% match

Cant seem to figure out this cypher query.

解决方案

Here is an adaptation of my answer for a similar question:

MATCH (u1:User)-[:KNOWS]->(:Skill)<-[:KNOWS]-(u2:User) // (1)
MATCH
  (u1)-[:KNOWS]->(s1:Skill),
  (u2)-[:KNOWS]->(s2:Skill) // (2)
WITH
  u1, u2, 
  COUNT(DISTINCT s1) AS s1Count, COUNT(DISTINCT s2) AS s2Count // (3)
MATCH (u1)-[:KNOWS]->(s:Skill)<-[:KNOWS]-(u2) // (4)
WITH
  u1, u2,
  s1Count, s2Count, COUNT(s) AS commonSkillsCount // (5)
WHERE
  // we only need each u1-u2 pair once
  ID(u1) < ID(u2) AND // (6)
  // similarity
  commonSkillsCount / 0.5 >= s1Count AND
  commonSkillsCount / 0.5 >= s2Count // (7)
RETURN u1, u2
ORDER BY u1.name, u2.name

We look for u1, u2 Users that have at least a single common Skill (1). We then gather their individual skills separately (2) and count them (3), and also gather their mutual Skills (4) and count them (5). We then remove one of the (u1, u2) and (u2, u1) pairs (e.g. from (Alice, Bob) and (Bob, Alice) we only keep the former (6) and check if the number of their common skills is above the threshold (7). (Floating point arithmetics is sometimes tricky in Cypher - IIRC if we move the / 0.5 to the right side of the inequality as sXCount * 0.5, we'd have to use the toFloat() function).

这篇关于将节点与具有关系的公共节点匹配 - Neo4j Cypher的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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