基于Neo4j图数据库的可查询对象持久化和关系框架。
项目描述
基于Neo4j图数据库构建的基于图的可查询对象持久化框架。
构建状态
示例
除了对象,开索还存储图中的类信息。这使我们能够使用Cypher查询实例信息,同时也能回答有关我们类型的问题。
让我们定义一些基本类
from kaiso.attributes import Integer, Outgoing, String, Uuid
from kaiso.types import Entity, Relationship
# define a simple type hierarchy
class Knows(Relationship):
pass
class Animal(Entity):
id = Uuid(unique=True)
name = String()
knows = Outgoing(Knows)
class Carnivore(Animal):
pass
class Herbivore(Animal):
pass
class Penguin(Herbivore):
favourite_ice_cream = String()
class Lion(Carnivore):
n_siblings = Integer()
与任何ORM一样,我们可以在我们的图中创建一些实例并将它们持久化
from kaiso.persistence import Manager
manager = Manager("http://localhost:7474/db/data/")
# create some instances
fred = Penguin(name="Fred")
tom = Lion(name="Tom")
relation = Knows(fred, tom)
manager.save(fred)
manager.save(tom)
manager.save(relation)
使用Neo4j网络界面探索我们的图,我们发现Tom和Fred
然而,除此之外,我们还可以看到图中的类型信息
我们可以利用类型信息在我们的查询中,例如找到所有知道食肉动物的食草动物
MATCH
(Herbivore:PersistableType {id: "Herbivore"}),
(Carnivore:PersistableType {id: "Carnivore"}),
Carnivore <-[:ISA*]-()<-[:INSTANCEOF]-(carnivore),
Herbivore <-[:ISA*]-()<-[:INSTANCEOF]-(herbivore),
(herbivore)-[:KNOWS]->(carnivore)
RETURN
"The herbivore",
herbivore.name,
"knows the carnivore",
carnivore.name;
==> +---------------------------------------------------------------------+ ==> | "The herbivore" | "Fred" | "knows the carnivore" | "Tom" | ==> +---------------------------------------------------------------------+