'Weight in Relationship Property Neo4j

I want to add weight in relationship property. Weight are counted from this code:

MATCH (n1)-[r1:NEXT]->(n2)
DELETE r1
RETURN n1, n2, apoc.create.vRelationship(n1, 'WEIGHT', {weight:count(r1)}, n2);

But the "WEIGHT" doesn't appear in the property of r1 How can I fix this?



Solution 1:[1]

I didn't worked with apoc so I answer your question with pure cypher:

MATCH (n1)-[r1:NEXT]-(n2)
WITH count(r1) as count_rel, n
CREATE (n1)-[:WEIGHT {weight: count_rel}]->(n2)

If you delete r1 relation it would create WEIGHT relation for every NEXT relation. For handling this problem you can remove NEXT relation in another query after this one, unfortunately I don't know how merge these 2 query in one query :(. Hope someone help to improve this answer :).

Solution 2:[2]

This APOC method creates a virtual relationship - do notice the v in apoc.create.vRelationship

https://neo4j.com/labs/apoc/4.1/overview/apoc.create/apoc.create.vRelationship/ apoc.create.vRelationship(nodeFrom,'KNOWS',{key:value,…?}, nodeTo) returns a virtual relationship

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mastisa
Solution 2 Gabi Toma