Pregunta

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?

¿Fue útil?

Solución

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top