Assume that I am implementing a user friend relationship on Neo4j. I am using spring-neo4j for modeling.
Users are related to each other as friend. We can take a users friends and this is first level relationship. Is there a way to take friends friends which is second level relationship and third level relationship in Neo4J, especially spring-neo4j?.
Thank You!
Edited:
Could relation level be given dynamically? I have a piece that find every one on all levels.
MATCH u1-[:friend*]->(u2)
WHERE u1.userId IN ["userId1"]
RETURN u2
My edited question is that can I just give a relation level and get the result. For example, I am giving 2 and trying to get 2nd level friends. If I gave 3 it should return 3th level friends. Could this be dynamically give?
Not exactly sure what is the specific question here but this is how you could do that using spring-data-neo4j
#NodeEntity
public class User
{
#RelatedTo(type="FRIEND")
Set<User> friends;
private Iterable<Person> friends;
#Query(value = "start n=node({self}) match (u)-[r]->(friend)-[r]->(friendOfFriend) where r.type = 'FRIEND' return friendOfFriend")
Iterable<User> friendsOfFriends;
}
Anwser of edited part of my question is ;
MATCH u1-[:friend*relationLevel]->(u2)
WHERE u1.userId IN ["userId1"]
RETURN u2
relationLevel is the level that I want. If it is 2, result will be only for 2nd level relation.
And it is probably in spring,
#Query("start user1=node({0}) match (user1)-[r:friend*{1}]->(user2) where has(user2.userId) return user2.userId")
Iterable<String> getRelatedUserIdsByRelationLevel(GraphUser graphUser,int relationLevel);
Related
Example Realm relationship:
People.Dogs.FavouriteFoods
that are strictly one way -> RLMArrays
I have:
let result = RLMResult<People> from a previous operation.
and I have an array of FavouriteFood.IDs that a user selected
let selectedIDs: [String]
Now I am trying to filter/predicate this result, but instead of returning People, which I already have, I am trying to get out the FavouriteFood objects that intersect with the selectedIDs I can only find guides that explain how to sort/filter on RLMResults<People> where the result is People i.e. the same as the generic type on RLMResult.
My goal is to, in the end, construct a list where I can say "Out of the 14 FavouriteFoods Person A's Dogs have, 7 of them are in the selectedIDs list" etc. for Person B, C, D...
I want something like: "ANY dogs.favouriteFoods.ID in selectedIDs" but it should return all the FavouriteFoods matching the predicate for an individual Person, instead of all the People having Dogs having these particular favouriteFoods.
Is this possible to do as a predicate? Is there a way to flip the concept to ask for FavouriteFoods instead, or must I loop over all people, dogs, favouriteFoods and manually tally this up?
Thanks for any help given.
I have been trying to use neography for the following basic use case, but can't seem to get it to work:
For a given node, tell me all the associated relationships for that node.
For a given node and a specific relationship, return the node or nodes in that relationships?
I followed the examples from here: https://maxdemarzi.com/2012/01/04/getting-started-with-ruby-and-neo4j/
I tried the following code:
def create_person(name)
Neography::Node.create("name" => name)
end
johnathan = create_person('Johnathan')
mark = create_person('Mark')
phil = create_person('Phil')
mary = create_person('Mary')
luke = create_person('Luke')
johnathan.both(:friends) << mark
First, I want to see the associated relationships that are incoming. My expectation is to see relationship with type :friends:
johnathan.incoming
=> #<Neography::NodeTraverser:0x0000000133f1c0 #from=#<Neography::Node name="Johnathan">, #order="depth first", #uniqueness="none", #relationships=[{"type"=>"", "direction"=>"in"}]>
I tried relationships:
2.2.1 :060 > johnathan.incoming.relationships
=> [{"type"=>"", "direction"=>"in"}]
My expectation would be to see "type"=>":friends" but I don't.
However, when I try the following, I do, but it doesn't work for my use case since I want to know what the relationships are without knowing in advance what they are:
2.2.1 :061 > johnathan.incoming(:friends).relationships
=> [{"type"=>"friends", "direction"=>"in"}]
Second use case is to actually retrieve the nodes, which does work.
Question:
How can I get the types of relationships associated for any given node?
I think I am close to figuring it out:
johnathan.rels.map{|n| n}.first.rel_type
=> "friends"
You are right, almost there. The documentation for this is at the bottom of https://github.com/maxdemarzi/neography/wiki/Phase-2-Node-relationships#retrieval-by-type but basically:
n1 = johnathan
n1.rels # Get node relationships
n1.rels(:friends) # Get friends relationships
n1.rels(:friends).outgoing # Get outgoing friends relationships
n1.rels(:friends).incoming # Get incoming friends relationships
n1.rels(:friends, :work) # Get friends and work relationships
n1.rels(:friends, :work).outgoing # Get outgoing friends and work relationships
There is no way to get just what are all the relationship types connected to me as far as I know, but that would be a good improvement in the Neo4j REST API.
The functionality exists in the Java API, see https://neo4j.com/docs/java-reference/current/javadocs/org/neo4j/graphdb/Node.html#getRelationshipTypes--
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.
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