【问题标题】:How to use indexed properties of NodeModels in cypher queries of Neo4django?如何在 Neo4django 的密码查询中使用 NodeModels 的索引属性?
【发布时间】:2013-07-15 14:41:34
【问题描述】:

我是 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服务器上创建的索引:

  1. graph-person_conns lucene
    {"to_lower_case":"true", "_blueprints:type":"MANUAL","type":"fulltext"}
  2. node_auto_index lucene {"_blueprints:type":"MANUAL", "type":"exact"}

按照https://github.com/scholrly/neo4django/issues/123 的建议,我使用connection.cypher(queries) 来查询neo4j 数据库

例如:

listpar = connection.cypher("START no=node(*) RETURN no.owner?, no.name?",raw=True)

Above 正确返回所有节点的所有者和名称。但是当我尝试查询索引属性而不是“数字”或“*”时,例如:

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。谢谢你:)

【问题讨论】:

    标签: django-models indexing neo4j neo4django


    【解决方案1】:

    嗯,你为什么要使用 Cypher?如果您将属性设置为indexed=True,neo4django QuerySets 就可以很好地完成上述工作(或者不,它会更慢)。

     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)
    

    第一个示例应该可以工作。一个想法 - 您是否在 beforeafter 上翻转了自动索引,以保存您正在搜索的节点?如果之后,请注意您必须使用 Java API 或通过重新设置节点上的属性手动重新索引节点,因为它不会被自动索引。

    HTH,欢迎来到 StackOverflow!

    【讨论】:

    • 谢谢!这就像一个魅力。尽管文档只涵盖了基础知识,但我只能通过您对所有网站上所有帖子的快速而详细的回复来学习 neo4django 的所有魔力。我真的很感激你的工作。非常感谢!实际上,我需要找到两个节点之间的最短路径,因此需要与密码查询作斗争。
    猜你喜欢
    • 1970-01-01
    • 2016-09-16
    • 2014-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-17
    • 1970-01-01
    相关资源
    最近更新 更多