py2neo、Neo4j中的索引错误
我是py2neo的新手,所以我想我应该从一个简单的程序开始。它返回一个类型错误:索引不可编辑。无论如何,我尝试添加一组节点,然后为它们创建关系,同时避免重复。不知道我做错了什么py2neo、Neo4j中的索引错误,neo4j,py2neo,Neo4j,Py2neo,我是py2neo的新手,所以我想我应该从一个简单的程序开始。它返回一个类型错误:索引不可编辑。无论如何,我尝试添加一组节点,然后为它们创建关系,同时避免重复。不知道我做错了什么 from py2neo import neo4j, cypher graph_db = neo4j.GraphDatabaseService("http://localhost:7474/db/data/") happy = "happy" glad = "glad" mad = "mad" irate = "irat
from py2neo import neo4j, cypher
graph_db = neo4j.GraphDatabaseService("http://localhost:7474/db/data/")
happy = "happy"
glad = "glad"
mad = "mad"
irate = "irate"
love = "love"
wordindex = graph_db.get_or_create_index(neo4j.Node, "word")
for node in wordindex:
wordindex.add("word", node["word"], node)
def createnodes(a, b, c, d, e):
nodes = wordindex.get_or_create(
{"word": (a)},
{"word": (b)},
{"word": (c)},
{"word": (d)},
{"word": (e)},
)
def createrel(a, b, c, d, e):
rels = wordindex.get_or_create(
((a), "is", (b)),
((c), "is", (d)),
((e), "is", (a)),
)
createnodes(happy, glad, mad, irate, love)
createrel(happy, glad, mad, irate, love)
您在这里错误地使用了许多方法,从方法开始。应该使用此方法将现有节点添加到索引中,但此时代码中没有实际创建的节点。我认为您应该使用以下方法:
nodes = []
for word in [happy, glad, mad, irate, love]:
# get or create an entry in wordindex and append it to the `nodes` list
nodes.append(wordindex.get_or_create("word", word, {"word": word}))
graph_db.get_or_create_relationships(
(nodes[0], "is", nodes[1]),
(nodes[2], "is", nodes[3]),
(nodes[4], "is", nodes[0]),
)
这基本上取代了createnodes
函数,因为节点是直接通过索引创建的,保持了唯一性
然后,您可以唯一地创建与通过上述代码以及以下方法获得的节点对象的关系:
nodes = []
for word in [happy, glad, mad, irate, love]:
# get or create an entry in wordindex and append it to the `nodes` list
nodes.append(wordindex.get_or_create("word", word, {"word": word}))
graph_db.get_or_create_relationships(
(nodes[0], "is", nodes[1]),
(nodes[2], "is", nodes[3]),
(nodes[4], "is", nodes[0]),
)
希望这有帮助
奈杰尔谢谢奈杰尔,非常有帮助!我还发现你的幻灯片演示也非常有用