Neo4j Cypher查询:长度可变的路径关系的返回类型
用户, 我尝试接收所有路径(例如,长度<3),打印节点标题,还打印关系类型 使用:Neo4j Cypher查询:长度可变的路径关系的返回类型,neo4j,cypher,Neo4j,Cypher,用户, 我尝试接收所有路径(例如,长度
MATCH (p0:Page {title:'New Zealand'}), (p1:Page {title:'Kiwi'}), p = (p0)-[r*..2]-(p1)
RETURN p AS path
打印路径,但关系表示为{}
当我使用*.2时
`RETURN p,type(r)'
不工作(类型不匹配:预期关系,但为集合)
我可以使用以下解决方法:
MATCH
(p0:Page {title:'New Zealand'}),
(p1:Page {title:'Kiwi'}),
p = (p0)-[r]-(p3)-[r1]-(p1)
RETURN p0,type(r),p3,type(r1)
但是随着pathlength的增加,我会对每个pathlength执行一个查询
我试过:
MATCH
(p0:Page {title:'New Zealand'}),
(p1:Page {title:'Kiwi'}),
p = (p0)-[r*..2]-(p1) FOREACH(r in p | RETURN p,p0,type(r),p1)
->应为集合,但为路径
有人有暗示吗
Neo4j接口的JSON输出(代码段)附加信息:
{
"results": [
{
"columns": [
"p",
"rels(p)",
"nodes(p)"
],
"data": [
{
"row": [
[
{
"title": "New Zealand"
},
{},
{
"title": "Category:Birds of New Zealand"
},
{},
{
"title": "Kiwi"
}
],
[
{},
{}
],
[
{
"title": "New Zealand"
},
{
"title": "Category:Birds of New Zealand"
},
{
"title": "Kiwi"
}
]
],
"graph": {
"nodes": [
{
"id": "11120",
"labels": [
"Page"
],
"properties": {
"title": "Kiwi"
}
},
{
"id": "1942858",
"labels": [
"Page"
],
"properties": {
"title": "New Zealand"
}
},
{
"id": "11994493",
"labels": [
"Category"
],
"properties": {
"title": "Category:Birds of New Zealand"
}
}
],
"relationships": [
{
"id": "1070940",
"type": "To_Category",
"startNode": "11120",
"endNode": "11994493",
"properties": {}
},
使用
rels
功能是否有帮助
MATCH (p0:Page {title:'New Zealand'}), (p1:Page {title:'Kiwi'}), p = (p0)-[r*..2]-(p1)
RETURN p, rels(p), nodes(p)
您可以简单地使用
extract
来提取路径中的关系类型
基于上的简单电影图:
您的查询将是:
MATCH (p0:Page {title:'New Zealand'}), (p1:Page {title:'Kiwi'}),
p=(p0)-[r*..2]-(p1)
RETURN p, extract (rel in rels(p) | type(rel) ) as types
这将返回以下类型的集合:
[爱,知道,知道]
所以你可以有重复的。如果需要消除重复,请使用“展开”和“区分”:
MATCH (p0:Page {title:'New Zealand'}), (p1:Page {title:'Kiwi'}),
p=(p0)-[r*..2]-(p1)
WITH p, extract (rel in rels(p) | type(rel) ) as types
UNWIND types as t
RETURN p, collect(distinct t) as types
更新
我对最后一个不太满意,所以这里有一个更简单的方法:
MATCH (p0:Page {title:'New Zealand'}), (p1:Page {title:'Kiwi'}),
p=(p0)-[r*..2]-(p1)
UNWIND rels(p) as rel
RETURN p, collect(distinct type(rel)) as types
谢谢你的快速回复。不幸的是,它具有相同的效果:rels(p)[(空),(空)];列:[p,rels(p),nodes(p)]当前行0:[[{title=newzealand},{},{title=Category:Birds of newzealand},{},{},{title=Kiwi}],{},{title=newzealand},{title=Category:Birds of newzealand},{title=Kiwi}]。也许我在构建图表时做错了什么(?)
MATCH (p0:Page {title:'New Zealand'}), (p1:Page {title:'Kiwi'}),
p=(p0)-[r*..2]-(p1)
UNWIND rels(p) as rel
RETURN p, collect(distinct type(rel)) as types