我是Django和neo4j的新手。我正在使用Django 1.4.5,neo4j 1.9.2和neo4django 0.1.8
我为人员节点创建了NodeModel,并将其编入“所有者”和“名称”属性的索引。这是我的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服务器之前,我将自动索引设置为True,并在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中所建议的,我使用connection.cypher(查询)来查询neo4j数据库
例如:
listpar = connection.cypher(“START no = node(*)RETURN no.owner?,no.name?”,raw = True)
Above会正确返回所有节点的所有者和名称。但是当我尝试查询索引属性而不是'number'或'*'时,如下所示:
listpar = connection.cypher(“START no = node:node_auto_index(name ='s2')RETURN no.owner?,no.name?”,raw = True)
上面给出了0行。
listpar = connection.cypher(“START no = node:graph-person_conns(name ='s2')RETURN no.owner?,no.name?”,raw = True)
上面给出了
例外值:
错误[400]:错误请求。错误的请求语法或不支持的方法。 发送的数据无效:(' expected but
- '在图表后找到
我尝试了其他字符串,如name,person_conns而不是graph-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,请建议。谢谢:)
答案 0 :(得分:1)
QuerySet
如果你将属性设置为indexed=True
,那么上述工作就可以正常工作了(或者不是,对于那些人来说,它的速度会慢一些)。
people = person_conns.objects.filter(name='n2')
The neo4django docs还有其他一些查询示例,Django docs也是如此。 Neo4django在后端执行这些查询作为Cypher-你真的不需要自己编写Cypher,除非你有一个非常特殊的遍历模式或性能问题。
无论如何,为了更直接地解决你的问题 - 你使用的最后一个例子需要反引号来逃避索引名称,比如
listpar = connection.cypher("START no=node:`graph-person_conns`(name='s2') RETURN no.owner?, no.name?",raw=True)
第一个例子应该有效。有人认为你在之后在上翻转了自动索引, 之后是否保存了你正在搜索的节点?如果之后,请注意您必须使用Java API手动重新索引节点,或者重新设置节点上的属性,因为它不会自动索引。
HTH,欢迎来到StackOverflow!