2012-02-24 71 views
2

我正在構建一個包含標籤節點和url節點的數據庫,並且url節點連接到了標籤節點。在這種情況下,如果將相同的url插入到數據庫中,它應該鏈接到標記節點,而不是創建重複的url節點。我認爲索引可以解決這個問題。怎樣才能用neo4jrestclient做索引和遍歷呢?鏈接到教程會很好。我目前使用versae neo4jrestclientpython中neo4j中的索引節點

感謝

回答

5

的neo4jrestclient同時支持索引和遍歷圖,但我認爲僅僅通過使用索引可能是enoguh爲您的使用情況。但是,我不知道我是否理解了你的問題。無論如何,這樣的事情可能工作:

>>> 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"的數量。

>>> url_node.relationships.create(tag_node["tag"], tag_node, count=1) 

然後,我們根據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 
+1

非常感謝您的幫助.. – jvc 2012-02-25 15:57:01

+1

不客氣:--D – 2012-02-26 00:12:05

3

的一個重要概念是指標是鍵/值/對象三胞胎其中對象可以是一個節點,或者你想索引的關係。

步驟來創建和使用索引:

創建圖形數據庫其餘客戶端的一個實例。

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

創建一個節點或關係指數(這裏創建一個節點索引)

index = gdb.nodes.indexes.create('latin_genre') 

節點添加到索引

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有兩種類型的索引節點和關係索引。通過 節點索引可以索引和查找節點,並且可以使用關係索引 來確定關係。

每個索引都有一個提供者,它是處理該索引的底層實現 。默認的提供者是lucene,但是如果你喜歡,你可以創建自己的索引 。Neo4j索引採用鍵/值/對象三元組(「對象」是節點或 的關係),它將索引鍵/值對,並將此 與提供的對象相關聯。索引了一組關鍵/值/對象三元組後,您可以查詢索引並獲取 對象,該對象的索引中包含與您的查詢匹配的鍵/值對。例如,如果您的數據庫中有「用戶」節點,並且想要 通過用戶名或電子郵件快速找到它們,您可以創建名爲「用戶」的節點索引 ,併爲每個用戶索引用戶名和電子郵件。通過 默認的lucene配置,您可以使用如下查詢搜索「用戶」索引 :「username:bob或email:[email protected]」。

您可以使用數據瀏覽器以這種方式查詢您的索引,上述查詢的 語法爲「node:index:Users:username:bob OR email:[email protected]」。

相關問題