【问题标题】:neo4j Cypher hierarchical tree build response to JSONneo4j Cypher 分层树构建对 JSON 的响应
【发布时间】:2016-07-25 22:03:05
【问题描述】:

你能帮我建立密码查询吗?我有以下图形数据库结构:

(parent:Category)-[:subcategory]->(child:Category)

有了这个图形数据,我就有了深层次的分层树。

我在 Stackoverfllow.com 上找到了以下代码并针对我的数据进行了更改:

MATCH (root:Category)-[:subcategory]->(parent:Category)-[:subcategory]->(child:Category)
WITH root, {category: parent, children: collect(child)} AS parent_with_children
WHERE NOT(()-[:subcategory]->(root))
RETURN {category: root, children: collect(parent_with_children)}

但他仅针对具有 3 级树的深度构建响应。我需要更大的。我正在尝试像这个例子一样构建 json 响应:

  [
    category: {
      name: "PC"
      children: {
        category: {
          name: "Parts"
          children: {
            category: {
              name: "CPU"
              ...
            }
          }
        },
        category: {
          name: "Accessories"
          ...
        }
      } 
    }, 
    category: {
      name: "Laptop"
      ...
    }
  ]

Cypher 可以进行递归调用吗?我觉得这样会更好。

谢谢。

附:我知道关于 SO 有类似的问题,但它们对我没有帮助。

【问题讨论】:

  • 是的,Cypher 并没有真正进行递归。返回数据树时,最好的选择是使用以下cybersam 建议的内容,或者将节点/关系作为表格返回并在内存中构建它们。

标签: neo4j cypher


【解决方案1】:

Cypher 不太适合在叶子处于任意深度时以树状结构转储图形数据。

但是,使用 neo4j 3.x,如果您能够在您的服务器上安装 APOC plugin 并使用 apoc.convert.toTree 过程,则可以接近您想要的。

首先,让我们创建一些示例数据:

CREATE
  (c1:Category {name: 'PC'}),
    (c1)-[:subcategory]->(c2:Category {name: 'Parts'}),
      (c2)-[:subcategory]->(c3:Category {name: 'CPU'}),
        (c3)-[:subcategory]->(c4:Category {name: 'CacheRAM'}),
    (c1)-[:subcategory]->(c5:Category {name: 'Accessories'}),
      (c5)-[:subcategory]->(c6:Category {name: 'Mouse'}),
      (c5)-[:subcategory]->(c7:Category {name: 'Keyboard'}),
  (c10:Category {name: 'Laptop'}),
    (c10)-[:subcategory]->(c20:Category {name: 'Parts'}),
      (c20)-[:subcategory]->(c30:Category {name: 'CPU'}),
    (c10)-[:subcategory]->(c40:Category {name: 'Accessories'}),
      (c40)-[:subcategory]->(c50:Category {name: 'Stylus'});

然后用这个查询:

MATCH p=(n:Category)-[:subcategory*]->(m)
WHERE NOT ()-[:subcategory]->(n)
WITH COLLECT(p) AS ps
CALL apoc.convert.toTree(ps) yield value
RETURN value;

...您将获得 N 个结果行,其中 N 是根 Category 节点的数量。这是样本结果的 sn-p:

{
  ...
      "row": [
        {
          "_id": 150,
          "_type": "Category",
          "name": "PC",
          "subcategory": [
            {
              "_id": 154,
              "_type": "Category",
              "name": "Accessories",
              "subcategory": [
                {
                  "_id": 156,
                  "_type": "Category",
                  "name": "Keyboard"
                },
                {
                  "_id": 155,
                  "_type": "Category",
                  "name": "Mouse"
                }
              ]
            },
            {
              "_id": 151,
              "_type": "Category",
              "name": "Parts",
              "subcategory": [
                {
                  "_id": 152,
                  "_type": "Category",
                  "name": "CPU",
                  "subcategory": [
                    {
                      "_id": 153,
                      "_type": "Category",
                      "name": "CacheRAM"
                    }
                  ]
                }
              ]
            }
          ]
        }
      ],
  ...
      "row": [
        {
          "_id": 157,
          "_type": "Category",
          "name": "Laptop",
          "subcategory": [
            {
              "_id": 158,
              "_type": "Category",
              "name": "Parts",
              "subcategory": [
                {
                  "_id": 159,
                  "_type": "Category",
                  "name": "CPU"
                }
              ]
            },
            {
              "_id": 160,
              "_type": "Category",
              "name": "Accessories",
              "subcategory": [
                {
                  "_id": 161,
                  "_type": "Category",
                  "name": "Stylus"
                }
              ]
            }
          ]
        }
      ],
  ...
}

【讨论】:

    猜你喜欢
    • 2013-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-18
    • 2019-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多