1
votes

how would you do this Cypher query from the movie graph example in neo4j web module work in py2neo without using the graph.cypher.execute (or get graph.cypher.execute to return a set of nodes instead of the ugly return-string it has)

MATCH (tom:Person {name: "Tom Hanks"})-[:ACTED_IN]->(tomHanksMovies) RETURN tom,tomHanksMovies

What I want is something like:

(n4358:Person {born:1956,name:"Tom Hanks"}, {'PLAYED_IN', 'year': 1990}, n4354:Movie {released:1998,tagline:"At odds in life... in love on-line.",title:"You've Got Mail"})

where a[0] gives the tom hanks node, a[1] the relation and a[2] gives the movie.


EDIT: Added "wrong" example-output

>>> print('List all Tom Hanks movies...')
>>> a = graph.cypher.execute('MATCH (tom:Person {name: "Tom Hanks"})-[:ACTED_IN]->(tomHanksMovies) RETURN tomHanksMovies')
>>> print(a)

List all Tom Hanks movies...
    | tomHanksMovies                                                                                                                                                                 
----+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  1 | (n4354:Movie {released:1998,tagline:"At odds in life... in love on-line.",title:"You've Got Mail"})                                                                            
  2 | (n4360:Movie {released:1993,tagline:"What if someone you never met, someone you never saw, someone you never knew was the only someone for you?",title:"Sleepless in Seattle"})
  3 | (n4365:Movie {released:1990,tagline:"A story of love, lava and burning desire.",title:"Joe Versus the Volcano"})                                                               
  4 | (n4372:Movie {released:1996,tagline:"In every life there comes a time when that thing you dream becomes that thing you do",title:"That Thing You Do"})                         
  5 | (n4392:Movie {released:2012,tagline:"Everything is connected",title:"Cloud Atlas"})                                                                                            
  6 | (n4398:Movie {released:2006,tagline:"Break The Codes",title:"The Da Vinci Code"})                                                                                              
  7 | (n4417:Movie {released:1999,tagline:"Walk a mile you'll never forget.",title:"The Green Mile"})                                                                                
  8 | (n4431:Movie {released:1995,tagline:"Houston, we have a problem.",title:"Apollo 13"})                                                                                          
  9 | (n4437:Movie {released:2000,tagline:"At the edge of the world, his journey begins.",title:"Cast Away"})                                                                        
 10 | (n4446:Movie {released:2007,tagline:"A stiff drink. A little mascara. A lot of nerve. Who said they couldn't bring down the Soviet empire.",title:"Charlie Wilson's War"})     
 11 | (n4448:Movie {released:2004,tagline:"This Holiday Season… Believe",title:"The Polar Express"})                                                                                 
 12 | (n4449:Movie {released:1992,tagline:"Once in a lifetime you get a chance to do something different.",title:"A League of Their Own"})   


>>> print(a[0])

 tomHanksMovies                                                                                     
-----------------------------------------------------------------------------------------------------
 (n4354:Movie {released:1998,tagline:"At odds in life... in love on-line.",title:"You've Got Mail"})

>>> print(type(a[0]))

<class 'py2neo.cypher.core.Record'>
2
Well what does it return?Bhargav Rao
see edit. It that what you asked for?Thomas Repsdorph
Oh holy crap. The output is damn screwed. Thank god I use neo4j api instead of py2neo. Even I'm waitin for the answer now .:(Bhargav Rao
@BhargavRao Yes.. I would prefer to use Cypher all the time, but haven't found out how to get a nice and workable object back yet.. :-( But do you use Neo4j with python to?Thomas Repsdorph
I've written a paper with all background work in py using neo4j. The api I used was neo4jBhargav Rao

2 Answers

2
votes

I presume you realise that execute doesn't actually return a string? What you see there in a is a representation of a RecordList as specified in the docs:

http://py2neo.org/2.0/cypher.html#py2neo.cypher.CypherResource.execute

a[0] then gives you the first Record in that RecordList. Values in that Record can then be accessed by name, e.g. a[0]['tomHanksMovies'].

The details for Record objects are here:

http://py2neo.org/2.0/cypher.html#records

1
votes

If you're married to using py2neo what you can do is modify your query ever so slightly to return all the info you want. For example,

a = graph.cypher.execute("MATCH (a:Person {name:"Tom Hanks"})-[acted:ACTED_IN]->(movies:Movie) RETURN a, acted, movies")

What this should do is give you the results in a list, much like the one you said you don't want. But, from here you can index the results to get each part that you'd like. For example, a[0] will give you the first row of results, a[0][0] will give you the person node from the first row of results, a[0][0][0] will give you the first property of the first node from the first row, etc. From here you could run a for loop to organize the results into a form you're more interested in.

Hope this helps.