Neo4j Cypher hierarchical tree-building response to JSON

Can you help me build a cypher query? I have the following db diagram structure:

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

      

With this graph data, I have a deep-level hierarchical tree.

I found the 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 it builds an answer only for depth with three tree levels. I need more. I am trying to build a json response like this example:

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

      

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

Thank.

PS I know there are similar questions on SO but they didn't help me.

+1


source to share


1 answer


Cypher

not suitable for dumping graph data in a tree structure when leaves are at an arbitrary depth.

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

.

First, 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 get N rows of results, where N is the number of root nodes Category

. Here is a snippet of the 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"
                }
              ]
            }
          ]
        }
      ],
  ...
}

      

+4


source







All Articles