Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Neo4j Cypher - String to Integer

Tags:

neo4j

cypher

Here the value in property r.weight is a string. i have tried following possibilities to convert it into a integer,

INT(r.weight)
Integer.parseInt(r.weight)
convert(INT,r.weight)

but nothing works. Is there a proper function to do so?

reduce(total = 0, r in relationships(p): total + INT(r.weight))

Note : Its not a possible duplicate of Gremlin type conversion

Version : 1.9.5

like image 894
Jeevanantham Avatar asked Mar 06 '14 09:03

Jeevanantham


3 Answers

In Cypher 3.3.5 toInt() is deprecated, rather use toInteger()

i.e. toInteger(r.weight) - this works fine

like image 84
chamber5 Avatar answered Nov 15 '22 10:11

chamber5


To extend chamber5's answer.

Presumably you are interested in doing some arithmetic with the weights. An obvious use case is to sum the weights going into a node. For example, in a social network perhaps we are interested in how many messages Ada has received from other nodes (colleagues); with one message increasing the weight of that edge or relationship by one.

Thus we want to MATCH for all relationships of a given type (r) going into a Person node with the name property of "Ada".

MATCH ()-[r]->(n:Person {name:"Ada"})
RETURN sum(toInteger(r.weight)) AS total_weights_in;

Of course we could look for bidirectional total weights also by adjusting our ASCii art:

MATCH ()<-[r]->(n:Person {name:"Ada"})
RETURN sum(toInteger(r.weight)) AS total_weights_in;

This work in Neo4j version 3.4.9.

like image 42
mammykins Avatar answered Nov 15 '22 12:11

mammykins


As stated in the comments above there is no easy way to do this with Cypher in 1.9.x. As a workaround you could use the neo4j-shell and use the gsh or jsh or eval command to execute a script that iterates over all relationships and converts the value of weight property from String to numeric.

like image 25
Stefan Armbruster Avatar answered Nov 15 '22 11:11

Stefan Armbruster