neo4j Cypher 分层树构建对 JSON 的响应 [英] neo4j Cypher hierarchical tree build response to JSON

查看:37
本文介绍了neo4j Cypher 分层树构建对 JSON 的响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Can you help me to build cypher query? i have following graph db structure:

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

有了这个图数据,我就有了深层次的层次树.

With this graph data i have hierarchical tree with deep level.

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

I found following code on Stackoverfllow.com and changed for my data:

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 响应:

But he is build response only for depth with 3 levels of tree. I need bigger. I'm try to build json response like this example:

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

Cypher 可以进行递归调用吗?我认为这样会更好.

The Cypher can make recursive calls? I think this will be better.

谢谢.

附言我知道在 SO 上也有类似的问题,但它们对我没有帮助.

P.S. I know there are similar questions on SO, but they did not help me.

推荐答案

Cypher 不太适合在叶子处于任意深度时转储树结构中的图数据.

Cypher is not well suited for dumping out graph data in a tree structure when leaves are at arbitrary depths.

但是,使用 neo4j 3.x,如果您能够安装 APOC 插件 并使用 apoc.convert.toTree 程序.

However, with neo4j 3.x, you can get close to what you want if you are able to install the APOC plugin on your server and use the apoc.convert.toTree procedure.

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

First, let's create some sample data:

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 节点的数量.以下是示例结果的片段:

... you will get N result rows, where N is the number of root Category nodes. Here is a snippet of sample results:

{
  ...
      "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"
                }
              ]
            }
          ]
        }
      ],
  ...
}

这篇关于neo4j Cypher 分层树构建对 JSON 的响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆