过滤
语法
gds.graph.filter()
过程投影一个新图CALL gds.graph.filter(
graphName: String,
fromGraphName: String,
nodeFilter: String,
relationshipFilter: String,
configuration: Map
) YIELD
graphName: String,
fromGraphName: String,
nodeFilter: String,
relationshipFilter: String,
nodeCount: Integer,
relationshipCount: Integer,
projectMillis: Integer
名称 | 类型 | 描述 |
---|---|---|
graphName |
字符串 |
存储在图目录中的新图的名称。 |
fromGraphName |
字符串 |
图目录中原始图的名称。 |
nodeFilter |
字符串 |
用于过滤输入图中节点的 Cypher 谓词。可以使用 |
relationshipFilter |
字符串 |
用于过滤输入图中关系的 Cypher 谓词。可以使用 |
configuration |
映射 |
配置子图创建的其他参数。 |
名称 | 类型 | 默认值 | 可选 | 描述 |
---|---|---|---|---|
concurrency |
整数 |
|
是 |
用于过滤图的并发线程数。 |
jobId |
字符串 |
|
是 |
可以提供的 ID,以便更轻松地跟踪投影的进度。 |
parameters |
映射 |
|
是 |
传递到节点和关系过滤器的用户定义查询参数的映射。 |
名称 | 类型 | 描述 |
---|---|---|
graphName |
字符串 |
存储在图目录中的新图的名称。 |
fromGraphName |
字符串 |
图目录中原始图的名称。 |
nodeFilter |
字符串 |
节点的过滤器谓词。 |
relationshipFilter |
字符串 |
关系的过滤器谓词。 |
nodeCount |
整数 |
子图中节点的数量。 |
relationshipCount |
整数 |
子图中关系的数量。 |
projectMillis |
整数 |
投影子图所用的毫秒数。 |
nodeFilter
和 relationshipFilter
配置键可用于表达过滤器谓词。过滤器谓词是绑定到单个实体的 Cypher 谓词。实体可以是节点或关系。过滤器谓词始终需要计算结果为 true
或 false
。如果节点过滤器计算结果为 true
,则节点包含在子图中。如果关系过滤器计算结果为 true
且其源节点和目标节点包含在子图中,则关系包含在子图中。
谓词是表达式的组合。表达式最简单的形式是字面量。GDS 目前支持以下字面量
-
浮点型字面量,例如
13.37
-
整型字面量,例如
42
-
布尔型字面量,即
TRUE
和FALSE
属性、标签和关系类型表达式绑定到实体。节点实体始终由变量 n
标识,关系实体由 r
标识。使用变量,我们可以引用
-
节点标签表达式,例如
n:Person
-
关系类型表达式,例如
r:KNOWS
-
节点属性表达式,例如
n.age
-
关系属性表达式,例如
r.since
布尔谓词组合两个表达式并返回 true
或 false
。GDS 支持以下布尔谓词
-
大于/小于,例如
n.age > 42
或r.since < 1984
-
大于/小于或等于,例如
n.age >= 42
或r.since ⇐ 1984
-
相等性,例如
n.age = 23
或r.since = 2020
-
逻辑运算符,例如
-
n.age > 23 AND n.age < 42
-
n.age = 23 OR n.age = 42
-
n.age = 23 XOR n.age = 42
-
n.age IS NOT 23
可以在谓词中使用的变量名称不是任意的。节点谓词必须引用变量 n
。关系谓词必须引用变量 r
。
一个例外是度函数,它只返回节点的度。该函数将关系类型作为参数。多个类型被视为析取。例如,degree() > 42
过滤所有关系类型的度大于 42 的节点。表达式 degree('Foo', 'Bar') > 42
过滤 Foo
和 Bar
关系的总和大于 42 的节点。
示例
以下所有示例都应在空数据库中运行。 这些示例使用 Cypher 投影 作为规范。原生投影将在将来的版本中弃用。 |
为了演示 GDS 项目子图功能,我们将创建 Neo4j 中的一个小型社交图。
CREATE
(p0:Person { age: 16 }),
(p1:Person { age: 18 }),
(p2:Person { age: 20 }),
(b0:Book { isbn: 1234 }),
(b1:Book { isbn: 4242 }),
(p0)-[:KNOWS { since: 2010 }]->(p1),
(p0)-[:KNOWS { since: 2018 }]->(p2),
(p0)-[:READS]->(b0),
(p1)-[:READS]->(b0),
(p2)-[:READS]->(b1)
MATCH (n:Person)-[r:KNOWS|READS]->(m:Person|Book)
RETURN gds.graph.project('social-graph', n, m,
{
sourceNodeLabels: labels(n),
targetNodeLabels: labels(m),
sourceNodeProperties: n { .age },
targetNodeProperties: CASE WHEN m:Person THEN m { .age } ELSE {} END,
relationshipType: type(r),
relationshipProperties: CASE WHEN r:KNOWS THEN r { .since } ELSE {} END
}
)
节点过滤
CALL gds.graph.filter(
'teenagers',
'social-graph',
'n.age > 13 AND n.age <= 18',
'*'
)
YIELD graphName, fromGraphName, nodeCount, relationshipCount
graphName | fromGraphName | nodeCount | relationshipCount |
---|---|---|---|
"teenagers" |
"social-graph" |
2 |
1 |
节点度过滤
CALL gds.graph.filter(
'degree-graph',
'social-graph',
'degree() > 2',
'*'
)
YIELD graphName, fromGraphName, nodeCount, relationshipCount
graphName | fromGraphName | nodeCount | relationshipCount |
---|---|---|---|
"degree-graph" |
"social-graph" |
1 |
0 |
节点和关系过滤
CALL gds.graph.filter(
'teenagers',
'social-graph',
'n.age > 13 AND n.age <= 18',
'r.since >= 2012.0'
)
YIELD graphName, fromGraphName, nodeCount, relationshipCount
graphName | fromGraphName | nodeCount | relationshipCount |
---|---|---|---|
"teenagers" |
"social-graph" |
2 |
0 |
二部子图
READS
关系类型连接CALL gds.graph.filter(
'teenagers-books',
'social-graph',
'n:Book OR n:Person',
'r:READS'
)
YIELD graphName, fromGraphName, nodeCount, relationshipCount
graphName | fromGraphName | nodeCount | relationshipCount |
---|---|---|---|
"teenagers-books" |
"social-graph" |
5 |
3 |
二部图节点过滤
CALL gds.graph.filter(
'teenagers-books',
'social-graph',
'n:Book OR (n:Person AND n.age > 18)',
'r:READS'
)
YIELD graphName, fromGraphName, nodeCount, relationshipCount
graphName | fromGraphName | nodeCount | relationshipCount |
---|---|---|---|
"teenagers-books" |
"social-graph" |
3 |
1 |
使用查询参数
CALL gds.graph.filter(
'teenagers-parameterized',
'social-graph',
'n.age > $lower AND n.age <= $upper',
'*',
{ parameters: { lower: 13, upper: 18 } }
)
YIELD graphName, fromGraphName, nodeCount, relationshipCount
graphName | fromGraphName | nodeCount | relationshipCount |
---|---|---|---|
"teenagers-parameterized" |
"social-graph" |
2 |
1 |