英文:
How to find all paths from node and filter by relationship property?
问题
我想要查找从给定节点开始的所有路径,其中包括关系属性(每个节点都有参数id
)。关系RELATED_TO
具有属性type
。
从10到1的逻辑(通过3):10 ->(类型:type1)5 ->(类型:type1)3 ->(类型:type1)1
从10到1的逻辑(通过2):10 ->(类型:type1)5 ->(类型:type1)2 ->(类型:type2)1;
示例:
- 节点.id:
10
- 关系.type:
type1
期望输出:10 -> 5 -> 3 -> 1 和 10 -> 5 -> 2
如何编写Cypher查询?
英文:
I want to find all path from given node with relationship property(each node has param id
). Relationship RELATED_TO
has property type
.
The logic from 10 to 1(through 3): 10 ->(type: type1) 5 ->(type: type1) 3 ->(type: type1) 1
The logic from 10 to 1(through 2): 10 ->(type: type1) 5 ->(type: type1) 2 ->(type: type2) 1;
Example:
- node.id:
10
- rel.type:
type1
Expected output: 10 -> 5 -> 3 -> 1 and 10 -> 5 -> 2
How to write the cypher?
答案1
得分: 1
假设你通过rel.type
指的是在你的RELATED_TO
关系上有一个type
属性,并且你想要确保该值为type1
,同时也假设当你说node:10
时,意思是具有id为10
的节点,你可以使用以下查询来实现:
MATCH path=(n)-[r:RELATED_TO* {type: 'type1'}]->(o)
WHERE id(n) = 10
RETURN path
英文:
Supposing by rel.type
you mean there is a type
property on your RELATED_TO
relationships and you want to ensure the value is type1
and also assuming that when you mean node:10
it means the node with id 10
, you can do it with the following query :
MATCH path=(n)-[r:RELATED_TO* {type: 'type1'}]->(o)
WHERE id(n) = 10
RETURN path
答案2
得分: 0
一种选择是使用 APOC 插件:
MATCH (n:Node{key: 10})
CALL apoc.path.expandConfig(n, {
relationshipFilter: "RELATED_TO>"
})
YIELD path
RETURN nodes(path)
英文:
One option is to use APOC plugin:
MATCH (n:Node{key: 10})
CALL apoc.path.expandConfig(n, {
relationshipFilter: "RELATED_TO>"
})
YIELD path
RETURN nodes(path)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论