1
votes

suppose i have following relationships stored in neo4j.

A->B,A->D,C->B,C->E

Here A, C are of same label nodes and B, E also are of same label nodes. What is the cypher query to count how many nodes A and C have in common? Based on that I want to make to a relationship between A and C. I would like to add a relationship rank between them and give it some value say 0.5 because 1 node common. What would that query look like?

1

1 Answers

0
votes

To return the number of common nodes between A and C match a pattern that has A at one and C a the other with an intermediary node. Then count the occurrences of the intermediary node.

match (:TypeOne {name: 'A'})--(common)--(:TypeOne {name: 'C'})
return count(common)

If you want to create a relationship directly between A and C as a result of the match then use merge or create with the A and C nodes. And use set to add a value to the newly created relationship.

Something like this should satisfy your requirements.

match (a:TypeOne {name: 'A'})--(common)--(c:TypeOne {name: 'C'})
with a, c, count(common) as in_common
merge (a)-[rel:COMMON_WITH]->(c)
set rel.value = in_common * 0.5
return *