检查图是否存在

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

语法

检查目录中是否存在图
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 图不存在。