Neo4j连接多个节点之间的多个关系 [英] Neo4j Connecting multiple relationships between multiple nodes

查看:645
本文介绍了Neo4j连接多个节点之间的多个关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现此处显示的内容: 我有2个CSV文件,diease_mstr和Test_mstr现在在Test_mstr中,我对疾病ID记录进行了很多测试,这意味着它们都不是唯一的.疾病ID指向disease_mstr文件.在disease_mstr文件中,我只有2个字段,即ID和Disease_name(疾病名称是唯一的).

I am trying to achieve what is shown here: I have 2 CSV Files, diease_mstr and Test_mstr Now in Test_mstr, I have many test to disease ID records, which means none of them are unique. The disease ID points to disease_mstr file. In disease_mstr file I have only 2 fields, ID and Disease_name (disease name is unique).

现在,我正在创建3个带有标签的节点 1)具有唯一测试(总共345个唯一测试名称)的测试(只有"testname"属性)

Now, I am creating 3 nodes with labels 1) Tests (only "testname" property) which will have unique tests (total 345 unique testnames)

**Properties :**
a) testname

2)Linknode(提取了整个Test_mstr文件)也从Disease_mstr文件中提取了对应的疾病ID的疾病名称"

2) Linknode (pulled entire Test_mstr file) also pulled "disease_name" for corresponding disease_ID from Disease_mstr File

**Properties**
a)tname
b)dname
c)did

3)疾病文件(从疾病文件mstr中提取).

3) Disease (pulled form disease_mstr) file.

**Properties**
a)did
b)diseasename

此后,我运行创建关系

1)MATCH (t:Tests),(n:Linknode) where t.testname = n.tname CREATE (n)-[r:TEST_2]->(t) RETURN n,r,t

2)MATCH (d:Disease), (l:Linknode) where d.did = l.did MERGE (d)-[r:FOR_DISEASE]->(l) RETURN d,r,l

要获得如图所示的预期结果,我运行以下cypher命令:

To get the desired result as shown in image, I run following cypher command :

MATCH (d:Disease)-[r2:FOR_DISEASE]->(l:Linknode)-[r:TEST_2]->(t:Tests) RETURN l,r,t,r2 LIMIT 25

有人可以帮我建立2个以上的关系,这些关系在图像中用蓝色和绿色线条标记和链接吗?

Can someone please help me create 2 more relationships which is marked and linked in image with BLUE and GREEN lines?.

可以在我的Google文件夹中访问示例文件和图像链接

推荐答案

您的目标是将所有疾病与测试联系起来,以便对于任何疾病都可以找到与哪些测试相关并且对于每种测试,针对哪种疾病进行测试?

Is your goal to link all diseases to tests so that for any disease you can find out which tests are relevant and for each test, which diseases it tests for?

如果是,那么您就快到了.

If so, you are nearly there.

除了在将测试链接到疾病的过程中帮助您之外,您不需要链接节点.在当前情况下,您将像创建关系数据库一样对待链接节点.他们不会在您的图形数据库中添加任何值.您可以在疾病和检查之间建立单一关系,以完成所有工作.

You don't need the link nodes other than to help you during linking the tests to the diseases. In your current scenario you're treating the link nodes as you would if you were creating a relational database. They won't add any value in your graph db. You can create a single relationship between diseases and tests which will do all the work.

这是逐步加载数据库的方法. (它可能不是最有效的,但是很容易遵循并且有效.)

Here's a step by step way to load your database. (It probably isn't the most efficient, but it's easy to follow and it works.)

标准化并加载测试:

load csv with headers from "file:///test_mstr_csv.csv" as line
merge (:Test {testname:line.test_name});

加重您的疾病(对我来说这些看起来很正常)

Load your diseases (these looked normalised to me)

load csv with headers from "file:///disease_mstr_csv.csv" as line
create (:Disease {did:line.did, diseasename:line.disease_name});

加载您的链接节点:

load csv with headers from "file:///test_mstr_csv.csv" as line
merge (:Link {testname:line.test_name, parentdiseaseid:line.parent_disease_ID});

现在,您可以使用以下查询在疾病和测试之间建立直接关系:

Now you can create a direct relationship between the diseases and tests with the following query:

match(d:Disease), (l:Link) where d.did = l.parentdiseaseid
with d, l.testname as name
match(t:Test {testname:name}) create (d)<-[:TEST_FOR]-(t);

最后一个查询将找到每种疾病的所有链接节点,并提取测试名称.然后,它会查找测试并将其直接加入相应的疾病中.

This last query will find all the link nodes for each disease and extract the test name. It then looks up the test and joins it directly to its corresponding disease.

链接节点现在是多余的,因此您可以根据需要删除它们.

The link nodes are redundent now, so you can delete them if you wish.

要创建蓝线"(我假设这是为了显示测试在哪些地方具有共同的疾病),请运行以下查询:

To create the 'blue lines', which I assume are meant to show where tests have diseases in common, run the query below:

match (d:Disease)<-[]-(:Test)-[]->(e:Disease) where id(d) > id(e) 
merge (d)-[:BLUE_LINE]->(e);

match 子句使用通用测试查找所有疾病对, where 子句确保仅在一个方向上创建链接,并且合并子句可确保仅创建一个链接.

The match clause finds all disease pairs with a common test, the where clause ensures a link is created in only one direction and the merge clause ensures only one link is created.

这篇关于Neo4j连接多个节点之间的多个关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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