我是Django的新手以及neo4j。我使用Django 1.4.5,neo4j 1.9.2和neo4django 0.1.8如何在Neo4django的密碼查詢中使用NodeModel的索引屬性?
我已經爲一個人節點創建了NodeModel,並在'owner'和'name'屬性上建立了索引。這裏是我的models.py:
from neo4django.db import models as models2
class person_conns(models2.NodeModel):
owner = models2.StringProperty(max_length=30,indexed=True)
name = models2.StringProperty(max_length=30,indexed=True)
gender = models2.StringProperty(max_length=1)
parent = models2.Relationship('self',rel_type='parent_of',related_name='parents')
child = models2.Relationship('self',rel_type='child_of',related_name='children')
def __unicode__(self):
return self.name
之前,我連接到服務器的Neo4j,我設置自動索引爲真,並給了在CONF可轉位鍵/ neo4j.properties文件,如下所示:
# Autoindexing
# Enable auto-indexing for nodes, default is false
node_auto_indexing=true
# The node property keys to be auto-indexed, if enabled
node_keys_indexable=owner,name
# Enable auto-indexing for relationships, default is false
relationship_auto_indexing=true
# The relationship property keys to be auto-indexed, if enabled
relationship_keys_indexable=child_of,parent_of
我跟着Neo4j: Step by Step to create an automatic index更新上面的文件並在neo4j服務器上手動創建node_auto_index。
下面是執行上的Neo4j數據庫的Django的syndb後Neo4j的服務器上創建的索引和手動創建自動索引:
graph-person_conns lucene
{"to_lower_case":"true", "_blueprints:type":"MANUAL","type":"fulltext"}node_auto_index lucene {"_blueprints:type":"MANUAL", "type":"exact"}
如建議在https://github.com/scholrly/neo4django/issues/123我使用的連接.cypher(查詢)查詢neo4j數據庫
例如:
listpar = connection.cypher(「START無=節點(*)RETURN no.owner?no.name?",raw=True)
以上返回所有的所有者和名稱節點正確。但是,當我嘗試在索引屬性,而不是 '數字' 或 '*' 查詢,如情況下:
listpar = connection.cypher(「START無=節點:node_auto_index(NAME = 'S2') RETURN no.owner?,no.name?「,draw = True)
上面給出0行。
listpar = connection.cypher(「START =無節點:圖形-person_conns(名稱= 'S2')RETURN no.owner ?, no.name?",raw=True)
以上給出
異常值:
錯誤[400]:錯誤的請求。錯誤的請求語法或不支持的方法。 發送的無效數據:(' expected but
- 圖後,」發現
我試過其他類似的字符串名稱,person_conns而不是圖形的person_conns但每次都提示錯誤,特定索引不存在。我在添加索引時犯了一個錯誤嗎?
我的項目主要依賴於根據屬性過濾節點,所以這部分是非常重要的。任何指針或建議,將不勝感激。謝謝。
這是我在stackoverflow上的第一篇文章。因此,如果有任何信息遺漏或混淆,請耐心等待。謝謝。
更新: 謝謝你的幫助。爲了其他人的利益,我想舉例說明如何使用密碼查詢來遍歷/找到兩個節點之間的最短路徑。
from neo4django.db import connection
results = connection.cypher("START source=node:`graph-person_conns`(person_name='s2sp1'),dest=node:`graph-person_conns`(person_name='s2c1') MATCH p=ShortestPath(source-[*]->dest) RETURN extract(i in nodes(p) : i.person_name), extract(j in rels(p) : type(j))")
這是在圖上找到名爲s2sp1和s2c1的節點之間的最短路徑。 Cypher查詢真的很酷,並幫助遍歷節點限制跳數,關係類型等。
有人可以評論這種方法的性能嗎?另外請建議是否有任何其他有效的方法從Django訪問Neo4j。謝謝:)
謝謝!這就像一個魅力。儘管文檔僅包含基礎知識,但我只能通過對所有網站上所有帖子的快速而詳細的回覆來了解neo4django的所有魔法。我非常感謝你的工作。非常感謝!實際上,我需要找到兩個節點之間的最短路徑,並因此與密碼查詢進行鬥爭。 –