Getting friends of friend are pretty easy, I got this which seems to work great.
g.v(1).in('FRIEND').in('FRIEND').filter{it != g.v(1)}
But what I want to do is only get friends of friends that have the same interests. Below I want Joe to be suggested Moe but not Noe because they do not have the same interest.
You simply need to extend your gremlin traversal to go over the LIKES edges too:
g.v(1).in('FRIEND').in('FRIEND').filter{it != g.v(1)}.dedup() \
as('friend').in('LIKES').out('LIKES').filter{it == g.v(1)}. \
back('friend').dedup()
Basically this goes out to friends of friends, as you had before and saves the position in the pipe under the name friend. It then goes out to mutual likes and searches for the original
source node. If it finds one it jumps back friend. The dedup() just removes duplicates and may speed up traversals.
The directionality of this may not be 100% correct as you haven't indicated direction of edges in your diagram.
Does this have to be in Gremlin? If Cypher is acceptable, you can do:
START s=node(Joe)
MATCH s-[:FRIEND]-()-[:FRIEND]-fof, s-[:LIKES]-()-[:LIKES]-fof
WHERE s != fof
RETURN fof
Query to get Mutual friends without considering common likes,
But if you they have common likes it will come on top.
Take a look of Order by.
MATCH (me:User{userid:'34219'})
MATCH (me)-[:FRIEND]-()-[:FRIEND]-(potentialFriend)
WITH me, potentialFriend, COUNT(*) AS friendsInCommon
WITH me,
potentialFriend,
SIZE((potentialFriend)-[:LIKES]->()<-[:LIKES]-(me)) AS sameInterest,
friendsInCommon
WHERE NOT (me)-[:FRIEND]-(potentialFriend)
RETURN potentialFriend, sameInterest, friendsInCommon,
friendsInCommon + sameInterest AS score
ORDER BY score DESC;
If you want only common likes add foll. condition -
Where sameInterest>0
Related
Good morning,
I want to build a structure in Neo4J where I can handle my users and groups (kind of ACL). The idea is to have for each user and for each group a node with all the details. The groups shall become a graph where a root group will have sub-groups that can have also sub-groups without limit. The relation will be -[:IS_SUBGROUP_OF]- - so far nothing exciting. Every user will be related to a group with -[:IS_MEMBER_OF]- to have a clear assignment. Of course a user can be a member of 1 or more groups. Some users will have a different relation like -[:IS_LEADER_OF]- to identify teamlead of the groups.
My tasks:
Assignment: I can query each member of a group with a simple query, I can even query members of the subgroups using the current logged in and asking user:
MATCH (d1:Group:Local) -- (c:User)
MATCH (d:User) -[:IS_MEMBER_OF|IS_LEADER_OF]- (g:Group:Local)-[:IS_SUBGROUP_OF*0..]->(d1)
WHERE c.login = userLogin
RETURN DISTINCT d.lastname, d.firstname
I get every related user to every group of the current user and below (subgroups). Maybe you have a hint how I cna improve the query or the model.
Approval
Here I am stucked as I want to have all users of the current group from the querying user and all members of all subgroups - except the leader of the current group. The reason behind is that a teamlead shall not be able to approve actions for himself but though for every other member of his group and all members of subgroups including their teamleads.
I tried to use the relations -[:IS_LEADER_OF]- to exclude them but than I loose also the teamleads of the subgroups. Does anyone has an idea how I would either change the model or how I can query the graph to get all users except the teamlead of the current group?
Thanks for your time,
Balael
* EDIT *
I think I am getting close, I just need to understand the results of those both queries:
MATCH (d:User) -- (g:Group) WHERE g.uuid = "xx"
RETURN d.lastname, d.firstname
Returns all user in this group no matter what relationship (leader / member)
MATCH (d:User) -- (g:Group), (g)--(c:User{uuid:"yy"})
RETURN d.lastname, d.firstname
Returns all user of that group except the user c. I would have expected to get c as well in the list with d-users as c is part of that group and should be found with (d:User).
I do not understand the difference between both queries, maybe someone has a hint for me?
You can simplify your query slightly (however this should not have an impact on performance):
MATCH (d:User) -[:IS_MEMBER_OF|IS_LEADER_OF]- (g:Group:Local)-[:IS_SUBGROUP_OF*0..]->(d1:Group:Local)--(c:User{login:"userlogin"})
RETURN DISTINCT d.lastname, d.firstname
Don't completely understand your question, but I assume you want to make sure that d1 and c are not connected by a IS_LEADER_OF relationship. If so, try:
MATCH (d:User) -[:IS_MEMBER_OF|IS_LEADER_OF]- (g:Group:Local)-[:IS_SUBGROUP_OF*0..]->(d1:Group:Local)-[r]-(c:User{login:"userlogin"})
WHERE type(r)<>'IS_LEADER_OF'
RETURN DISTINCT d.lastname, d.firstname
following up on * EDIT * in the question
In a MATCH you specify a path. By definition a path does not use the same relationship twice. Otherwise there is a danger to run into infinite recursion. Looking at the second query in the "EDIT" section above: the right part matches yy's relationship to the group whereas the left part matches all user related to this group. To prevent multiple usage of the same relationship the left part does not hit use yy
I have a possibly bone-headed question, but I'm just starting out with Neo4j, and I hope someone can help me out with learning Cypher syntax, which I've just started learning and evaluating.
I have two User nodes, and a single NewsPost node. Both users LIKE the NewsPost. I'm able to construct a Cypher query to count the likes for the post, but I'm wondering if it's also possible to check if the current user has liked the post in the same query.
What I have so far for a Cypher query is
match (p:NewsPost)<-[r:LIKES]-(u:User)
where id(p) = 1
return p, count(*)
Which returns the post and like count, but I can't figure out the other part of "has the current user liked this post". I know you're not supposed to filter on <id>, but I learned that after the fact and I'll go back and fix it later.
So first, is it possible to answer the "has the current user liked this post" question in the same query? And if so, how do I modify my query to do that?
The smallest change to your query that adds a true/false test for a particular user liking the news post would be
MATCH (p:NewsPost)<-[r:LIKES]-(u:User)
WHERE ID(p) = 1
RETURN p, count(r), 0 < size(p<-[:LIKES]-(:User {email:"michael#nero.com"}))
This returns, in addition to your query, the comparison of 0 being less than the size of the path from the news post node via an incoming likes relationship to a user node with email address michael#nero.com. If there is no such path you get false, if there is one or more such paths you get true.
If that does what you want you can go ahead and change the query a little, for instance use RETURN ... AS ... to get nicer result identifiers, and so on.
What you are looking for is Case.
In your database you should have something unique for each user (id property, email or maybe login, I don't know), so you have to match this user, and then match the relation to the post you want, using case you can return a boolean.
Example:
Optional Match (u:User{login:"Michael"})-[r:LIKES]-(p:newPost{id:1})
return CASE WHEN r IS NULL THEN false ELSE true END as userLikesTopic
If you want to get the relation directly (to get a property in it as example) you can remove the CASE part and directly return r, if it does not exist, null will be returned from the query.
I have a graph which looks like this:
User->Friend->Area
| (Which area does this friend live in)
------------->Area<-Friend<-Other users
(Which area does this user live in)
If i like to find all areas where i dont have friends, i do this
start user=node(reference)
match user-->friend-->area<-[r?:HAVE_A]-friend<--user
where r is null
return area
Works great!
But how to order the results so i get the area which have the most users first?
If I'm understanding your question correctly, you are getting the correct areas returned but want to organize them by the count of users.
In that case you need a WITH clause to get the users, COUNT them, and an ORDER BY to sort them:
START user=node(reference)
MATCH user-->friend-->area<-[r?:HAVE_A]-friend<--user
WHERE r is null
WITH area
MATCH area<--users
RETURN area, COUNT(users) as cnt
ORDER BY cnt DESC
Does it possible to have an order by "property" with a where clause and now the "index/position" of the result?
I mean, when using order for sorting we need to be able to know the position of the result in the sort.
Imagine a scoreboard with 1 million user node, i do an order by on user node.score with a where "name = user_name" and i wan't to know the current rank of the user. I do not find how to do this using order by ...
start game=node(1)
match game-[:has_child_user]->user
with user
order by user.score
with user
where user.name = "my_user"
return user , "the position in the sort";
the expected result would be :
node_user | rank
(i don't want to fetch one million entries at client side to know the current rank/position of a node in the ORDER BY!)
This functionality does not exist today in Cypher. Do you have an example of what this would look like in SQL? Would the below be something that fits the bill? (just a sketch, not working!)
(your code)
start game=node(1)
match game-[:has_child_user]->user
with user
order by user.score
(+ this code)
with user, index() as rank
return user.name, rank;
If you have more thoughts or want to start hacking on this please open an issue at https://github.com/neo4j/neo4j/issues
For the time being there is a work around that you can do:
start n=node(0),rank_node=node(1)
match n-[r:rank]->rn
where rn.score <= rank_node.score
return rank_node,count(*) as pos;
For live example see: http://console.neo4j.org/?id=bela20
Let's say I've pulled down the Twitter graph local to myself into Neo4J. I want to find people who follow my friends in number larger that should be expected. More specifically, I want to find people who follow the people I follow, but I want the results to be sorted so that the person following the highest number of my friends is sorted first. Possible in Cypher?
Here's a console example:
http://console.neo4j.org/r/p36cgj
create (me {n:"a"}), (fo1 {n:"fo1"}), (fo2 {n:"fo2"}), (fo3 {n:"fo3"}), (fr1 {n:"fr1"}),
(fr2 {n:"fr2"}), (fr3 {n:"fr3"}),
fo1-[:follows]->me, fo2-[:follows]->me, fo3-[:follows]->me, me-[:follows]->fr1,
me-[:follows]->fr2, me-[:follows]->fr3, fo1-[:follows]->fr1, fo2-[:follows]->fr2,
fo1-[:follows]->fr2, fo1-[:follows]->fr3;
start me=node:node_auto_index(n="me")
match me-[:follows]->friends<-[:follows]-follower-[:follows]->me
return follower, count(friends) as creepinessFactor, length(me-[:follows]->()) as countIFollow
order by creepinessFactor desc;
I'm curious to hear the results, btw. :P
You could also throw in a where like:
where not(me-[:follows]->follower)
To avoid getting friends within your circle.