Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cypher - create relationship using value of calculation

I'm working on Neo4j and manipulate data using their REST API and Cypher.
I have the following code:

MATCH (u:USER) WHERE id(u) = 10192
MATCH (uc:USERS_CLUSTER) WHERE id(uc) = 19618
MATCH (u)-[r1:HAVE]->(i:ITEM)<-[r2:HAVE]-(u2:USER)-[ic:IN_CLUSTER]->(uc)
OPTIONAL MATCH (u)-[oldic:IN_CLUSTER]->(uc)
WHERE (u) <> (u2)
DELETE oldic
// --> PROBLEM --> CREAT (u)-[:IN_CLUSTER {score: XXXXXXXXX}]->(uc)
RETURN id(u),u,id(uc),uc,
// --> VALUE --> 
((avg(abs(2-abs(r1.rank-r2.rank))*(2-abs(r1.rank-r2.rank)))+4)/8)*100 as calcedMatch
ORDER BY calcedMatch DESC

There is a value calcedMatch I'm calculating, and I'm able to return it without any problem. But what I would like to do is create a new IN_CLUSTER relation and having a property score with value of the calcedMatch (Where I put the XXXXXXXX). Is that possible?

like image 997
Roee Gavirel Avatar asked Nov 25 '25 13:11

Roee Gavirel


1 Answers

It is advisable to not use the id of a node as a reference; it is not immutable in the context of the assigned node or relationship (i.e. they can be reused when objects are destroyed).

I updated your query a little. You do not need to delete the relationship and then recreate it. You can simply use MERGE and it will create it if it is not present. Then you can set the new score on it afterwards.

MATCH (u:USER) WHERE id(u) = 10192
WITH u
MATCH (uc:USERS_CLUSTER) WHERE id(uc) = 19618
WITH uc
MATCH (u)-[r1:HAVE]->(i:ITEM)<-[r2:HAVE]-(u2:USER)-[ic:IN_CLUSTER]->(uc)
WHERE (u) <> (u2)
WITH u, uc, ((avg(abs(2-abs(r1.rank-r2.rank))*(2-abs(r1.rank-r2.rank)))+4)/8)*100 as calcedMatch
MERGE (u)-[ic:IN_CLUSTER]->(uc)
set ic.score = calcedMatch
RETURN id(u),u,id(uc),uc, ic.score
ORDER BY ic.score DESC
like image 58
Dave Bennett Avatar answered Nov 28 '25 15:11

Dave Bennett