简体   繁体   English

Neo4j-关系建模问题

[英]Neo4j - Relationship Modeling Issue

I have 2 CSV files exported from mysql : 我有2个从mysql导出的CSV文件:

# Disease CSV Headers # Disease Master ID (autoincrement, pk) disease_name #疾病CSV标头#疾病主ID(自动递增,pk)疾病名称

# Tests CSV Headers # Test Master ID (autoincrement, pk), test_name, parent_disease_ID (points to ID column in Disease. #测试CSV标头#测试主ID(自动递增,pk),test_name,parent_disease_ID(指向疾病中的ID列)。
Master tbl) 大师tbl)

I run following cypher commands : 我运行以下cypher命令:

    LOAD CSV WITH HEADERS FROM.   
   "http://localhost/disease_mstr.csv" AS line
    MERGE (d:Disease {did: toInteger(line.ID),  diseasename: 
    line.disease_name})

    LOAD CSV WITH HEADERS FROM.  
   "http://localhost/test_mstr.csv" AS line
    MERGE (d:Tests {tid: toInteger(line.ID),  testname: 
    line.test_name, did: toInteger(line.parent_disease_ID)})

   MATCH (t:Tests), (d:Disease) CREATE (t:Tests)- 
   [r:TEST_FOR]->(d:Disease) RETURN t, r, d

Above cypher returns one disease connected to many tests whereas i want just the reverse! 上面的密码返回一种与许多测试有关的疾病,而我只想相反! Can someone please correct me? 有人可以纠正我吗?

You could create the disease nodes, test nodes and relationship between test and disease nodes in one pass of the tests file. 您可以在一遍测试文件中创建疾病节点,测试节点以及测试和疾病节点之间的关系。

LOAD CSV WITH HEADERS 
FROM "http://localhost/test_mstr.csv" 
AS line
MERGE (disease:Disease {did: toInteger(line.parent_disease_ID)})
MERGE (test:Tests {tid: toInteger(line.ID), testname: 
line.test_name})
MERGE (test)-[r:TEST_FOR]->(did)

And then update the disease names after the fact in a second pass. 然后在第二遍事实之后更新疾病名称。

LOAD CSV WITH HEADERS 
FROM "http://localhost/disease_mstr.csv" AS line
MERGE (d:Disease {did: toInteger(line.ID)})
SET d.diseasename = line.disease_name

[EDITED] [EDITED]

In the query that creates the relationships, you need to filter for Tests and Disease nodes that share the same did value: 在创建关系查询,则需要筛选TestsDisease共享相同的节点did值:

MATCH (t:Tests), (d:Disease)
WHERE t.did = d.did
MERGE (t)-[r:TEST_FOR]->(d)
RETURN t, r, d;

This query also replaced CREATE with MERGE to avoid creating duplicate TEST_FOR relationships between the same t and d pair. 此查询还用MERGE替换了CREATE ,以避免在相同的td对之间创建重复的TEST_FOR关系。 If you already have such duplicate relationships, delete them first. 如果您已经具有此类重复关系,请首先将其删除。

Also, for efficiency, you should consider creating in index on either :Disease(did) or :Tests(did) -- whichever has the most instances. 同样,为了提高效率,您应该考虑在:Disease(did):Tests(did)上创建索引 -以实例最多的一个为准。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM