Neo4j Cypher - String to Integer

后端 未结 3 542
臣服心动
臣服心动 2021-01-13 12:02

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)
         


        
相关标签:
3条回答
  • 2021-01-13 12:37

    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.

    0 讨论(0)
  • 2021-01-13 12:38

    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.

    0 讨论(0)
  • 2021-01-13 12:58

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

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

    0 讨论(0)
提交回复
热议问题