Reputation: 125
this is how the situation looks like, in this example, I want to have a weight of 4 on each edge, then I can delete the duplicates and like this I can have the graph weighted for my situationI want to count the number of duplicate edges from each node in my graph and then to put that number on one edge to make the graph weighted.
Is there a query which will help me achieve this?
Upvotes: 0
Views: 445
Reputation: 125
by modifing this query MATCH (n) WHERE size((n)-[:AGGREGATED]->()) = 0 AND size((n)-->()) > 0 WITH n LIMIT 1000 MATCH (n)-[r]->(m) WITH n, m, count(r) AS count CREATE (n)-[:AGGREGATED { weight:count}]->(m) RETURN count(*);
I arrived at the point where I want with the following query
OPTIONAL MATCH (u:Disease)-[r:HAS_CHILD]->(o:Disease) WITH u,o,count(r)as count CREATE (u)-[r:HAS_CHILD{weight:count}]->(o) RETURN u,r,o;
THE RESULT AFTER DELETING DUPLICATES RELATIONSHIPS
Upvotes: 0
Reputation: 7478
You can try something like that :
MATCH (n)-[r]->(m)
WITH n, m, count(r) AS count
CREATE (n)-[:AGGREGATED { weight:count}]->(m)
But this query is not performant at all because you're working on the entire graph, and you will probably put all your database in RAM.
So you have to batch this query :
MATCH (n) WHERE size((n)-[:AGGREGATED]->()) = 0 AND size((n)-->()) > 0
WITH n LIMIT 1000
MATCH (n)-[r]->(m)
WITH n, m, count(r) AS count
CREATE (n)-[:AGGREGATED { weight:count}]->(m)
RETURN count(*)
You can execute this query again and again till its result is not 0. And if you are lazy, there is a procedure in APOC to do that :
call apoc.periodic.commit("
MATCH (n) WHERE size((n)-[:AGGREGATED]->()) = 0 AND size((n)-->()) > 0
WITH n LIMIT $limit
MATCH (n)-[r]->(m)
WITH n, m, count(r) AS count
CREATE (n)-[:AGGREGATED { weight:count}]->(m)
RETURN count(*)", {limit: 1000});
Upvotes: 1