Finding triplets having highest common relationshi

2019-06-05 22:12发布

问题:

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

回答1:

MATCH (kw1:Tag)<-[e:TAGGED]-(tp1:Testplan)-[e2:TAGGED]->(kw2:Tag),
(tp1)-[:TAGGED]->(kw3:Tag)
WHERE kw1.name = "result"
AND ID(kw2)<ID(kw3)
RETURN kw2, kw3,count(tp1)
ORDER BY count(tp1) DESC


回答2:

I think you can easily fix this using collect()

MATCH (kw1:Tag)<-[e:TAGGED]-(tp1:Testplan)-[e2:TAGGED]->(kw2:Tag) 
WHERE kw1.name = "result"
WITH tp1,collect(kw2) as tags,count(*) as count
RETURN tags,sum(count)

ORDER BY count DESC


标签: neo4j cypher