简体   繁体   English

Py2neo:查找关系并返回节点

[英]Py2neo: Find relations and return nodes

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) 你如何从py2neo中的neo4j web模块中的电影图形示例中执行此Cypher查询而不使用graph.cypher.execute(或者获取graph.cypher.execute以返回一组节点而不是它具有的丑陋的返回字符串)

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. 其中a [0]给出tom hanks节点,a [1]表示关系,a [2]表示电影。


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'>

I presume you realise that execute doesn't actually return a string? 我假设你意识到execute实际上并没有返回一个字符串? What you see there in a is a representation of a RecordList as specified in the docs: 你在a看到a是文档中指定的RecordList的表示:

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

a[0] then gives you the first Record in that RecordList . a[0]然后给你RecordList的第一个Record Values in that Record can then be accessed by name, eg a[0]['tomHanksMovies'] . 然后可以通过名称访问该Record值,例如a[0]['tomHanksMovies']

The details for Record objects are here: Record对象的详细信息如下:

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

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. 如果您已经结婚使用py2neo,您可以做的是稍微修改您的查询以返回您想要的所有信息。 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. 例如,a [0]会给你第一行结果,[0] [0]会给你第一行结果的人节点,[0] [0] [0]会给你第一行中第一个节点的第一个属性,等等。从这里你可以运行一个for循环来将结果组织成一个你更感兴趣的形式。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM