问题
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