You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
Indexes and constraints are essential for performance and data integrity in Neo4j. Indexes speed up queries by avoiding full graph scans, while constraints enforce rules about your data.
Without indexes, Neo4j must scan every node with a given label to find matches:
// Without an index, this scans ALL Person nodes
MATCH (p:Person {email: "alice@example.com"})
RETURN p
With an index on :Person(email), Neo4j goes directly to the matching node.
| Index Type | Purpose | Example |
|---|---|---|
| Range index | General-purpose lookups, ranges, sorting | Equality, >, <, STARTS WITH |
| Text index | Full-text string matching | CONTAINS, ENDS WITH |
| Point index | Geospatial queries | Distance and bounding-box queries |
| Composite index | Multi-property lookups | Queries filtering on multiple properties |
| Full-text index | Advanced text search with scoring | Fuzzy matching, relevance ranking |
| Lookup index | Internal label/type lookups | Automatic — do not create manually |
// Index on a single property
CREATE INDEX person_email FOR (p:Person) ON (p.email)
// Index on a relationship property
CREATE INDEX purchased_date FOR ()-[r:PURCHASED]-() ON (r.date)
CREATE INDEX person_name_age FOR (p:Person) ON (p.name, p.age)
CREATE TEXT INDEX person_name_text FOR (p:Person) ON (p.name)
Text indexes support CONTAINS and ENDS WITH queries efficiently.
CREATE POINT INDEX location_idx FOR (p:Place) ON (p.location)
CREATE FULLTEXT INDEX product_search FOR (p:Product) ON EACH [p.name, p.description]
Query full-text indexes using db.index.fulltext.queryNodes:
CALL db.index.fulltext.queryNodes("product_search", "wireless mouse") YIELD node, score
RETURN node.name, score
ORDER BY score DESC
SHOW INDEXES
DROP INDEX person_email
EXPLAIN MATCH (p:Person {email: "alice@example.com"})
RETURN p
Look for NodeIndexSeek in the query plan — this confirms the index is being used.
Constraints enforce data integrity rules in your graph:
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.