如何从节点找到所有路径并按关系属性筛选?

huangapple go评论63阅读模式
英文:

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 have a graph like this:
如何从节点找到所有路径并按关系属性筛选?

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)

huangapple
  • 本文由 发表于 2023年1月9日 19:12:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/75056449.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定