在neo4j和py2neo中使用索引 [英] Working with indexes in neo4j and py2neo

查看:458
本文介绍了在neo4j和py2neo中使用索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用py2neo和neo4j.

I have just started working with py2neo and neo4j.

我对如何使用数据库中的索引感到困惑.

I am confused about how to go about using indices in my database.

我创建了一个create_user函数:

I have created a create_user function:

g = neo4j.GraphDatabaseService()
users_index = g.get_or_create_index(neo4j.Node, "Users")
def create_user(name, username, **kwargs):
    batch = neo4j.WriteBatch(g)
    user = batch.create(node({"name" : name, "username" : username}))
    for key, value in kwargs.iteritems():
        batch.set_property(user, key, value)
    batch.add_labels(user, "User")
    batch.get_or_add_to_index(neo4j.Node, users_index, "username", username, user)
    results = batch.submit()
    print "Created: " + username

现在可以通过用户名获取用户了

Now to obtain users by their username:

def lookup_user(username):
    print node(users_index.get("username", username)[0])

我看到了 Schema 类,并注意到我可以在"User"标签,但我不知道如何获取索引并向其中添加实体.

I saw the Schema class and noticed that I can create an index on the "User" label, but I couldn't figure out how to obtain the index and add entities to it.

我希望它尽可能高效,所以如果以后要添加更多带有不同标签的节点,在"User"标签上添加索引是否会提高性能?它已经是最高效的了吗?

I want it to be as efficient as possible, so would adding the index on the "User" label add to performance, in case I were to add more nodes with different labels later on? Is it already the most efficient it can be?

此外,如果我希望我的用户名系统对每个用户来说都是唯一的,我该怎么做?我怎么知道batch.get_or_add_to_index是要获取实体还是要添加实体?

Also, if I would want my username system to be unique per user, how would I be able to do that? How do I know whether the batch.get_or_add_to_index is getting or adding the entity?

推荐答案

您的困惑是可以理解的. Neo4j中实际上有两种类型的索引-旧式索引(可以使用get_or_create_index方法访问)和新索引(用于处理基于标签的索引).

Your confusion is understandable. There are actually two types of indexes in Neo4j - the Legacy Indexes (which you access with the get_or_create_index method) and the new Indexes (which deal with indexing based on labels).

新索引不需要手动保持最新状态,它们可以在您对图形进行更改时保持同步,并在您对该标签/属性对发出密码查询时自动使用.

The new Indexes do not need to be manually kept up to date, they keep themselves in sync as you make changes to the graph, and are automatically used when you issue cypher queries against that label/property pair.

保留旧索引的原因是它们支持一些新索引尚无法使用的复杂功能,例如地理空间索引,全文本索引和复合索引.

The reason the legacy indexes are kept around is that they support some complex functionality that is not yet available for the new indexes - such as geospatial indexing, full text indexing and composite indexing.

这篇关于在neo4j和py2neo中使用索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆