gpt4 book ai didi

python - 无法在浏览器中使用 cypher 查询查看我使用 py2neo 创建的节点

转载 作者:行者123 更新时间:2023-12-01 05:04:32 30 4
gpt4 key购买 nike

我正在使用 py2neo 创建节点,如下所示:

from py2neo import neo4j 
graph_db = neo4j.GraphDatabaseService("http://localhost:7474/db/data")

print graph_db.neo4j_version
graph_db.clear()

if not graph_db.get_index(neo4j.Node, "Kiran"):
from py2neo import node,rel
trial = graph_db.create(
node(name="Kiran"),
node(name="teja"),
rel(0, "Brother", 1),
)

#else:
details = graph_db.get_index(neo4j.Node, "Kiran")
print details

get_index 返回一些数据,例如

Index(Node, u'http://localhost:7474/db/data/index/node/Kiran')

但是当我在浏览器上搜索该节点时,它没有返回任何内容......我在这里做错了什么吗?

我还尝试发布一些网络信息,如下:

from py2neo import neo4j
from py2neo import node,rel
graph_db = neo4j.GraphDatabaseService("http://localhost:7474/db/data")

def nodepublish(dpid, port, mac):
if graph_db.get_index( neo4j.Node, dpid ) == None:
create = graph_db.create({"DPID": dpid})
print "switch "+str(dpid)+" added to graph"
if graph_db.get_index( neo4j.Node, mac ) == None:
query = neo4j.CypherQuery(graph_db, "MATCH (sw) WHERE sw.DPID = "+str(dpid)+" CREATE (nd {MAC: "+str(mac)+"}) CREATE (sw)-[:connected {PORT: "+str(port)+"}]->(nd)")
print "node "+str(mac)+" added to graph"

当我像这样调用nodepublish()函数时

nodepublish(1,1,"aa:aa:aa:aa:aa:aa")

每次 get_index 不返回 None 时,它​​都会创建一个 dpid:1 的新节点,而不是跳过 if 语句。

有人可以帮我解决这个问题吗?

谢谢!

最佳答案

第 1 点:确保 GraphDatabaseService URI 上有一个尾部斜杠。如果没有它,您可能会得到不正确的结果。

第 2 点:您在此处使用旧索引。通过阅读 this 明确您正在使用哪种类型的索引.

我认为您混淆了索引索引条目索引(在本例中可能称为People)指向一组条目,每个条目都由一个键值对标识。在每个入口点,您可以引用一个或多个节点。了解有关旧索引的更多信息 here .

您可能希望您的代码看起来更像这样:

from py2neo import neo4j 
graph_db = neo4j.GraphDatabaseService("http://localhost:7474/db/data/")

# Create a "People" index if one doesn't already exist
people = graph_db.get_or_create_index(neo4j.Node, "People"):

# Create two people nodes if they don't already exist
kiran = people.get_or_create("name", "Kiran", {"name": "Kiran"})
teja = people.get_or_create("name", "Teja", {"name": "Teja"})

# Relate the two
brothers, = graph_db.create((kiran, "BROTHER", teja))

print kiran
print teja
print brothers

This page可能会对代码中的一些细节有所帮助,因为它描述了您此处需要的旧索引函数。

关于python - 无法在浏览器中使用 cypher 查询查看我使用 py2neo 创建的节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25321604/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com