1

I am working on an application which uses neo4j. I have imported my Data Base in neo4j using CSV file and created the nodes and relations. My Database contains 3 columns, Anumber,Bnumber and DateTime. This DB represents the calling history from ANUMBER -> BNUMBER with DATETIME

So I have created the nodes and relations by using these commands on neo4j Browser

LOAD CSV WITH HEADERS FROM "file:///2.csv" AS ROW
CREATE (a:ANUMBER {aNumber:ROW.aNumber} )
CREATE (b:BNUMBER {bNumber:ROW.bNumber} )
MERGE (a)-[:CALLED]->(b);

This successfully created two nodes, one relation and 2 properties keys.

Now I am facing two issues.

First one is When I query data related to single node It creates new circle for every occurrence. I have used this query

MATCH p=(a:ANUMBER)-[r:CALLED]->(b:BNUMBER)
WHERE a.aNumber = "3217913664"
RETURN p 

This results the correct response BUT it created new circle for every relation between 3217913664 and any other number. Please see the attached Image. I need to show one circle for 3217913664 and all of the edges goes out from single circle and created the required circle and edges. This image shows 23 nodes for 3217913664 BUT I only need to show one node for 3217913664 and other node as expected. enter image description here

Second one is I need to show the all shortest path between two numbers. Lets say

123 called 456,789,000,222
000 called 333,555,999
999 called 963
456 called 963
123 called 963

So I need to find the all shortest path between 123 and 963, which are:

123 -> 000 -> 999 -> 963
123 -> 456 -> 963
123 -> 963

Any idea how can I solve these 2 problems. I have already implemented these scenarios using GraphStream BUT I need to implement the same use cases in neo4j using Cypher Query Language

Noob Player
  • 279
  • 6
  • 25

1 Answers1

1

I believe that your first issue is related to the import script. You are creating the a number and b number for each line in your CSV. Probably this is resulting the 23 nodes with number 3217913664. To solve this replace the CREATE statements by MERGE, like this:

LOAD CSV WITH HEADERS FROM "file:///2.csv" AS ROW
MERGE (a:ANUMBER {aNumber:ROW.aNumber} )
MERGE (b:BNUMBER {bNumber:ROW.bNumber} )
MERGE (a)-[:CALLED]->(b);

About the second issue: Probably you are looking for the allShortestPath function.

Bruno Peres
  • 15,845
  • 5
  • 53
  • 89
  • Yes, I want `allShortestPath`. I tried this BUT it gives no result may be due to my wrong import command. I will try your commands and see what happens for allShortestPath – Noob Player Aug 02 '17 at 11:47
  • I have run this query for shortest path `MATCH (a:ANUMBER {aNumber: "3217913664"}), (b:BNUMBER {bNumber: "3212862749"}), path = shortestpath((a)-[:CALLED*]-(b)) RETURN path` – Noob Player Aug 02 '17 at 12:43
  • BUT it gives me the **cartesian product warning** on first line and **The provided pattern is unbounded** on second line. Any idea how can I resolve this warning. The result is good and accurate but facong these 2 warnings – Noob Player Aug 02 '17 at 12:44
  • 1
    You can solve the first warning taking a look in this [SO question](https://stackoverflow.com/questions/33352673/why-does-neo4j-warn-this-query-builds-a-cartesian-product-between-disconnected). Probably a query like `MATCH (a:ANUMBER {aNumber: "3217913664"}) WITH a MATCH (b:BNUMBER {bNumber: "3212862749"}) WITH a, b MATCH path = shortestpath((a)-[:CALLED*]-(b)) RETURN path` will solve the first warning. – Bruno Peres Aug 02 '17 at 13:41
  • 1
    @BilalZafar, to solve the second warning you should add an upper limit to the number of node hops of your pattern. That is: the pattern `(a)-[:CALLED*]-(b)` should be something like `(a)-[:CALLED*..5]-(b)` limiting to 5 hops. – Bruno Peres Aug 02 '17 at 13:57