1
votes

I have a couple of nodes in my neo4j database, all of the same type and with a couple of attributes & values. Now I would like to introduce a ratio to each node: how likely is it that a different node has the same value at this attribute (if it has this attribute). I can calculate the ratio, which I am aiming at. But in order to use that in a SET command, I need to add user2 to the second WITH line - and then the ratio doesn't work any more. How must I change the command so the ratio is calculated and stored in the 7 nodes?

cheers - rene

Example: http://console.neo4j.org/r/iwvxf1

My goal: (obviously, 'ratio' doesn't exist yet - that's what I'm trying to achieve)

> match n return n.ratio, n.feature1, n.feature2
> +---------+------------+------------+ 
> | n.ratio | n.feature1 | n.feature2 |
> +---------+------------+------------+
> | 0.42857 | abc123     | ABC        | 
> | 0.42857 | abc123     | ABC        |
< | 0.42857 | abc123     | ABC        |
> | 0.28571 | xyz123     | ABC        |
> | 0.28571 | abc987     | QWE        |
> | 0.28571 | xyz123     | WER        |
> | 0.28571 | abc987     | XQZ        |
> | null    | null       | 123        |
> | null    | null       | 987        |
> +---------+------------+------------+

My query so far:

// GET GLOBAL COUNT
MATCH (user1:User)
WHERE user1.feature1 IS NOT NULL
WITH count(*) AS global_count

// GET INDIVIDUAL COUNT
OPTIONAL MATCH (user2:User)
WHERE user2.feature1 IS NOT NULL
WITH global_count, user2.feature1 AS feature, count(*) AS indiv_count
RETURN feature, global_count, indiv_count, ((indiv_count*1.0)/(global_count*1.0)) AS ratio

This does indeed return the ratios I want. But I have no clue what I need to do in order to store these ratios in the individual nodes. Being a script-kid, my natural reaction would be an associative array, but AFAIK, there's no such thing in cypher. If you can tell me that I'm wrong, you make my day :-)

1

1 Answers

4
votes

Instead of return, use WITH:

WITH feature, ratio
MATCH (u:User)
WHERE u.feature1 = feature
SET u.ratio = ratio