Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to merge nodes that have the same value for name property in Neo4j

I just push text corpus into Neo4j database. When I execute MATCH (n) RETURN n Cypher query, it returns multiple nodes with the same name. how can I merge these nodes as one?

Graph Visualization of my DB nodes having same name

like image 940
AkhilTC Avatar asked Feb 21 '18 06:02

AkhilTC


1 Answers

Your name values have different values because of upper and lower case letters ("Java" and "java" are different).

I reproduced your scenario creating a sample data set:

CREATE (n1:Node {name : "Java"}),
(n2:Node {name : "Java"}),
(n3:Node {name : "java"}),
(n1)-[:TYPE]->(),
(n1)-[:TYPE]->(),
(n1)-[:TYPE]->(),
(n2)-[:TYPE]->(),
(n2)-[:TYPE]->(),
(n3)-[:TYPE]->()

The above query will produce this graph:

Sample graph

To merge all "Java" nodes you can use the APOC Procedure apoc.refactor.mergeNodes(nodes). Running the following query:

MATCH (n:Node)
// using toLower function to group nodes with the same name but 
// different cases (eg Java, java, javA)
WITH toLower(n.name) as name, collect(n) as nodes
// passing the nodes collection to mergeNodes APOC procedure
CALL apoc.refactor.mergeNodes(nodes) yield node
RETURN *

Will update your graph to:

Updated graph

like image 170
Bruno Peres Avatar answered Oct 04 '22 20:10

Bruno Peres