cancel
Showing results for 
Search instead for 
Did you mean: 

Ordering nodes by property

mfyfe
Node Clone

I am trying to translate a process map made in Visio, into nodes mapped in Neo4j. The nodes are all of the same type, but the property for process number varies.

So far I have this:

LOAD CSV WITH HEADERS FROM "file:/Book111.csv" AS line
CREATE (p:Stage {name: line.MasterName, id: line.ProcessNumber})
RETURN p

Which gives me this:

But, I can't seem to figure out how to create a relationship between the nodes according to the numeric property for process number.

Any help would be much appreciated

1 ACCEPTED SOLUTION

Thanks to you both for your help @ameyasoft & @clem. It seems like there are some pretty big problems with newer versions of Neo4j on windows 10, so i just went back to 1.2.8.

I managed to get the query working with a couple of small adjustments:

LOAD CSV WITH HEADERS FROM "file:/Book1.csv" AS line
CREATE (p:Person {id: line.ProcessNumber, name: line.MasterName})
WITH p order by p.id ASC
WITH collect(p) as p1
CALL apoc.nodes.link(p1, 'NEXT')
RETURN p1

View solution in original post

25 REPLIES 25

ameyasoft
Graph Maven
MATCH (p:Stage)
WITH p order by p.id
RETURN p

Thanks Ameyasoft. That seems to give me a whole load more nodes, but still no relationship between the nodes.

I get that I have more nodes because i've double up the query, but I need the relationship links between the nodes. Surely one component should be to define the relationship itself?

From your LOAD CSV, I didn't see any relationships. I see only node creation.

If you have any relationships between the nodes, then use this:

MATCH (p:Stage)
WITH p order by p.id
MATCH (p)-[:ABC]-(a)
RETURN p, a

Ah, well perhaps what i'm trying to do simply isn't possible then. I only have one node type. I want to create relationships between the same node type dependant on a unique numeric value property "ProcessNumber". So if "ProcessNumber has the value "1", it might have a relationship "ComesBefore" p with value "2" for "ProcessNumber". (p"1")-[:ABC]->(p"2")

If you want to link all your existing nodes, you can use apoc.nodes.link. Here's the Cypher to do that.

MATCH (p:Stage)
WITH p order by p.id ASC
WITH collect(p) as p1
CALL apoc.nodes.link(p1, 'NEXT')
RETURN p

This will connect all the selected nodes with 'NEXT' relationship and visually you see a train of nodes.

mfyfe
Node Clone

Aha, so this is exactly what i'm trying to do - order by p.id. But, when I try to put the lines in the order you have it:

LOAD CSV WITH HEADERS FROM "file:/Book111.csv" AS line
CREATE (p:Stage {name: line.MasterName, id: line.ProcessNumber})
MATCH (p:Stage)
WITH p order by p.id ASC
WITH collect(p) as p1
CALL apoc.nodes.link(p1, 'NEXT')
RETURN p

It tells me:

And if i try to put the WITH's before match it tells me:

Let me know about your Neo4j version and is community/desktop/enterprise?

I have normal desktop

Sorry I meant Neo4j Desktop version

mfyfe
Node Clone

Yes, it's the desktop Neo4j that i have

Did you install APOC?

No, i haven't, should I add it in? I haven't added any extensions so far

I have Neo4j 1.2.8, would [apoc-4.3.0-rc01-all.jar] be the right one to get?

When you start Neo4j Desktop you will see your a box with the database name you created. On this box there will be three circles at right top corner. Click on it and select Manage from the drop down list. Next you will see another window and on that click on Plugins tab. In that tab you will see APOC and click on install button. That does the job. Restart you database and play.

Very nice . The only thing now is that it's making one relationship for every node, between each node (there are 17 nodes and between each node there are 17 relationships')

clem
Graph Steward

You have to get the APOC to match the version of Neo4J. Neo4J 1.2.8 is positively ancient. You should upgrade to the latest 4.2.1.

(added) Oops. My misunderstanding. 1.2.8 must be the version of the desk top. Nevermind.

gosh, I'm sorry that this turned into such an epic. I've uninstalled and got the latest version from the website. The version that installed is 1.3.11, but when I create a database it creates a 4.1.3 file:

...and when I try to open the database, it tells me this:

If I select 'Fix Configuration' it basically just does the same again.

I mean, basically it tells me this after I 'Fix configuration':

Thanks to you both for your help @ameyasoft & @clem. It seems like there are some pretty big problems with newer versions of Neo4j on windows 10, so i just went back to 1.2.8.

I managed to get the query working with a couple of small adjustments:

LOAD CSV WITH HEADERS FROM "file:/Book1.csv" AS line
CREATE (p:Person {id: line.ProcessNumber, name: line.MasterName})
WITH p order by p.id ASC
WITH collect(p) as p1
CALL apoc.nodes.link(p1, 'NEXT')
RETURN p1

clem
Graph Steward

Can you give use some sample entries in the data file you're trying to import. It's a little hard to guess what data there is to work with and what you are trying to do exactly.

Well, I'm just taking baby steps towards solving a problem that my company is wrestling with.

Basically, we want to build basic process maps in Visio, something like this:

...Visio then has the functionality to export to excel tables something like this:

2X_3_3fab512527cb7bc333e75e4814ce9a78bb65a403.png

...which can then be converted to csv and translated into Neo4j. What i'm aiming for with regards this query is for Neo4j to compile the process maps into a sort of atlas, and so it's just a first step towards that. We're hoping to do things beyond that too, but I'm just kinda playing around and testing functionality at this point.

The Neoj4 visual looks like this after getting that query in place:

Awesome! You made it.

clem
Graph Steward

Baby steps are fine!

Graph DB's and Neo4J are conceptually different, so it helps taking things gradually:

  1. Cypher is declarative language, so if you are coming from a programming background, that takes some getting used to. (There is the flavor of multiple inheritance, but that's usually a mess in OOPS).
  2. The key thing about Graph DB's is the relationship vs. in Relational DB's where it's the Table and the keys.

I recommend playing with the Movie DB example comes with Neo4J plus the various online tutorials and YouTubes. And of course, these community boards.

I commend Neo4J in creating such great support material! I don't think I've every seen such a well supported product in my career!!

I must admit, it is tricky to get my head round the approach. I'm fairly familiar with C#, and it's an adjustment to get into this declarative approach, as you say.

I have managed to do a fair bit with nodes and relationships, but when it comes to forming relationships using properties of the nodes it's a bit harder. I guess it's just fallout of trying to work from a CSV rather than just using cypher.

Thanks again guys.