Neo4j Cypher - String to Integer

两盒软妹~` 提交于 2019-12-19 08:55:05

问题


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


回答1:


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.




回答2:


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

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




回答3:


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.



来源:https://stackoverflow.com/questions/22220732/neo4j-cypher-string-to-integer

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!