gpt4 book ai didi

neo4j - 密码查询 : Return type of relationship of paths with variable length

转载 作者:行者123 更新时间:2023-12-04 22:04:53 25 4
gpt4 key购买 nike

用户,我正在尝试接收所有路径(例如,长度 < 3),打印节点标题,还有关系类型。

使用:

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)

但是随着路径长度的增加,我会为每个路径长度执行一个查询。

我试过:

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)

-> 期望 Collection 但却是 Path

有没有人有提示?

来自 Neo4j 接口(interface)的附加信息 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": {}
},

最佳答案

您可以简单地使用 extract 来提取路径内的关系类型。

基于 http://console.neo4j.org 上的简单电影图:

MATCH p=(:Crew { name:"Neo" })-[r*3]-()
RETURN p, extract(x IN rels(p)| type(x)) AS types

您的查询将是:

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

这将返回一个类型的集合:

[爱,知道,知道]

所以你可以有重复项。如果您需要对它们进行重复数据删除,请使用 UNWIND 和 distinct :

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

关于neo4j - 密码查询 : Return type of relationship of paths with variable length,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33802597/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com