level-2 or level-3 connections when we have bidirectional relationship

别说谁变了你拦得住时间么 提交于 2019-12-12 03:26:42

问题


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


回答1:


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:


Does something along these lines return the nodes you expect?

MATCH path=(user1:User)-[knows:KNOWS*2..3]-(user2:User)
WHERE user1.name = 'User1'
AND user1 <> user2
RETURN user2, collect(length(path)) as downlevels, min(length(path)) as min_length
ORDER BY min_length



回答3:


This query might suit your needs:

MATCH path=(user1:User)-[knows:KNOWS*2..3]->(user2:User) 
WHERE user1.mobile = 9000090001 AND user1 <> user2 
RETURN user1, COLLECT(DISTINCT user2), length(path) as downlevel 
ORDER BY downlevel;

It returns a row for each downlevel, and each row has a collection of the distinct nodes at that level. It also omits user1 from the collections.



来源:https://stackoverflow.com/questions/36596002/level-2-or-level-3-connections-when-we-have-bidirectional-relationship

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!