Hi,
I have a graph (attached picture), how to write an optimal and the best query that retrieves me all of graph with all nodes, assuming that the number of questions and answers may of course be different.
My experience has been that it's usually better to build your graph model as close to the 'real world' as possible - not only is this much easier (and one of the biggest advantages of a graph db over relational) but it should allow you to ask a greater number of questions about your data, including questions that you haven't even thought of yet.
I would start by relating all (:Question) nodes to your (:Survey) node, since that is the reality and would for example allow you to quickly count the number of questions in a survey (and compare that across surveys if needed). From there you could label the correct (:Answer) node to each question with a (:CorrectAnswer) label, or relate it to the next (:Question) as you have done, if you never want to move to another question without correctly answering the last.
In terms of retrieving all relevant nodes in a single query, you should have a look the apoc subgraph functions, e.g subgraphNodes.
Try this:
MATCH (s:Survey) where s.name = "S1"
CALL apoc.path.spanningTree(s,{maxLevel:4}) YIELD path
RETURN path
Change the maxLevel number to your desired level.
Thanks, the query is too general and it will return too many nodes if I have a more extensive graph. The example below shows the extra nodes that I don't want to return as a result. I am interested only in type nodes: Survay, Question, Answer, Result
Thanks @nghia71. I added the node label filter to the query:
MATCH (s:Survey) where s.name = "S1"
CALL apoc.path.spanningTree(s,{labelFilter:'Survey|Question|Answer|Result', maxLevel:4}) YIELD path
RETURN path