danny
danny

Reputation: 65

find the group in Neo4j graph db

I am using Neo4j for my project.

In my graph DB I have 2 types of nodes:

  1. person
  2. fruit

person nodes may be connected with each other with relation friend person nodes are connected with the nodes of fruits if they like that fruit.

I want to find the set of group of 3 people who likes for example apple peach and orange and there is at least the path from one person to 3rd with relation "friend", or all 3 are friends.

Since I have started using neo4j just recently, I need Guru's help to come up with solution.

My though is:

find out the group of people who likes apple find out the group of people who likes peach find out the group od people who likes orange

from that 3 sets find out the existing node-rel-node-rel... path which starts in one of that sets goes through 2nd set end ends up in 3rd.

Could you please confirm if my approach is correct and optimal and is it possible to implement via Cypher or py2neo?

Couldn't find the way to share, but can paste the query here. If you paste it back to console.neo4j.org you will get a graph:

CREATE (Neo { name:'Neo' }),(Morpheus { name: 'Morpheus' }),(Trinity { name: 'Trinity' }),(Cypher { name: 'Cypher' }),(Apple { fruit: 'Apple' }),(Peach { fruit: 'Peach' }),(Banana { fruit:'Banana' }), root-[:ROOT]->Neo, Neo-[:KNOWS]->Morpheus, Neo-[:KNOWS]->Trinity, Morpheus-[:KNOWS]->Cypher, Neo-[:LIKES]->Peach, Trinity-[:LIKES]-Banana, Morpheus-[:LIKES]-Apple

Assuming that you see the model in mentioned website. So here I need to search people who like (Peach, Banana, Apple) as a Result I want to get Neo, Trinity and Morpheus, because Neo likes peach, Trinity likes banana and Morpheus likes apple and they are connected somehow (Neo knows both Morpheus and Trinity, even tho Trinity doesn't know Morpheus).

There gonna be 100K people in my DB and everyone connected with some people and with fruits they like. I want to proceed described search and get all possible matchings like Neo, Morpheus and Trinity. Hope this description much more clear.

Upvotes: 0

Views: 506

Answers (1)

Lisa Li
Lisa Li

Reputation: 2592

Try and see if this query meets your requirements. it retrieves groups of 3 persons, each of them likes one of the kind of fruit with a given name, and they are connected each other by one or two relationships [:KNOWS]. Not sure it would scale well.

Match p1:Person-[:LIKES]->f1:Fruit, p2:Person-[:LIKES]->f2:Fruit, p3:Person-[:LIKES]->f3:Fruit, path1=p1-[:KNOWS*1..2]-p2, path2=p2-[:KNOWS*1..2]-p3,path3=p1-[:KNOWS*1..2]-p3
Where f1.name = 'Apple' and f2.name='Peach' and f3.name = 'Banana' and all(n in nodes(path1) where n in [p1,p2,p3]) and all(n in nodes(path2) where n in [p1,p2,p3]) and all(n in nodes(path3) where n in [p1,p2,p3])
Return p1.name, p2.name, p3.name

Note: I have added a label ":Fruit" to each fruit node, and a label ":Person" to each person node.

Here is the console for the graph and the query,

http://console.neo4j.org/?id=fswj2b

Upvotes: 1

Related Questions