aq13
aq13

Reputation: 25

Cypher Query Language/Neo4j - Nested Returns of Variable Path Length

I have a graph structure in Neo4j for a questionnaire that has the following relationships:

    (a:Category)-[:INITIAL_QUESTION]->(b:Question)-[c:Answer*]->(d:Question)

where the specific question text is contained in (b|d).text and the possible answers for each question is contained in the relationship c.response From the initial question, there are some paths that are longer than others. I would like the return to look something like this:

{"category": "example questionnaire category",
"initialQuestion" : {
                    "id": 1,
                    "text": "Example text here",
                    "possibleAns": {
                      "yes" : {
                                "id": 2,
                                "text": "Second question text here?",
                                "possibleAns": {
                                                  "ans1": {/*Question 4 containing all possible child question nodes nested
                                                              in possibleAns*/},
                                                  "ans2": {/*Question 5 containing all possible child question nodes nested
                                                              in possibleAns*/},
                                }
                      },
                      "no" :{
                                "id": 3,
                                "text": "Different question text here?",
                                "possibleAns": {
                                                  "ans1": {/*Question 6 containing all possible child question nodes nested
                                                              in possibleAns*/},
                                                  "ans2": {/*Question 7 containing all possible child question nodes nested
                                                              in possibleAns*/},
                                }
                      }
                    }
 }
}

so that the entire category questionnaire is contained in a single, nested map. I've seen some other examples, but haven't been able to tweak those queries to fit my needs, especially given the variable depth of the questionnaire branches.

Is there a Cypher query that makes this possible? If not, what would be the best approach for retrieving the entire questionnaire from the db?

Upvotes: 1

Views: 176

Answers (1)

stdob--
stdob--

Reputation: 29167

I think that this is not done with standard tools (cypher etc.)

So, or transform the result from cypher query in the json-tree programmatically.

Or, if your neo4j-server versions not less 3.0, you can try apoc.convert.toTree:

MATCH path = (a:Category)
             -[:INITIAL_QUESTION]->(b:Question)-[c:Answer*]->
             (d:Question)
WITH collect(path) as paths
CALL apoc.convert.toTree(paths) yield value as tree
RETURN tree

Upvotes: 2

Related Questions