Constraints and Indexes in Neo4j

Constraints in Neo4j

A constraint is implemented internally as an index and is used to constrain what is added to the graph. There are three types of constraints you can define:

  • Uniqueness for a single node property value.

  • Existence for a property of a node or relationship.

  • Existence and uniqueness for a set of node property values (called a Node key).

A best practice is to create constraints before you load your data.

Enterprise Edition Only Features

Some constraints are only supported in Enterprise Edition of Neo4j. You should consult the Neo4j Cypher Manual for details.

Indexes in Neo4j

An index in Neo4j is a data structure that allows the graph engine to retrieve data quickly. All indexes in Neo4j require more storage in the graph, so you must ensure that you do not index everything!

After the data is loaded, you create indexes to make your queries perform faster. Using indexes makes writing data slower, but retrieving it faster.

The types of indexes in Neo4j include:

  • RANGE

  • LOOKUP

  • TEXT

  • POINT

  • Full-text

You can create an index on multiple properties or relationships. This type of index is called a Composite index.

Full-text Indexes

Full-text indexes are used differently from the other types of indexes in Neo4j. Full-text indexes will be covered separately in a later module. This course does not currently cover POINT indexes

Check your understanding

What type of index?

Suppose you need to create an index that will support fast lookups during a query.

What are some of the types of indexes can you use? (select all that apply)

  • ✓ TEXT

  • ✓ Full-text

  • ✓ RANGE

  • ❏ Hash

Hint

Three of these indexes types are supported in Neo4j.

Solution

The correct answers are TEXT, full-text, and RANGE

Summary

In this lesson, you learned about the types of constraints and indexes that Neo4j supports. In the next lesson, you will learn about determining what constraints and indexes to create in your graph.