How to access Relationship value from returned results


(PankajM) #1

I have a python code quering Neo4j REST API, so when run this code

q = ("MATCH (n:Person)-[rel]->(a:Address), RETURN rel 
results = self.db.query(q, returns=(client.Relationship))
    print(results[0][1])

The output is the URL below http://localhost:7474/db/data/relationship/3602>

After clicking the URL, I can see the correct relationship info listed below in a browser. and I am trying to extract the value of "type" which is "RESIDENT_Of"

Relationship

Properties

Relationship info

type RESIDENT_Of

start http://localhost:7474/db/data/node/1066

end http://localhost:7474/db/data/node/34804

But when I change the code to

print(results[0][1]['type') I get following error:

KeyError: 'type'

I have tried all combinations such as below but still keep getting the same error.

results[0][1]['Relationship info]['type']

results[0][1]['Properties']['Relationship info]['type']

Any idea what key I should be using on results to get the string description of relationship type?


(Michael Hunger) #2

Don't do that. The REST API will go away.

Use a proper driver to run queries against your database, see

https://community.neo4j.com/t/about-the-python-category/39/2


(Nigel Small) #3

As @michael.hunger says, the REST API will be disappearing in Neo4j 4.0; it is being deprecated in 3.5.

If you run the Cypher through the official driver instead, you will need something like the following:

from neo4j.v1 import GraphDatabase

driver = GraphDatabase.driver("bolt://localhost:7687", auth=("neo4j", "password"))

with driver.session() as session:
    result = session.run("MATCH (n:Person)-[rel]->(a:Address) RETURN rel")
    for record in result:
        rel = record["rel"]
        print(rel.type)

Alternatively, if you're just working with graphy types (nodes and relationships) in your results, and ordering doesn't matter, you can jump into the result graph:

    # ...skipping several lines
    graph = session.run("MATCH (n:Person)-[rel]->(a:Address) RETURN rel").graph()
    for rel in graph.relationships:
        print(rel.type)