1
votes

How can I can get the type of the relationship with a cypher query in py2neo? I have this piece of code that works.

def print_row(row):
    a,b = row
    print (a["name"] + " " + b["name"])

cypher.execute(graph_db, "START a=node(1) MATCH (a) - [] - (b)  RETURN a,b", row_handler=print_row) 

This way I can print out the nodes that are connected to my input node (1).

ROCK PAPER
ROCK SCISSORS

However what I would like is to print the type of relationship that these nodes have.

For instance:

ROCK BEATS SCISSORS
ROCK BEATEN_BY PAPER

What I tried (and failed) is as follows:

def print_row(row):
    a,b,r = row
    print (a["name"] + r["type"] + b["name"])

cypher.execute(graph_db,"START a=node(1) MATCH (a) -[r]->(b) RETURN a,b,r", row_handler=print_row)

How can I do this with py2neo?

1

1 Answers

2
votes

You need to use the Cypher TYPE function (http://docs.neo4j.org/chunked/milestone/query-functions-scalar.html#functions-type). Your code will then look something like this:

def print_row(row):
    a, r_type, b = row
    print(a["name"] + " " + r_type + " " + b["name"])

cypher.execute(graph_db, "START a=node(1) MATCH (a)-[r]->(b) RETURN a, TYPE(r), b", row_handler=print_row)