Having a graph like post-->category
, how can I get one post per 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.
I don't mind what post I get for a given category, just to get one for each category.
Thanks!
To pick a post per category by random:
MATCH (p:Post)-[:HAS_CATEGORY]->(c:Category)
WITH c, collect(p) as posts
RETURN c, posts[toInt(rand()*length(posts))]
We're using the collect
aggregation function per category and in the return we pick one entry by random.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.