How to merge nodes that have the same value for na

2019-05-03 09:30发布

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

1条回答
一夜七次
2楼-- · 2019-05-03 09:59

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

查看更多
登录 后发表回答