How to set weights for infered edges?

I would like to add edges between nodes of distance 2 apart, and set a weight property according to how many such paths of length 2 exists. I think a query like this is close, but isn't quite right:

Match path=((u1:User)-[:KNOWS]->(:User)-[:KNOWS]->(u2:User))
With u1, u2, Count(path) as weight
Merge (u1)-[e:FILLEDIN]->(u2)
Set e.weight=weight

This doesn't seem to do anything in my graph. What's the best way to do this?

That looks fine to me. You may want to check that you're using the right labels. If you stopped your query early, doing a RETURN on line 2 instead of a WITH, do you get results back?

Ah, you're right! There was a typo in my labels, thank you.

Related to my question: is it possible to return a "view" of the resulting edges in the browser without actually saving the new edges to the database?

Hi Rogie,

The view you are looking for might be a good use case for virtual relationships with the apoc library. 14.2. Virtual Nodes/Rels - Chapter 14. Virtual Nodes & Relationships (Graph Projections)

Match path=((u1:User)-[:KNOWS]->(:User)-[:KNOWS]->(u2:User))
With u1, u2, Count(path) as weight
RETURN u1, u2, apoc.create.vRelationship(u1,'FILLEDIN',{weight:weight},u2) as rel;

Best wishes,
Nathan

3 Likes