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

查看:241
本文介绍了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.

谢谢.

P.S.我知道在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.

但是,如果您能够安装服务器上的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天全站免登陆