Py2neo:查找关系和 return 节点
Py2neo: Find relations and return nodes
在不使用 graph.cypher.execute(或获取 graph.cypher.execute 到 return 的一组节点而不是丑陋的 return-字符串)
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] 给出了电影。
编辑:添加了"wrong"示例输出
>>> 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'>
如果您已经习惯使用 py2neo,您可以稍微修改您的查询以 return 您想要的所有信息。例如,
a = graph.cypher.execute("MATCH (a:Person {name:"Tom Hanks"})-[acted:ACTED_IN]->(movies:Movie) RETURN a, acted, movies")
它应该做的是在列表中给你结果,就像你说你不想要的那样。但是,从这里您可以为结果编制索引以获得您想要的每个部分。例如,a[0] 将为您提供第一行结果,a[0][0] 将为您提供第一行结果中的人员节点,a[0][0][0] 将为您提供第一行第一个节点的第一个 属性,等等。从这里您可以 运行 一个 for 循环将结果组织成您更感兴趣的形式。
希望对您有所帮助。
我想您已经意识到 execute
实际上 return 不是一个字符串?您在 a
中看到的是文档中指定的 RecordList
的表示形式:
http://py2neo.org/2.0/cypher.html#py2neo.cypher.CypherResource.execute
a[0]
然后给你 RecordList
中的第一个 Record
。然后可以通过名称访问 Record
中的值,例如a[0]['tomHanksMovies']
.
Record
个对象的详细信息在此处:
在不使用 graph.cypher.execute(或获取 graph.cypher.execute 到 return 的一组节点而不是丑陋的 return-字符串)
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] 给出了电影。
编辑:添加了"wrong"示例输出
>>> 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'>
如果您已经习惯使用 py2neo,您可以稍微修改您的查询以 return 您想要的所有信息。例如,
a = graph.cypher.execute("MATCH (a:Person {name:"Tom Hanks"})-[acted:ACTED_IN]->(movies:Movie) RETURN a, acted, movies")
它应该做的是在列表中给你结果,就像你说你不想要的那样。但是,从这里您可以为结果编制索引以获得您想要的每个部分。例如,a[0] 将为您提供第一行结果,a[0][0] 将为您提供第一行结果中的人员节点,a[0][0][0] 将为您提供第一行第一个节点的第一个 属性,等等。从这里您可以 运行 一个 for 循环将结果组织成您更感兴趣的形式。
希望对您有所帮助。
我想您已经意识到 execute
实际上 return 不是一个字符串?您在 a
中看到的是文档中指定的 RecordList
的表示形式:
http://py2neo.org/2.0/cypher.html#py2neo.cypher.CypherResource.execute
a[0]
然后给你 RecordList
中的第一个 Record
。然后可以通过名称访问 Record
中的值,例如a[0]['tomHanksMovies']
.
Record
个对象的详细信息在此处: