简体   繁体   中英

Count the number of relationships between a pair of nodes and set it as parameter in Neo4J

I have a graph where a pair of nodes can have several relationships between them. I would like to count this relationships between each pair of nodes, and set it as a parameter of each relationship.

I tried something like:

MATCH (s:LabeledExperience)-[r:NextExp]->(e:LabeledExperience) 
with s, e, r, length(r) as cnt
MATCH (s2:LabeledExperience{name:s.name})-[r2:NextExp{name:r.name}]->(e2:LabeledExperience{name: e.name})
SET r2.weight = cnt

But this set the weight always to one. I also tried:

MATCH ()-[r:NextExp]->() 
    with r, length(r) as cnt
    MATCH ()-[r2:NextExp{name:r.name}]->()
    SET r2.weight = cnt

But this takes too much time since there are more than 90k relationships and there is no index (since it is not possible to have them on edges).

They are always set to 1 because of the way you are counting.

When you group by s, e, r that is always going to result in a single row. But if you collect(r) for every s, e then you will get a collection of all of the :NextExp relationships between those two nodes.

Also, length() is for measuring the length (number of nodes) in a matched path and should not work directly on a relationship.

Match the relationship and put them in a collection for each pair of nodes. Iterate over each rel in the collection and set the size of the collection of rels.

MATCH (s:LabeledExperience)-[r:NextExp]->(e:LabeledExperience) 
WITH s, e, collect(r) AS rels
UNWIND rels AS rel
SET rel.weight = size(rels)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM