Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SET in combination with CASE statement in cypher

Tags:

case

neo4j

cypher

I am tryin to set two different relationship properties to a count, with a case construct depending on the value of another relationship property. There is a console at http://console.neo4j.org/?id=rt1ld5

the cnt column contains the number of times r.value occurs. The two first rows of the initial query in the console indicate that the term "Car" is linked to 1 document that is considered relevant, and to two documents that are considered not relevant.

I want to SET a property on the [:INTEREST] relation between (user) and (term) with two properties, indicating how many times an interest is linked to a document that is considered relevant or not. So for (John)-[r:INTEREST]->(Car) I want r.poscnt=1 and r.negcnt=2

I.m struggling with the CASE construct. I tried various ways, this was the closest I got.

MATCH (u:user)-[int:INTEREST]->(t:term)<-[:ISABOUT]-(d:doc)<-    [r:RELEVANCE]-(u)
WITH int, t.name, r.value, count(*) AS cnt
CASE
  WHEN r.value=1 THEN SET int.poscnt=cnt
  WHEN r.value=-1 THEN SET int.negcnt=cnt
END

But it's returning an error

Error: Invalid input 'A': expected 'r/R' (line 3, column 2)
"CASE"
  ^
like image 668
Graphileon Avatar asked Mar 17 '14 16:03

Graphileon


Video Answer


1 Answers

This did it! Also see console at http://console.neo4j.org/?id=rq2i7j

MATCH (u:user)-[int:INTEREST]->(t:term)<-[:ISABOUT]-(d:doc)<-[r:RELEVANCE]-(u)
WITH int, t, 
     SUM(CASE WHEN r.value= 1 THEN 1 ELSE 0 END ) AS poscnt, 
     SUM(CASE WHEN r.value= -1 THEN 1 ELSE 0 END ) AS negcnt
SET int.pos=poscnt,int.neg=negcnt
RETURN t.name,int.pos,int.neg
like image 135
Graphileon Avatar answered Nov 15 '22 08:11

Graphileon