Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
10-05-2020 04:54 PM
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.
Additional question, does it make sense to add additional relations (survey node connected to all question nodes) for a more optimal query?
What's the proper way to handle this in cypher as tree structure?
Solved! Go to Solution.
10-06-2020 02:28 PM
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
10-05-2020 07:38 PM
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.
10-05-2020 09:24 PM
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.
10-06-2020 02:51 AM
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
10-06-2020 08:59 AM
I think you can use @ameyasoft query and filter the nodes by labels (the ones you want to use).
10-06-2020 02:28 PM
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
All the sessions of the conference are now available online