|| apoc.path.elements - APOC 核心文档 - Neo4j 文档

apoc.path.elements

详情

语法

apoc.path.elements(path)

描述

将给定的 PATH 转换为 LIST<NODE | RELATIONSHIP>

参数

名称

类型

描述

path

PATH

要转换为节点和关系列表的路径。

返回

LIST<ANY>

使用示例

本节中的示例基于以下示例图

MERGE (manUtd:Club {name: 'Man Utd'})
MERGE (juventus:Club {name: 'Juventus'})
MERGE (flamengo:Club {name: 'Flamengo'})

MERGE (premierLeague:League {name: 'Premier League'})
MERGE (serieA:League {name: 'Serie A'})
MERGE (brasileirao:League {name: 'Brasileirão'})

MERGE (england:Country {name: 'England'})
MERGE (brazil:Country {name: 'Brazil'})

MERGE (uefa:Confederation {name: 'UEFA'})

MERGE (manUtd)-[:IN_LEAGUE]->(premierLeague)
MERGE (premierLeague)-[:IN_COUNTRY]->(england)
MERGE (england)-[:IN_CONFEDERATION]->(uefa)

MERGE (juventus)-[:IN_LEAGUE]->(serieA)

MERGE (flamengo)-[:IN_LEAGUE]->(brasileirao)
MERGE (brasileirao)-[:IN_COUNTRY]->(brazil);

apoc.path.elements 函数将路径转换为节点和关系列表。

以下示例使用 APOC 和 Cypher 返回 (club)-[:IN_LEAGUE]→(league)-[:IN_COUNTRY]→(country) 路径中的实体列表

apoc.path.elements
MATCH path = (club:Club)-[:IN_LEAGUE]->(league)-[:IN_COUNTRY]->(country)
RETURN path, apoc.path.elements(path) AS list
使用 Cypher 的 nodes()、relationships() 和 reduce() 函数
MATCH path = (club:Club)-[:IN_LEAGUE]->(league)-[:IN_COUNTRY]->(country)
WITH path, nodes(path) AS nodes, relationships(path) AS rels
RETURN path, reduce(acc=[nodes[0]], i IN range(0,size(rels)-1) | acc + rels[i] + nodes[i+1]) AS list
结果
path 列表

(:Club {name: "Man Utd"})-[:IN_LEAGUE]→(:League {name: "Premier League"})-[:IN_COUNTRY]→(:Country {name: "England"})

[(:Club {name: "Man Utd"}), [:IN_LEAGUE], (:League {name: "Premier League"}), [:IN_COUNTRY], (:Country {name: "England"})]

(:Club {name: "Flamengo"})-[:IN_LEAGUE]→(:League {name: "Brasileirão"})-[:IN_COUNTRY]→(:Country {name: "Brazil"})

[(:Club {name: "Flamengo"}), [:IN_LEAGUE], (:League {name: "Brasileirão"}), [:IN_COUNTRY], (:Country {name: "Brazil"})]

我们可以使用此函数返回表示路径中包含的节点和关系的三元组流。

以下示例使用 APOC 和 Cypher 返回 (subject, predicate, object) 三元组

apoc.path.elements
MATCH (club:Club)
OPTIONAL MATCH path1 = (club)-[:IN_LEAGUE]->(league)
OPTIONAL MATCH path2 = (league)-[:IN_COUNTRY]->(country)
WITH apoc.path.combine(path1, path2) AS path
WITH apoc.path.elements(path) AS elements
UNWIND range(0, size(elements) - 2) AS index
WITH elements, index WHERE index % 2 = 0
RETURN elements[index] AS subject, elements[index+1] AS predicate, elements[index+2] AS object
使用 Cypher 的 nodes()、relationships() 和 reduce() 函数
MATCH (club:Club)
OPTIONAL MATCH path1 = (club)-[:IN_LEAGUE]->(league)
OPTIONAL MATCH path2 = (league)-[:IN_COUNTRY]->(country)
WITH apoc.path.combine(path1, path2) AS path
WITH path, nodes(path) AS nodes, relationships(path) AS rels
WITH path, reduce(acc=[nodes[0]], i IN range(0,size(rels)-1) | acc + rels[i] + nodes[i+1]) AS elements
UNWIND range(0, size(elements) - 2) AS index
WITH elements, index WHERE index % 2 = 0
RETURN elements[index] AS subject, elements[index+1] AS predicate, elements[index+2] AS object
结果
主语 谓语 宾语

(:Club {name: "Man Utd"})

[:IN_LEAGUE]

(:League {name: "Premier League"})

(:League {name: "Premier League"})

[:IN_COUNTRY]

(:Country {name: "England"})

(:Club {name: "Juventus"})

[:IN_LEAGUE]

(:League {name: "Serie A"})

(:Club {name: "Flamengo"})

[:IN_LEAGUE]

(:League {name: "Brasileirão"})

(:League {name: "Brasileirão"})

[:IN_COUNTRY]

(:Country {name: "Brazil"})

© . This site is unofficial and not affiliated with Neo4j, Inc.