【问题标题】:Cypher - Convert graph result to list or JSONCypher - 将图形结果转换为列表或 JSON
【发布时间】:2018-01-08 13:51:26
【问题描述】:

我有以下 Neo4j 图表结果。


一世 '我正在使用多重关系-[RELATED_TO*]- 命令来获取它。

Match(n:Comment)
MATCH(n)-[RELATED_TO*]-(d:Comment)
return n, d;

我想在一个列表中显示结果,我可以说这个答案来自那个答案,或者在 JSON 级联文件中。实现这一目标的最佳方法是什么?

【问题讨论】:

    标签: json neo4j cypher py2neo


    【解决方案1】:

    我认为您可以使用 APOC 过程 apoc.convert.toTree 返回 JSON 级联结构。看看这个例子:

    创建样本数据集:

    CREATE (:Comment {id : 1})<-[:RELATED_TO]-(:Comment {id : 2})<-[:RELATED_TO]-(:Comment {id : 3})<-[:RELATED_TO]-(:Comment {id : 4})
    

    查询:

    MATCH p = (n:Comment)<-[RELATED_TO*]-(d:Comment)
    // I believe you are interested only in the "complete" path.
    // That is: you are not interested in the sub path like (:Comment {id : 2})-[:RELATED_TO]->(:Comment {id : 3}).
    // So this WHERE clause is used to avoid these sub paths.
    WHERE NOT (n)-->() AND NOT (d)<--()
    CALL apoc.convert.toTree([p]) yield value
    RETURN value
    

    输出:

    {
      "_type": "Comment",
      "related_to": [
        {
          "_type": "Comment",
          "related_to": [
            {
              "_type": "Comment",
              "related_to": [
                {
                  "_type": "Comment",
                  "_id": 142701,
                  "id": 4
                }
              ],
              "_id": 142700,
              "id": 3
            }
          ],
          "_id": 142699,
          "id": 2
        }
      ],
      "_id": 142698,
      "id": 1
    }
    

    或者,您可以使用nodes() 函数将路径作为参数传递来返回节点列表:

    MATCH p = (n:Comment)<-[RELATED_TO*]-(d:Comment)
    WHERE NOT (n)-->() AND NOT (d)<--()
    return nodes(p)
    

    结果将是:

    ╒═════════════════════════════════════╕
    │"nodes(p)"                           │
    ╞═════════════════════════════════════╡
    │[{"id":1},{"id":2},{"id":3},{"id":4}]│
    └─────────────────────────────────────┘
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-13
      • 1970-01-01
      • 2020-09-10
      • 2015-02-15
      • 2023-03-25
      • 1970-01-01
      • 2018-10-08
      • 2018-02-04
      相关资源
      最近更新 更多