【问题标题】:Cypher Query Language/Neo4j - Nested Returns of Variable Path LengthCypher 查询语言/Neo4j - 可变路径长度的嵌套返回
【发布时间】:2017-02-27 17:11:54
【问题描述】:

我在 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*/},
                                }
                      }
                    }
 }
}

以便整个类别问卷包含在单个嵌套地图中。我看过其他一些示例,但无法调整这些查询以满足我的需求,尤其是考虑到调查问卷分支的可变深度。

是否有使这成为可能的 Cypher 查询?如果不是,从数据库中检索整个问卷的最佳方法是什么?

【问题讨论】:

    标签: neo4j cypher


    【解决方案1】:

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

    因此,或者以编程方式转换 json-tree 中密码查询的结果。

    或者,如果你的 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-06-14
      • 2012-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-23
      • 1970-01-01
      相关资源
      最近更新 更多