python中neo4j中的节点索引

2024-10-01 04:54:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在构建一个包含标记节点和url节点的数据库,url节点连接到标记节点。在这种情况下,如果将同一个url插入到数据库中,它应该链接到标记节点,而不是创建重复的url节点。我认为索引可以解决这个问题。如何使用neo4jrestclient进行索引和遍历?。链接到教程就可以了。我当前正在使用versae neo4jrestclient。在

谢谢


Tags: 标记数据库url节点链接情况教程neo4jrestclient
2条回答

neo4jrestclient支持索引和遍历图形,但我认为仅使用索引对您的用例来说是非常有用的。不过,我不知道我是否理解你的问题。不管怎样,这样的方法可能会奏效:

>>> from neo4jrestclient.client import GraphDatabase

>>> gdb = GraphDatabase("http://localhost:7474/db/data/")

>>> idx =  gdb.nodes.indexes.create("urltags")

>>> url_node = gdb.nodes.create(url="http://foo.bar", type="URL")

>>> tag_node = gdb.nodes.create(tag="foobar", type="TAG")

我们将属性count添加到关系中,以跟踪标记foobar标记的"http://foo.bar"的数量。在

^{pr2}$

然后,我们根据url的值索引url节点。在

>>> idx["url"][url_node["url"]] = url_node

然后,当我需要创建一个用标记节点标记的新URL节点时,我们首先查询索引以检查它是否已被索引。否则,我们创建节点并索引它。在

>>> new_url = "http://foo.bar2"

>>> nodes = idx["url"][new_url]

>>> if len(nodes):
...     rel = nodes[0].relationships.all(types=[tag_node["tag"]])[0]
...     rel["count"] += 1
... else:
...     new_url_node = gdb.nodes.create(url=new_url, type="URL")
...     new_url_node.relationships.create(tag_node["tag"], tag_node, count=1)
...     idx["url"][new_url_node["url"]] = new_url_node

一个重要的概念是,索引是key/value/object三元组,其中对象要么是一个节点,要么是一个要索引的关系。在

创建和使用索引的步骤:

创建图形数据库rest客户机的实例。在

from neo4jrestclient.client import GraphDatabase
gdb = GraphDatabase("http://localhost:7474/db/data/")

创建节点或关系索引(在此处创建节点索引)

^{pr2}$

向索引添加节点

nelly = gdb.nodes.create(name='Nelly Furtado')
shakira = gdb.nodes.create(name='Shakira')

index['latin_genre'][nelly.get('name')] = nelly
index['latin_genre'][shakira.get('name')] = shakira

根据索引获取节点并进行进一步处理:

for artist in index['latin_genre']['Shakira']:

  print artist.get('name')

更多细节可以从webadmin中的注释中找到

Neo4j has two types of indexes, node and relationship indexes. With node indexes you index and find nodes, and with relationship indexes you do the same for relationships.

Each index has a provider, which is the underlying implementation handling that index. The default provider is lucene, but you can create your own index provides if you like.

Neo4j indexes take key/value/object triplets ("object" being a node or a relationship), it will index the key/value pair, and associate this with the object provided. After you have indexed a set of key/value/object triplets, you can query the index and get back objects that where indexed with key/value pairs matching your query.

For instance, if you have "User" nodes in your database, and want to rapidly find them by username or email, you could create a node index named "Users", and for each user index username and email. With the default lucene configuration, you can then search the "Users" index with a query like: "username:bob OR email:bob@gmail.com".

You can use the data browser to query your indexes this way, the syntax for the above query is "node:index:Users:username:bob OR email:bob@gmail.com".

相关问题 更多 >