4

我有一个图表,其中包含无限级别的类别和子类别以及子子类别。如何在一个密码查询中获取所有这些分层数据?

我目前有这个查询:

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

这给了我以下结果:

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

我正在使用 node-neo4j。我将以 json 格式举例说明我想要的内容。

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

我这可能吗?我知道我总是可以通过编程或使用多个查询来完成它。但如果它可以在单个查询中完成,那将非常有帮助。

4

1 回答 1

3

*您可以通过在关系类型之后添加 a 来匹配任意深度的路径:

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

或者,您还可以指定最小和/或最大路径长度:

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

请参阅此处的参考:

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

于 2013-04-23T14:10:32.063 回答