首页 文章

Cypher查询语言/ Neo4j - 可变路径长度的嵌套返回

提问于
浏览
1

我在Neo4j中有一个图表结构,用于调查问卷,其中包含以下关系:

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

特定问题文本包含在 (b|d).text 中,并且每个问题的可能答案都包含在关系中 c.response 从最初的问题来看,有些路径比其他路径长 . 我希望返回看起来像这样:

{"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*/},
                                }
                      }
                    }
 }
}

这样整个类别问卷就包含在一个嵌套的 Map 中 . 我已经看到了其他一些例子,但未能调整这些查询以满足我的需求,特别是考虑到问卷分支的可变深度 .

是否有Cypher查询可以实现这一目标?如果没有,那么从数据库中检索整个调查问卷的最佳方法是什么?

1 回答

  • 1

    我认为这不是用标准工具(密码等)完成的 .

    所以,或者以编程方式从json-tree中的cypher查询转换结果 .

    或者,如果您的neo4j-server版本不低于3.0,您可以尝试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
    

相关问题