递归neo4j查询 [英] Recursive neo4j query
问题描述
我有一个图,其中的类别和子类别以及子子类别的级别不确定. 如何在一个密码查询中获得所有这些分层数据?
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?
我目前有这个查询:
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格式给出我想要的示例.
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.
推荐答案
您可以通过在关系类型之后添加*
来匹配任意深度的路径:
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);
请参阅此处的参考:
http://docs.neo4j. org/chunked/milestone/query-match.html#match-variable-length-relationships
这篇关于递归neo4j查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!