如何使用py2neo ogm获取子图
how to get a sub graph by using py2neo ogm
我知道使用 Cypher 查询获取子图很热门。
但是由于我使用 py2neo.ogm 模型。我只想知道如何使用 ogm 获取子图。例如:
class Company(GraphObject):
__primarykey__ = "firm_name"
firm_name = Property()
shareHolder = RelatedFrom("Company", "hold_by")
我已经创建了公司之间的关系。我想获得一家公司的子图。我查了py2neo的文档,好像没有例子。。。
任何人都可以帮忙吗?
最好的问候
源代码(部分复制py2neo v3 ogm doc)产生以下电影片名列表(不包括减号),当运行使用Neo4J社区版和电影示例(:播放电影)
- 有些东西必须付出
- 约翰尼助记符
- 替补
- 黑客帝国重装上阵
- 矩阵革命
- 黑客帝国
- 魔鬼代言人
- 几个好男人
- 阿波罗 13 号
- Frost/Nixon
- 几个好男人
- 支持我
- 几个好男人
- 壮志凌云
杰里·马奎尔
import py2neo
import py2neo.ogm
from py2neo import Graph, Node, Relationship
from py2neo.ogm import GraphObject, Property, RelatedFrom, RelatedTo, RelatedObjects
class Movie(GraphObject):
__primarykey__ = "title"
title = Property()
tag_line = Property("tagline")
released = Property()
actors = RelatedFrom("Person", "ACTED_IN")
directors = RelatedFrom("Person", "DIRECTED")
producers = RelatedFrom("Person", "PRODUCED")
class Person(GraphObject):
__primarykey__ = "name"
name = Property()
born = Property()
acted_in = RelatedTo(Movie)
directed = RelatedTo(Movie)
produced = RelatedTo(Movie)
def authenticateAndConnect():
# Authenticate the user using py2neo.authentication
py2neo.authenticate('localhost:7474', '<username>', '<password>')
# Connect to Graph and get the instance of graph
return Graph('http://localhost:7474/default.graphdb/data/')
def foo():
graph = authenticateAndConnect()
for person in list(Person.select(graph).where("_.name =~ 'K.*'")):
for movie in person.acted_in:
print(movie.title)
if __name__ == '__main__':
foo()
我知道使用 Cypher 查询获取子图很热门。 但是由于我使用 py2neo.ogm 模型。我只想知道如何使用 ogm 获取子图。例如:
class Company(GraphObject):
__primarykey__ = "firm_name"
firm_name = Property()
shareHolder = RelatedFrom("Company", "hold_by")
我已经创建了公司之间的关系。我想获得一家公司的子图。我查了py2neo的文档,好像没有例子。。。 任何人都可以帮忙吗? 最好的问候
源代码(部分复制py2neo v3 ogm doc)产生以下电影片名列表(不包括减号),当运行使用Neo4J社区版和电影示例(:播放电影)
- 有些东西必须付出
- 约翰尼助记符
- 替补
- 黑客帝国重装上阵
- 矩阵革命
- 黑客帝国
- 魔鬼代言人
- 几个好男人
- 阿波罗 13 号
- Frost/Nixon
- 几个好男人
- 支持我
- 几个好男人
- 壮志凌云
杰里·马奎尔
import py2neo import py2neo.ogm from py2neo import Graph, Node, Relationship from py2neo.ogm import GraphObject, Property, RelatedFrom, RelatedTo, RelatedObjects class Movie(GraphObject): __primarykey__ = "title" title = Property() tag_line = Property("tagline") released = Property() actors = RelatedFrom("Person", "ACTED_IN") directors = RelatedFrom("Person", "DIRECTED") producers = RelatedFrom("Person", "PRODUCED") class Person(GraphObject): __primarykey__ = "name" name = Property() born = Property() acted_in = RelatedTo(Movie) directed = RelatedTo(Movie) produced = RelatedTo(Movie) def authenticateAndConnect(): # Authenticate the user using py2neo.authentication py2neo.authenticate('localhost:7474', '<username>', '<password>') # Connect to Graph and get the instance of graph return Graph('http://localhost:7474/default.graphdb/data/') def foo(): graph = authenticateAndConnect() for person in list(Person.select(graph).where("_.name =~ 'K.*'")): for movie in person.acted_in: print(movie.title) if __name__ == '__main__': foo()