在Neo4j图形数据库中找到组 [英] find the group in Neo4j graph db

查看:314
本文介绍了在Neo4j图形数据库中找到组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的项目使用Neo4j.

I am using Neo4j for my project.

在我的图形数据库中,我有两种类型的节点:

In my graph DB I have 2 types of nodes:

  1. 水果

人节点可以与关系朋友相互连接 如果人的节点喜欢水果,则它们与水果的节点相连.

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.

我想找到一组由3个人组成的组,例如喜欢苹果桃子和橙子,并且从一个人到具有朋友"关系的第三人之间至少有一条路,或者所有3个人都是朋友.

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.

自从我最近才开始使用neo4j以来,我需要Guru的帮助来提出解决方案.

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

我虽然是:

找出喜欢苹果的人 找出一群喜欢桃子的人 找出喜欢橙色的人群

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

从这3个集合中找出现有的node-rel-node-rel ...路径,该路径从其中一个集合开始经过第2个集合,最终在第3个集合中结束.

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.

请您确认我的方法是否正确,最优,是否可以通过Cypher或py2neo实施?

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

找不到共享的方式,但可以在此处粘贴查询. 如果将其粘贴回console.neo4j.org,则会得到一个图形:

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

假设您在提到的网站中看到该模型.因此,在这里我需要搜索喜欢(桃子,香蕉,苹果)的人作为结果,我想获得Neo,Trinity和Morpheus,因为Neo喜欢桃子,Trinity喜欢香蕉,Morpheus喜欢苹果,并且它们之间有某种联系(Neo知道Morpheus和Trinity,甚至Trinity都不认识Morpheus.)

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).

我的数据库中将有10万人,每个人都与某些人和他们喜欢的水果联系在一起.我想进行描述性搜索,并获得所有可能的匹配,例如Neo,Morpheus和Trinity.希望这个描述更加清楚.

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.

推荐答案

尝试查看此查询是否满足您的要求.它检索由3个人组成的组,每个人都喜欢一种具有给定名称的水果,并且它们之间通过一两个关系[:KNOWS]相互连接.不确定是否可以很好地扩展.

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

注意:我已经在每个水果节点上添加了标签:Fruit",并在每个人节点上添加了标签:Person".

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

这篇关于在Neo4j图形数据库中找到组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆