如何从 py2neo 图形查询 return 节点?

How to return node from py2neo graph query?

我如何使用结果并将其转换为 graph.run 的节点或关系,例如

result = graph.run('match (x:Person) return x')

如何将结果转换为 Py2neo v3 node/relationship?

对于 Py2neo 1.6.7,您可以使用 GraphDatabaseService 的方法 find 遍历一组标记节点,可选择按 属性 键和值进行过滤,例如:

from py2neo import neo4j
# pass the base URI of your neo4j database as arg
graph_db = neo4j.GraphDatabaseService('http://localhost:7474/db/data/') 
resultset = graph_db.find('Person')

对于 Py2neo 3.0,您可以使用 Graph 的方法 find 生成具有给定标签的所有 节点 ,可选择按 属性键和值。因此,对于您的示例:

from py2neo import Graph
graph_db = Graph("http://localhost:7474/db/data/")
resultset = graph_db.find('Person')

最后,来自官方文档的重要警告

Note also that Py2neo is developed and tested exclusively under Linux using standard CPython distributions. While other operating systems and Python distributions may work, support for these is not available.

从查询中获取节点的最简单方法是使用 evaluate 而不是 run:

my_node = graph.evaluate('match (x:Person) return x')

此方法returns 从第一条记录返回第一个值。在这种情况下,您的节点。

http://py2neo.org/v3/database.html#py2neo.database.Graph.evaluate

另一种方法是根据cypher.execute返回的生成器生成列表。

result = graph.cypher.execute('match (x:Person) return x')
nodes = [n for n in result]

请注意,这样查询的节点是记录。访问真实节点 select 每个对象的属性 r。 :

l_nodes = map(lambda node : node.r, nodes)