aradhyamathur
aradhyamathur

Reputation: 350

How to return node from py2neo graph query?

How can i use the result and convert it to node or relationship from graph.run for eg.

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

How can i convert result to Py2neo v3 node/relationship?

Upvotes: 1

Views: 5909

Answers (3)

Juan
Juan

Reputation: 1041

Another way is to generate a list based on the generator returned by cypher.execute.

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

Do note that nodes queried like this are records. TO access the real nodes select the attribute r of each object. :

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

Upvotes: 0

Nigel Small
Nigel Small

Reputation: 4495

The simplest way to obtain the node from your query is to use evaluate instead of run:

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

This method returns the first value from the first record returned. In this case, your node.

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

Upvotes: 6

floatingpurr
floatingpurr

Reputation: 8559

For Py2neo 1.6.7 you can use GraphDatabaseService's method find that iterates through a set of labelled nodes, optionally filtering by property key and value, e.g.:

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')

For Py2neo 3.0 you can use Graph's method find that yields all nodes with a given label, optionally filtering by property key and value. So, for your example:

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

Finally, an important caveat from official doc:

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.

Upvotes: 0

Related Questions