Yohanes Setiawan
Yohanes Setiawan

Reputation: 81

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?

Upvotes: 0

Views: 2934

Answers (2)

Gabi Toma
Gabi Toma

Reputation: 1

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

Upvotes: 0

mastisa
mastisa

Reputation: 2083

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 :).

Upvotes: 1

Related Questions