bitnine-oss / agensgraph

AgensGraph, a transactional graph database based on PostgreSQL
http://www.agensgraph.org
Other
1.34k stars 149 forks source link

Does agensgraph support merging nodes? #448

Closed zzl221000 closed 5 years ago

zzl221000 commented 6 years ago

There are two such data (a)-[A]-(b) and (c)-[B]-(d). The two nodes a and c need to be merged to combine the original data into (d)-[B]-(a)-[A]-(b) Such a function cannot be found in the manual,Is there any way to achieve it?

htlim commented 6 years ago

Hi @zzl221000

There is no function that solves it at once. However, it seems to be solved by connecting vertex a and vertex d and assigning attribute of edge B to new edge. Here is the example code:

CREATE ({node:'a'})-[:rel {rel:'ab'}]->({node:'b'}), ({node:'c'})-[:rel {rel:'cd'}]->({node:'d'});  
GRAPH WRITE (INSERT VERTEX 4, INSERT EDGE 2)

htlim=# MATCH (a)-[r:rel]->(b) RETURN a,r,b;
              a              |               r                |              b              
-----------------------------+--------------------------------+-----------------------------
 ag_vertex[1.1]{"node": "a"} | rel[3.1][1.1,1.2]{"rel": "ab"} | ag_vertex[1.2]{"node": "b"}
 ag_vertex[1.3]{"node": "c"} | rel[3.2][1.3,1.4]{"rel": "cd"} | ag_vertex[1.4]{"node": "d"}
(2 rows)

MATCH (a {node:'a'})-[ab]->(b {node:'b'}), (c {node:'c'})-[cd]->(d {node:'d'}) WHERE a != c
CREATE (a)-[:rel =cd]->(d)
DELETE c, cd;
GRAPH WRITE (INSERT VERTEX 0, INSERT EDGE 1, DELETE VERTEX 1, DELETE EDGE 1)

htlim=# MATCH (a)-[r:rel]->(b) RETURN a,r,b;
              a              |               r                |              b              
-----------------------------+--------------------------------+-----------------------------
 ag_vertex[1.1]{"node": "a"} | rel[3.1][1.1,1.2]{"rel": "ab"} | ag_vertex[1.2]{"node": "b"}
 ag_vertex[1.1]{"node": "a"} | rel[3.3][1.1,1.4]{"rel": "cd"} | ag_vertex[1.4]{"node": "d"}
(2 `rows)
zzl221000 commented 6 years ago

Thank you @htlim I tried this solution. There are 90 million nodes and 200 million relationships in my database. It takes about 30 seconds to execute each delete statement, which seems a bit slow.