虚拟节点/关系

虚拟节点和关系在图中并不实际存在,它们仅作为图投影返回给 UI/用户进行表示。它们可以被可视化或以其他方式处理。请注意,它们的 ID 为负值。

函数概述

限定名称 类型

apoc.create.vNode
CALL apoc.create.vNode(['Label'], {key:value,…​}) YIELD node - 返回一个虚拟节点

过程

apoc.create.vNode
apoc.create.vNode(['Label'], {key:value,…​}) - 函数返回一个虚拟节点

函数

apoc.create.vNodes
apoc.create.vNodes(labels LIST<STRING>, props LIST<MAP<STRING, ANY>>) - 返回虚拟 NODE 值。

过程

apoc.create.virtual.fromNode
apoc.create.virtual.fromNode(node, [propertyNames]) - 函数返回一个根据现有节点构建的虚拟节点,只包含请求的属性

函数

apoc.create.vRelationship
CALL apoc.create.vRelationship(nodeFrom,'KNOWS',{key:value,…​}, nodeTo) YIELD rel - 返回一个虚拟关系

过程

apoc.create.vRelationship
apoc.create.vRelationship(nodeFrom,'KNOWS',{key:value,…​}, nodeTo) YIELD rel - 返回一个虚拟关系

函数

apoc.create.virtualPath
apoc.create.virtualPath(labelsN LIST<STRING>,n MAP<STRING, ANY>, relType STRING, props MAP<STRING, ANY>, labelsM LIST<STRING>, m MAP<STRING, ANY>) - 返回一个虚拟 PATH

过程

apoc.create.clonePathToVirtual

过程

apoc.create.clonePathsToVirtual

过程

虚拟节点/关系示例

虚拟节点和虚拟关系 vNodevRelationship

从这个简单的数据集

CREATE (from:Account), (to:Account)
WITH from, to
CREATE (from)-[:SENT]->(:Payment {amount: 250})-[:RECEIVED]->(to)
CREATE (from)-[:SENT]->(:Payment {amount: 750})-[:RECEIVED]->(to)

我们可以这样做

聚合关系的简单示例
MATCH (from:Account)-[:SENT]->(p:Payment)-[:RECEIVED]->(to:Account)
RETURN from, to, apoc.create.vRelationship(from,'PAID',{amount:sum(p.amount)},to) as rel;
apoc.create.vRelationship

从这个简单的数据集

CREATE (:Person {country: "Test1"})-[:KNOWS]->(:Person {country: "Test2"}),
(:Person {country: "Foo"})-[:KNOWS]->(:Person {country: "Bar"})

我们可以执行

包含虚拟节点查找的示例,按国家/地区分组的人员
MATCH (p:Person) WITH collect(distinct p.country) as countries
WITH [cName IN countries | apoc.create.vNode(['Country'],{name:cName})] as countryNodes
WITH apoc.map.groupBy(countryNodes,'name') as countries
MATCH (p1:Person)-[:KNOWS]->(p2:Person)
WITH p1.country as cFrom, p2.country as cTo, count(*) as count, countries
RETURN countries[cFrom] as from, countries[cTo] as to, apoc.create.vRelationship(countries[cFrom],'KNOWS',{count:count},countries[cTo]) as rel;
apoc.create.vRelationshipTwo

这当然使用 apoc.nodes.group 更容易。

从一个简单的数据集

CREATE(a:Person)-[r:ACTED_IN]->(b:Movie)

我们可以创建一个虚拟副本,将标签值作为 name 属性添加

MATCH (a)-[r]->(b)
WITH head(labels(a)) AS l, head(labels(b)) AS l2, type(r) AS rel_type, count(*) as count
CALL apoc.create.vNode([l],{name:l}) yield node as a
CALL apoc.create.vNode([l2],{name:l2}) yield node as b
CALL apoc.create.vRelationship(a,rel_type,{count:count},b) yield rel
RETURN *;
apoc.create.vRelationshipAndvNode
虚拟节点和虚拟关系总是具有负数 ID
vNodeId

虚拟节点也可以从现有节点构建,通过筛选属性来获取它们的子集。在这种情况下,虚拟节点保留原始节点的 ID。

MATCH (node:Person {name:'neo', age:'42'})
return apoc.create.virtual.fromNode(node, ['name']) as person
虚拟路径 virtualPath
CALL apoc.create.virtualPath(['British','Person'],{name:'James', age:28},'KNOWS',{since:2009},['Swedish','Person'],{name:'Daniel', age:30})
apoc.create.virtualPath

我们可以从现有模式创建一个虚拟模式

CREATE(a:Person {name:'Daniel'})-[r:KNOWS]->(b:Person {name:'John'})

从这个数据集我们可以创建一个虚拟模式

MATCH (a)-[r]->(b)
WITH head(labels(a)) AS labelA, head(labels(b)) AS labelB, type(r) AS rel_type, a.name AS aName, b.name AS bName
CALL apoc.create.virtualPath([labelA],{name: aName},rel_type,{since:2009},[labelB],{name: bName}) yield from, rel, to
RETURN *;

要更新虚拟节点,您可以使用 apoc.create.setPropertyapoc.create.setProperties,而对于虚拟关系,则可以使用 apoc.create.setRelPropertyapoc.create.setRelProperties

© . All rights reserved.