Working with indexes in neo4j and py2neo

回眸只為那壹抹淺笑 提交于 2019-12-23 20:21:22

问题


I have just started working with py2neo and neo4j.

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

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])

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.

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?

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?


回答1:


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.



来源:https://stackoverflow.com/questions/22903909/working-with-indexes-in-neo4j-and-py2neo

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!