4

I have a graph which has categories and sub-categories and sub-sub-categories to indefinite level. How I can I get all this hierarchical data in one cipher query?

I currently have this query :

START category=node:categoryNameIndex(categoryName = "category") 
MATCH path = category <-        [rel:parentCategory] - subcategory 
RETURN category, collect(subcategory);

Which gives me following result:

| category                                                                                               | collect(subcategory)                                                                                                                                                                                           |
==> +-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
==> | Node[26]{categoryName:"Test2",categoryDescription:"testDesc",imageUrl:"testUrl",imageName:"imageName"} | [Node[25]{categoryName:"Test1",categoryDescription:"testDesc",imageUrl:"testUrl",imageName:"imageName"}]                                                                                                       |
==> | Node[1]{categoryName:"Test1",categoryDescription:"testDesc",imageUrl:"testUrl",imageName:"imageName"}  | [Node[26]{categoryName:"Test2",categoryDescription:"testDesc",imageUrl:"testUrl",imageName:"imageName"},Node[2]{categoryName:"Test2",categoryDescription:"testDesc",imageUrl:"testUrl",imageName:"imageName"}] |

I am using node-neo4j. I will give an example of what I want in json format.

[{
    "categoryName": "Test2",
    "categoryDescription": "testDesc",
    "imageUrl": "testUrl",
    "children": [{
        "categoryName": "Test1",
        "categoryDescription": "testDesc",
        "imageUrl": "testUrl",
        "children" :  [{
            "categoryName": "Test1",
            "categoryDescription": "testDesc",
            "imageUrl": "testUrl"
        }]
    }]
}]

I this possible? I know I can always do it programmatically OR by using multiple queries. But it will very helpful if it can be done in a single query.

sthzg
  • 5,514
  • 2
  • 29
  • 50
RohanJ
  • 261
  • 3
  • 12

1 Answers1

3

You can match paths of arbitrary depth by adding a * after the relationship type:

START category=node:categoryNameIndex(categoryName = "category") 
MATCH path = category <-[rel:parentCategory*]- subcategory 
RETURN category, collect(subcategory);

Optionally, you can also specify a minimum and/or maximum path length:

START category=node:categoryNameIndex(categoryName = "category") 
MATCH path = category <-[rel:parentCategory*2..5]- subcategory 
RETURN category, collect(subcategory);

See reference here:

http://docs.neo4j.org/chunked/milestone/query-match.html#match-variable-length-relationships

ean5533
  • 8,884
  • 3
  • 40
  • 64
  • Thank you @ean5533. I will elaborate my question for better understanding. – RohanJ Apr 24 '13 at 04:30
  • Does this answer not answer your question? It is a single query. – ean5533 Apr 24 '13 at 15:59
  • No it does answer my question. If you check JSON example that I have given, I want it in a _hierarchical format_. I am currently doing this be firing multiple queries. I don't know whether this is even possible, but if it then that will be very helpful. – RohanJ Apr 26 '13 at 06:25
  • It seems there is no way doing what I want in a single query. I just wanted to avoid firing multiple queries. But first query from your answer was useful, thanks for that. – RohanJ Aug 17 '13 at 08:55