Py2neo:查找关系并返回节点

时间:2015-01-12 14:00:33

标签: python neo4j py2neo

如何在不使用graph.cypher.exe的neo4j web模块中的电影图形示例中执行此Cypher查询(或获取graph.cypher.execute以返回一组节点而不是丑陋的返回 - 它有的字符串)

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

我想要的是:

(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"})

其中a [0]给出tom hanks节点,a [1]表示关系,a [2]表示电影。


编辑:添加了“错误的”示例输出

>>> 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 个答案:

答案 0 :(得分:2)

我认为你发现execute实际上没有返回一个字符串?您在a中看到的内容是文档中指定的RecordList的表示形式:

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

a[0]然后会在Record中为您提供第一个RecordList。然后可以通过名称访问Record中的值,例如a[0]['tomHanksMovies']

Record个对象的详细信息在这里:

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

答案 1 :(得分:1)

如果您已经结婚使用py2neo,您可以做的就是稍微修改您的查询以返回您想要的所有信息。例如,

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

这应该做的是在列表中给出结果,就像你说的那样你不想要的结果。但是,从这里你可以索引结果以获得你喜欢的每个部分。例如,a [0]会给你第一行结果,[0] [0]会给你第一行结果的人节点,[0] [0] [0]会给你第一行中第一个节点的第一个属性,等等。从这里你可以运行一个for循环来将结果组织成一个你更感兴趣的表格。

希望这有帮助。