Finding triplets having highest common relationships in Neo4j

北战南征 提交于 2019-12-23 03:18:23

问题


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


来源:https://stackoverflow.com/questions/45541495/finding-triplets-having-highest-common-relationships-in-neo4j

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!