检查图是否存在

我们可以通过查找图的名称来检查该图是否存储在目录中。

语法

检查图是否存在于目录中
CALL gds.graph.exists(graphName: String) YIELD
  graphName: String,
  exists: Boolean
表 1. 参数
名称 类型 可选 描述

graphName

字符串

图在目录中存储时所使用的名称。

表 2. 结果
名称 类型 描述

graphName

字符串

已删除图的名称。

exists

布尔值

如果该图存在于图目录中。

此外,除了该过程外,我们还提供了一个函数,可直接从过程中返回 exists(存在)字段。

检查图是否存在于目录中
RETURN gds.graph.exists(graphName: String)::Boolean

示例

以下所有示例应在空数据库中运行。

这些示例以 Cypher 投影 为准。原生投影将在未来的版本中弃用。

为了演示 GDS 图存在性检查功能,我们将在 Neo4j 中创建一个小型社交网络图,并将其投影到我们的图目录中。

以下 Cypher 语句将在 Neo4j 数据库中创建示例图:
CREATE
  (florentin:Person { name: 'Florentin', age: 16 }),
  (adam:Person { name: 'Adam', age: 18 }),
  (veselin:Person { name: 'Veselin', age: 20 }),
  (florentin)-[:KNOWS { since: 2010 }]->(adam),
  (florentin)-[:KNOWS { since: 2018 }]->(veselin)
投影 Person 节点和 KNOWS 关系
MATCH (n:Person)-[r:KNOWS]->(m:Person)
RETURN gds.graph.project('persons', n, m)

过程

检查图是否存在于目录中
UNWIND ['persons', 'books'] AS graph
CALL gds.graph.exists(graph)
  YIELD graphName, exists
RETURN graphName, exists
表 3. 结果
graphName exists

"persons"

true

"books"

false

我们可以验证已投影的 persons 图是否存在,而 books 图不存在。

函数

作为过程的替代方案,我们也可以使用相应的函数。与过程不同,函数可以嵌入到其他 Cypher 语句中,例如 RETURNWHERE

检查图是否存在于目录中
RETURN gds.graph.exists('persons') AS personsExists, gds.graph.exists('books') AS booksExists
表 4. 结果
personsExists booksExists

true

false

与之前一样,我们可以验证已投影的 persons 图是否存在,而 books 图不存在。