Join result set

2019-03-04 08:09发布

I want mutual count of my friends with friendship status. I have created nodes for each user and created relationship with properties between them. I have found my desired result as per following queries. In this test case my login userid=1, and I want to search those users which is started from letter 'dh'. so, my query is as follows.

1st Query : which is returned all users with specific given keyword.
--------------------------------------------------------------------
START other=node(*)
WHERE other.FirstName =~ "(?i)dh.*" AND other.UserID <> 1 
WITH other, me=node:node_auto_index(UserID = '1')
RETURN other.UserID, other.UserName, other.FirstName, other.LastName, other.ImagePath 
LIMIT 100;

This query returns me all users started with 'dh' Now, I want friendship status between my login user and this searched users. so, I have done to this as follows:

2nd Query : which is returned approvalstatus between user1 and other
--------------------------------------------------------------------
START me=node:node_auto_index(UserID = '1')
MATCH me-[myR:friends]-(x)
RETURN x.UserID, myR.ApprovalStatus
ORDER BY x.UserID

and finally, I need mutual friend count between user 1 and others as per following query.

3rd Query : which is returned mutual count between user1 and other
------------------------------------------------------------------
START me=node:node_auto_index(UserID = '1'), other=node(*)
MATCH pMutualFriends=me-[r:friends]-mf-[r1:friends]-other
WHERE r.ApprovalStatus = 1
AND r1.ApprovalStatus = 1
AND other.FirstName =~ "(?i)dh.*" AND other.UserID <> 1 
RETURN other.UserID, COUNT(pMutualFriends) AS mutualCount
ORDER BY other.UserID

Now I want to join all of this query like we do in RDBMS. means result set 1st should return all records, join with 2nd & 3rd result set.

How do i do this?

1条回答
beautiful°
2楼-- · 2019-03-04 08:36

When you query a graph db, you should start with the piece of specific data that you know and work outward on the graph from there. Using START n=node(*) ... is very expensive: You are returning the entire list across the entire graph of users. That is not what you want, though, as you only want those that are connected to the user with UserID=1.

START me=node:node_auto_index(UserID = '1')
MATCH me-[r:FRIENDS]-friend-[r1:FRIENDS]-other
WHERE other.FirstName =~ "(?i)dh.*" AND other.UserID <> 1
    AND r.ApprovalStatus = 1 AND r1.ApprovalStatus = 1
    AND NOT (me-[:FRIENDS]-> other)
RETURN other.UserID, other.FirstName, COUNT(friend) AS MutualCount

This finds all friends of friends (other) who have a FirstName that starts with dh and counts the number of mutual friends they share with me.

I also added the clause AND NOT (me-[:FRIENDS]-> other) to remove the case of other also being a friend of me.

查看更多
登录 后发表回答