markai
markai

Reputation: 133

Extract cypher relationship weights for each type (label)

I need a cypher query for extracting relationships that are labelled with VIEWED and UPDATED and display the sum of the edge weight per user.

I want to calculate a value for each user based on how many times he viewed or updated ANY document.

Example: User a viewed document d 3 times and document e 4 times. The same user updated document f 6 times. I want to assign points each time the user updated or viewed a document, so I need a result like the following: user: a - views: 7 - updates: 6

This is what I currently have:

MATCH (n:User)-[r:VIEWED|UPDATED]->(f:File) WHERE f.id IN ['some_id','another_id'] RETURN n, collect(type(r)), collect(r.weight);

Upvotes: 1

Views: 512

Answers (2)

markai
markai

Reputation: 133

This query works for me:

MATCH (n:User)-[r1:VIEWED]->(f:File)
OPTIONAL MATCH (n:User)-[r2:UPDATED]->(f:File)
WHERE f.id IN ['some_id', 'another_id']
return n, sum(r1.weight) as totalViewedWeight, sum(r2.weight) as totalUpdatedWeight
ORDER BY totalUpdatedWeight DESC, totalViewedWeight DESC;

Upvotes: 0

Michael Hunger
Michael Hunger

Reputation: 41676

You almost got it, good attempt!

Either like this:

MATCH (n:User)-[r:VIEWED|UPDATED]->(f:File)
WHERE f.id IN ['some_id','another_id'] 
RETURN n, type(r) as type, sum(r.weight) as weight

or if you want one row per user:

MATCH (n:User)-[r:VIEWED|UPDATED]->(f:File)
WHERE f.id IN ['some_id','another_id'] 
WITH n, type(r) as type, sum(r.weight) as weight
RETURN n, collect({type:type,weight:weight})

Upvotes: 2

Related Questions