2
votes

How should auto-indexes be handled with the new label feature in neo4j 2.0. Will creating an auto-index, say "name" in a user object continue to create a generic name index for all objects or is there a way to specify that it should be only for a user type/label.

Should one instead do create the object and index in atomic cypher transaction of some kind?

The example use case could be a user object with say a unique case-insenstive username index.

2

2 Answers

1
votes

In 2.0 Labels is a great feature. I would not recommend auto indexing thought. With custom indexing on a label you can achieve unique creation of objects. The flow is like so Create index on the label first, let's say 'User' is the label to identify a user.

create index on :User(username)

Now you have an index over the 'username' on every node with label 'User' To create a unique node you need to have something to relate on creation. At the moment there is no such thing as

create unique (n:User {username:"tsartsaris"})

But you can use

CREATE UNIQUE (b)-[:ISUSER]->(m:User {username:"tsartsaris"})

This unique creation will search for m with label:User and username:tsartsaris, if it does not exists then it will create it. If you run the code again it will return the existing node and will not create a new one. If you use 2.0 m2 you can use merge as a method of creating a unique node.

  merge (n:User {username:"tsartsaris"})
  RETURN n

merge will search for a match in your graph , if it finds it will return it , otherwise will create the node and return it.

1
votes

Indexing in neo4j 2.0 will be mostly hidden. Indexes can be created for a label+property so that nodes with that label and property will be indexed in that index.

However there's as of yet no constraints (like uniqueness) to add, but it's in development, nor is there support for f.ex. case insensitivity. See more here http://docs.neo4j.org/chunked/preview/query-schema-index.html