Cypher query to find most commonly occurring node pairs that share same intermediate node with a given node

18 views
Skip to first unread message

amarrtya jana

unread,
Aug 7, 2017, 10:32:47 AM8/7/17
to Neo4j
Hi,

I have two types of nodes in my graph. One type is Testplan and the other is Tag. Testplans are tagged to Tags. I want most common pairs of Tags that share the same Testplans with a Tag having a specific name. I have been able to achieve the most common Tags sharing the same Testplan with one Tag, but getting confused when trying to do it for pairs of Tags. The cypher returning the list of single tags is shared below

MATCH (kw1:Tag)<-[e:TAGGED]-(tp1:Testplan)-[e2:TAGGED]->(kw2:Tag) 
WHERE kw1.name = "result"

RETURN kw1,kw2,count(tp1)

ORDER BY count(tp1) DESC

This cypher returns something as follows

Kw1                   kw2                       count(tp1)
“result”              “error”                   104
“result”              “prerequisites”           89
“result”              “alpha”                   63

I want the result to be

Kw1                   kw2                           count(tp1)
“result”              “error”,”prerequisites”       70
“result”              “error”,”alpha”               63

Here the count in the second table gives the number of Testplans being TAGGED to "result","error" and "prerequisites" and similarly "result","error" and "alpha"

Kamal Murthy

unread,
Aug 7, 2017, 4:53:12 PM8/7/17
to Neo4j
Hi,

You can try this:

//Get tp1 nodes tagged to "result"
MATCH (tp1:Testplan)-[:TAGGED]->(kw1:Tag) 
WHERE kw1.name = "result" 
WITH COLLECT (tp1) as nodes
UNWIND nodes as tp2

//use the above collection to get the tp1 nodes tagged to "result" and "error"
MATCH (tp2)-[:TAGGED]->(kw2:Tag)
WHERE kw2.name="error"
WITH COLLECT (tp2) as nodes
UNWIND nodes as tp3

//use the second collection to get the tp1 nodes tagged to "result", "error" and "prerequisites"
MATCH (tp3)-[:TAGGED]->(kw3:Tag)
WHERE kw2.name="prerequisites"
RETUTN Count(tp3) as Cnt
 
Here Cnt gives the total count of tp1 nodes tagged to "result", "error" and "prerequisites"

-Kamal
Reply all
Reply to author
Forward
0 new messages