我有一小段代码可以在嵌入式 Neo4j 数据库中加载文件。
使用此代码我有两个问题,我找不到解决它的文档。
我正在按照文档的示例创建索引,但是:a) 如何检测索引是否存在?文档解释说,如果索引已经存在,则会返回它,但在我的情况下它会返回错误。
b)当我从索引中获取 a 节点时,出现错误
from neo4j import GraphDatabase, INCOMING, Evaluation
# Create a database
db = GraphDatabase("c:/temp/graph")
with db.transaction:
# Create an index for "users" nodes
# to look for them using some of the properties
# HERE I GET AN ERROR WHEN THE INDEX EXISTS PREVIOUSLY, BUT THE DOCUMENTATION EXPLAINS THE OPOSITE.
users_idx = db.node.indexes.create('users')
# Create the "users_reference" node to connect all "users" nodes to here
users_reference = db.node()
db.reference_node.USERS(users_reference, provider='lucene', type='fulltext')
'''
Content of the file
1,Marc
2,Didac
3,Sergi
4,David
'''
f = open("../files/users","r")
for line in f:
v = line.split(",")
i = v[0]
name = v[1]
# All write operations happen in a transaction
user = db.node(id=i, name=name)
user.INSTANCE_OF(users_reference)
users_idx['id'][i] = user
# I suppose that here, the transaction is closed
# I want get the node whose property "Id" has value "3"
# to print the property "name" of the node with id = 3
# HERE I GET AN ERROR WHEN THE THERE'RE MULTIPLE NODES WITH THE SAME VALUE FOR THE PROPERTY "ID"
c = users_idx['id']['3'].single
print c['name']
'''
If the file has a duplicated ID, the previouly code returns an error...
1,Marc
1,Marc_2
1,Marc_3
2,Didac
3,Sergi
4,David
'''
# Always shut down your database when your application exits
db.shutdown()