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?
Asked
Active
Viewed 2,874 times
6

Bruno Peres
- 15,845
- 5
- 53
- 89

AkhilTC
- 71
- 1
- 3
-
Have you only `name` property in your nodes? – Bruno Peres Feb 21 '18 at 11:17
-
Also, can exists more than 2 nodes with the same name? For example, 3 nodes with `name = Java`. – Bruno Peres Feb 21 '18 at 11:20
-
yes, nodes have only name property and there exist more than one nodes with the same name – AkhilTC Feb 21 '18 at 13:32
-
more than TWO nodes with the same name? – Bruno Peres Feb 21 '18 at 13:34
-
I made a mistake in the previous comment, sorry. I need know if you can have more than 2 nodes with the same name. – Bruno Peres Feb 21 '18 at 13:34
-
yes https://i.stack.imgur.com/ThrdU.png – AkhilTC Feb 21 '18 at 14:29
-
AkhilTC - Let me know if the answer I provided has solved your issue. Thanks. – Bruno Peres Feb 22 '18 at 12:39
-
Bruno Peres - I tried the query you suggested but the issue was not solve – AkhilTC Feb 22 '18 at 16:21
-
I tried the query you suggested but shows error message Neo.ClientError.Procedure.ProcedureRegistrationFailed apoc.refactor.mergeNodes is unavailable because it is sandboxed and has dependencies outside of the sandbox. Sandboxing is controlled by the dbms.security.procedures.unrestricted setting. Only unrestrict procedures you can trust with access to database internals. – AkhilTC Feb 22 '18 at 16:30
-
AkhilTC - This is because APOC procedures should be installed. Take a look in the [installation section](https://neo4j-contrib.github.io/neo4j-apoc-procedures/#_installation). – Bruno Peres Feb 22 '18 at 16:38
1 Answers
6
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:
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:

Bruno Peres
- 15,845
- 5
- 53
- 89
-
I am mapping state transitions in APIs using Neo4j. My Relationship also has a property. The relationship is the count of the response codes. I want to merge them all together including the property. (API_1)-[:CALLS {'2xx': 1}] -> (API_2) (API_1)-[:CALLS {'4xx': 1}] -> (API_2) (API_1)-[:CALLS {'2xx': 2}] -> (API_2) (API_1)-[:CALLS {'4xx': 3}] -> (API_2) Desired merging result: (API_1)-[:CALLS {'2xx': 3, '4xx': 4}] -> (API_2) Is this possible, I am not sure – Anuvrat Tiku Sep 24 '18 at 19:17