Goofball
Goofball

Reputation: 755

py2neo command in neo4j BOLT driver

I have a command written in python using the py2neo to access the name of an exchange. This works.

graph = Graph()
stmt = 'MATCH (i:Index{uniqueID: 'SPY'})-[r:belongsTo]->(e:Exchange) RETURN e.name'
exchName = graph.cypher.execute(stmt)[0][0]

Can this be converted to a BOLT neo4j-driver statement? I always get an error. I want to avoid an iterator statement where I loop through the StatementResult.

driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j"))
session = driver.session()
stmt = 'MATCH (i:Index{uniqueID: 'SPY'})-[r:belongsTo]->(e:Exchange) RETURN e.name'
exchName = session.run(stmt)[0][0]
TypeError: 'StatementResult' object is not subscriptable

Upvotes: 0

Views: 854

Answers (1)

Martin Preusse
Martin Preusse

Reputation: 9369

Try to store the results of session.run() in a list to retain them:

driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j"))
session = driver.session()
stmt = 'MATCH (i:Index{uniqueID: 'SPY'})-[r:belongsTo]->(e:Exchange) RETURN e.name'

# transform to list to retain result
exchName = list(session.run(stmt))[0][0]

See the docs: http://neo4j.com/docs/developer-manual/current/#result-retain

Upvotes: 1

Related Questions