Create relationships according to the order of the nodes property values

Hi,

I have a graph database. The nodes in the graph suppose to be connected based on the order of the given property values. For example, the graph nodes can be:

(a{p:1}) (b{p:3}) (c{p:6}) (d{p:8})

then the connections suppose to be:

a--b--c--d

according to the order of the p value.

Where the problem is that the p values are randomly given.

So, how can I create the relationships between the nodes by Cypher?

Could you guys give me any suggestions? Thanks a lot!

MATCH (n:NodeLabel)
WITH n ORDER BY n.propertyValue
WITH COLLECT(n) as nodeList
CALL apoc.nodes.link(nodeList,"RELATIONSHIPTYPE")
RETURN nodeList

1 Like

Hi @ganesanmithun323, thanks for your answer. While could you give more explanations for the apoc.nodes.link operation. Neo4j is a pretty new platform for me. Thanks a lot!

HI @hn.chen , apoc is a kind of plugin which you can install in your db ..it has lof of useful functions ..

know more about apoc here : https://neo4j.com/docs/labs/apoc/current/

1 Like