Cypher Neo4j query -
i developing kind of social trivia game main db neo4j. having hard time in specific use-case.
i have challenge node 2 opponents nodes related:
(opponent1)-[:opponent]->(challenge)<-[:opponent]-(opponent2)
the challenge relate subject node:
challenge-[:subject]->subject
each subject relate many questions:
subject-[:has]->question
if opponent answered specific question before bellow relation exist:
opponent-[:answer]->question
the use-case: need retrieve x questions (which never been answered both opponents) challenge
i have following cypher query:
start challenge=node({0}) , subject=node({1}) match (opponent1)-[:opponent]->(challenge)<-[:opponent]-(opponent2) opponent1,opponent2,subject match (subject)-[:has]->(question) length(opponent1-[:answer]->question) = 0 , length(opponent2-[:answer]->question) = 0 return question limit {2}
the above query works fine , retrieve possible questions challenge.
the problem questions retrieved consequently , not randomly.
explanation: each question related template node has category property.
question-[:template]->template
the questions created in first place template mean questions specific category has sequenced id in db therefore above query retrieves questions same category.
i want able retrieve challenge questions randomly. also, query structure correct performance perspective?
i'm not aware of native way of how random result, there smting asked here: neo4j: there way/how select random nodes?
about query, depends on data, try rather this:
start challenge=node({0}) , subject=node({1}) match (opponent1)-[:opponent]->(challenge)<-[:opponent]-(opponent2) opponent1,opponent2,subject match (subject)-[:has]->(question) not(opponent1-[:answer]->question) , not (opponent2-[:answer]->question) return question limit {2}
Comments
Post a Comment