3.7. Schema

Neo4j is a schema-optional graph database. You can use Neo4j without any schema. Optionally you can introduce it in order to gain performance or modeling benefits. This allows a way of working where the schema does not get in your way until you are at a stage where you want to reap the benefits of having one.


Schema commands can only be applied on the master machine in a Neo4j cluster (see Chapter 23, High Availability). If you apply them on a slave you will receive a Neo.ClientError.Transaction.InvalidType error code. (see Section 19.2, “Neo4j Status Codes”)



This feature was introduced in Neo4j 2.0, and is not the same as the legacy indexes (see Chapter 34, Legacy Indexing).

Performance is gained by creating indexes, which improve the speed of looking up nodes in the database. Once you’ve specified which properties to index, Neo4j will make sure your indexes are kept up to date as your graph evolves. Any operation that looks up nodes by the newly indexed properties will see a significant performance boost.

Indexes in Neo4j are eventually available. That means that when you first create an index, the operation returns immediately. The index is populating in the background and so is not immediately available for querying. When the index has been fully populated it will eventually come online. That means that it is now ready to be used in queries.

If something should go wrong with the index, it can end up in a failed state. When it is failed, it will not be used to speed up queries. To rebuild it, you can drop and recreate the index. Look at logs for clues about the failure.

You can track the status of your index by asking for the index state through the API you are using. Note, however, that this is not yet possible through Cypher.

How to use indexes in the different APIs:



This feature was introduced in Neo4j 2.0.

Neo4j can help you keep your data clean. It does so using constraints, that allow you to specify the rules for what your data should look like. Any changes that break these rules will be denied.

In this version, unique constraints is the only available constraint type.

How to use constraints in the different APIs: