Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

neo4j Cypher hierarchical tree build response to 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.

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)}

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

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

Thanks.

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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'});

Then with this query:

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...