0

I have 2 CSV files exported from mysql :

# Disease CSV Headers # Disease Master ID (autoincrement, pk) disease_name

# Tests CSV Headers # Test Master ID (autoincrement, pk), test_name, parent_disease_ID (points to ID column in Disease.
Master tbl)

I run following cypher commands :

    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?

Dave Bennett
  • 10,996
  • 3
  • 30
  • 41
DaVinci007
  • 83
  • 12

2 Answers2

1

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
Dave Bennett
  • 10,996
  • 3
  • 30
  • 41
  • Your solution didnt work, please find dave1.jpg output in my google folder. It also has sample csv files. [link] https://drive.google.com/open?id=1ZrOW6WGiPZ66-r4nSgGO-GCcRK7HN1mO – DaVinci007 Jun 06 '18 at 05:32
  • Dave I managed to get 2 relationships : (1) Diseases->Tests (2) Test->Diseases, Now I am only missing to connect these two relationships further, for example "Blood gases" Test is used for 5 diseases in GRAPH1, in other graph I have "Acidosis and Alkalosis" which having 10 other tests in GRAPH2, Now I want to connect realtionships in either of the GRAPHS, how to go ahead? I have upload screenshots of both GRAPH VIEW and ROW VIEW in my google drive for you too look at. – DaVinci007 Jun 06 '18 at 11:18
0

[EDITED]

In the query that creates the relationships, you need to filter for Tests and Disease nodes that share the same did value:

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

cybersam
  • 63,203
  • 6
  • 53
  • 76
  • i already had tried with your solution and i am still getting 1 disease and many tests but i want vice versa, 1 test connected to 1 or more diseases in my test. Csv there are many repeated test names with diff parent_disease_id values for each of those! Can you please review? – DaVinci007 Jun 06 '18 at 02:36
  • I have updated my answer. If you still have issues, provide some sample CSV files that demonstrate the problem. – cybersam Jun 06 '18 at 02:43
  • Thanks for a quick reply, its again showing 1 disease many tests heres the link to images, i am trying to get hold of csv sample files until then heres the link for review https://drive.google.com/folderview?id=1ZrOW6WGiPZ66-r4nSgGO-GCcRK7HN1mO – DaVinci007 Jun 06 '18 at 03:12
  • Please link to my google drive folder with CSV files [link] (https://drive.google.com/drive/folders/1ZrOW6WGiPZ66-r4nSgGO-GCcRK7HN1mO?usp=sharing) – DaVinci007 Jun 06 '18 at 05:30
  • Cybersam, managed to get 2 relationships : (1) Diseases->Tests (2) Test->Diseases, Now I am only missing to connect these two relationships further, for example "Blood gases" Test is used for 5 diseases in GRAPH1, in other graph I have "Acidosis and Alkalosis" which having 10 other tests in GRAPH2, Now I want to connect realtionships in either of the GRAPHS, how to go ahead? I have upload screenshots of both GRAPH VIEW and ROW VIEW in my google drive for you too look at. – DaVinci007 Jun 06 '18 at 11:18
  • Please create a new question with your new data model (which is very different than in this question), your new query, and a clear description of what you are trying to do. – cybersam Jun 06 '18 at 16:45
  • As you said I posted with image and google drive link https://stackoverflow.com/q/50736680/8622150 – DaVinci007 Jun 07 '18 at 08:41