双向关系时的2级或3级连接 [英] level-2 or level-3 connections when we have bidirectional relationship

查看:78
本文介绍了双向关系时的2级或3级连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对此问题的扩展 Neo4j-获取Level2或Level3连接

我在Neo4j中有以下关系.很少有节点具有双向关系

我想获取给定用户的2级或3级连接.

我有以下CQL

START levelGraph=node(1)
MATCH path=(user1:User)-[knows:KNOWS*2..2]->(user2:User)
WHERE user1.mobile = 9000090001
RETURN user1, user2, length(path) as downlevel
ORDER BY length(path) asc

这给了我所有与User1有关系的节点,甚至在输出中也给了User1

我想从给定用户那里获得所有唯一的2级或3级连接

对于User1,各个级别的连接如下:

Level-1 => User2, User3, User4, User5, User6
Level-2 => User7, User8, User9, User10, User11, User12, User13, User14

因此,当我查询获取Level2连接时,我应该仅将这些user7分别获取到User14

解决方案

这是我仅需要获取二级连接的密码查询的内容:

CQL-1:

MATCH path=(user1:User)-[:KNOWS]->(:User)-[:KNOWS]->(user2:User) 
WHERE user1.mobile = 9000090001 AND user1 <> user2 AND NOT (user1)-[:KNOWS]->(user2)
RETURN distinct user2
ORDER BY user2.mobile

CQL-2

MATCH path=(user1:User)-[knows:KNOWS*2..2]->(user2:User)
WHERE user1.mobile = 9000090001 AND user1 <> user2 AND NOT (user1)-[:KNOWS]->(user2)
RETURN distinct user2
ORDER BY user2.mobile

Extention to this question Neo4j - Get Level2 or Level3 connections

I have the following relationship in Neo4j. Few nodes have bidirectional relationsips

I want to fetch the level-2 or level-3 connections for the given user.

I have the following CQL

START levelGraph=node(1)
MATCH path=(user1:User)-[knows:KNOWS*2..2]->(user2:User)
WHERE user1.mobile = 9000090001
RETURN user1, user2, length(path) as downlevel
ORDER BY length(path) asc

this one is giving me all the nodes who has relationship with User1 and its giving even User1 in output

I want to get all the unique level-2 or level-3 connection from the given user

EDIT:

For User1 the connections at individual levels are as follows:

Level-1 => User2, User3, User4, User5, User6
Level-2 => User7, User8, User9, User10, User11, User12, User13, User14

So when I query to get Level2 connections I should get only these user7 to User14 distinctly

解决方案

This is what the cypher query I have to fetch only level two connections:

CQL-1:

MATCH path=(user1:User)-[:KNOWS]->(:User)-[:KNOWS]->(user2:User) 
WHERE user1.mobile = 9000090001 AND user1 <> user2 AND NOT (user1)-[:KNOWS]->(user2)
RETURN distinct user2
ORDER BY user2.mobile

CQL-2

MATCH path=(user1:User)-[knows:KNOWS*2..2]->(user2:User)
WHERE user1.mobile = 9000090001 AND user1 <> user2 AND NOT (user1)-[:KNOWS]->(user2)
RETURN distinct user2
ORDER BY user2.mobile

这篇关于双向关系时的2级或3级连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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