有一个像 post-->category
这样的图,我怎样才能获得一个类别的帖子?
IE。:
Having
Post A1 --> Category A
Post A2 --> Category A
Post B1 --> Category B
Post B2 --> Category B
Post B3 --> Category B
Post C1 --> Category C
I should get Post A2, Post B1, Post C1.
我不介意我为给定类别获得什么职位,只是为每个类别获得一个。
谢谢!
最佳答案
要随机选择每个类别的帖子:
MATCH (p:Post)-[:HAS_CATEGORY]->(c:Category)
WITH c, collect(p) as posts
RETURN c, posts[toInt(rand()*length(posts))]
我们正在使用
collect
每个类别的聚合函数,在返回时我们随机选择一个条目。
关于Neo4j 密码 : Group nodes by the relation to another node,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29925814/