0
votes

I have simple use case but still can't find the solution....
I have Questions nodes each question has category , each category can have many questions. I want to do the following:

retrieve 5 questions where each one of them is from different category.

tried the following but it is not what I am looking for cause I still get questions from the same category.

 START question=node:__types__(className = "com.socialist.server.graph.entities.Question")
 RETURN distinct(question.category), question
 LIMIT 5

What is the correct query for that use-case? Your answer is highly appriciated.

1
how did you model the fact that each question has a category? From your query it looks like that category is a property of a question. Wouldn't it make more sense to introduce nodes for categories?Stefan Armbruster
Hi thank you for the comment... lets say I have Category nodes : question-[:RELATE]->category how it will help to achieve what i want?assaf_miz84

1 Answers

1
votes

Something like this might work, but if you have a lot per category it won't be the best efficiency:

START question=node:__types__(className = "com.socialist.server.graph.entities.Question")
RETURN question.category, head(collect(question))
LIMIT 5

Also, soon (hopefully by 2.0 release) there will be a good way to get a random item out of a collection, something like this:

START question=node:__types__(className = "com.socialist.server.graph.entities.Question")
WITH question.category as category, collect(question) as questions
RETURN category, questions[rand() * length(questions)]
LIMIT 5