25

I am creating a new Neo4j database. I have a type of node called User and I would like an index on the properties of user Identifier and EmailAddress. How does one go setting up an index when the database is new? I have noticed in the neo4j.properties file there looks to be support for creating indexes. However when I set these as so

# Autoindexing

# Enable auto-indexing for nodes, default is false
node_auto_indexing=true

# The node property keys to be auto-indexed, if enabled
node_keys_indexable=EmailAddress,Identifier

And add a node and do a query to find an Identifier that I know exists

START n=node:Identifier(Identifier = "USER0")
RETURN n;

then I get an

MissingIndexException: Index `Identifier` does not exist

How do I create an index and use it in a start query? I only want to use config files and cypher to achieve this. i.e. at the present time I am only playing in the Power Tool Console.

Somnath Muluk
  • 55,015
  • 38
  • 216
  • 226
Aran Mulholland
  • 23,555
  • 29
  • 141
  • 228
  • Make sure to see Boggle's answer below - how indexes work is significantly simpler in 2.0+ - and as you say it's a new database, so hopefully moving to the latest neo4j version is an option... – Dan G Apr 29 '14 at 12:19

3 Answers3

52

Add the following to the neo4j.properties file

# Autoindexing

# Enable auto-indexing for nodes, default is false
node_auto_indexing=true

# The node property keys to be auto-indexed, if enabled
node_keys_indexable=EmailAddress,Identifier

Create the auto index for nodes

neo4j-sh (0)$ index --create node_auto_index -t Node

Check if they exist

neo4j-sh (0)$ index --indexes

Should return

Node indexes:
node_auto_index

When querying use the following syntax to specify the index

start a = node:node_auto_index(Identifier="USER0")
return a;

As the node is auto indexed the name of the index is node_auto_index

This information came from a comment at the bottom of this page

Update

In case you want to index your current data which was there before automatic indexing was turned on (where Property_Name is the name of your index)

START nd =node(*) 
WHERE has(nd.Property_Name)
WITH nd
SET nd.Property_Name = nd.Property_Name
RETURN count(nd);
Michael Hunger
  • 41,339
  • 3
  • 57
  • 80
Aran Mulholland
  • 23,555
  • 29
  • 141
  • 228
  • [how to start up neo4j-sh on Windows](http://stackoverflow.com/a/19275558/1174169) – cod3monk3y Dec 07 '13 at 20:00
  • 2
    Use http://localhost:7474/webadmin. After page loads select the console tab. you can execute the shell commands there. – MSRS Oct 18 '14 at 13:04
  • 1
    This answer is really old and i'm not sure if this is still the best way to do this. Some clarification on this would be good. – Aran Mulholland Feb 11 '16 at 10:02
9

Indexes mainly made on property which is used for where condition. In Neo4j 2.0, indexes are easy to make now.

Create index on a label

CREATE INDEX ON :Person(name)

Drop index on a label

DROP INDEX ON :Person(name)

Create uniqueness constraint

CREATE CONSTRAINT ON (book:Book) ASSERT book.isbn IS UNIQUE

Drop uniqueness constraint

DROP CONSTRAINT ON (book:Book) ASSERT book.isbn IS UNIQUE

For listing all indexes and constraints in neo4j-browser, following command is useful

:schema

List indices and constraints for specific label with:

:schema ls -l :YourLabel
Somnath Muluk
  • 55,015
  • 38
  • 216
  • 226
8

In Neo4j 2.0, you should use labels and the new constraints instead

    CREATE CONSTRAINT ON (n:User) ASSERT n.Identifier IS UNIQUE
    CREATE CONSTRAINT ON (n:User) ASSERT n.EmailAddress IS UNIQUE

If email isn't unique per user, just create a plain index instead:

    CREATE INDEX ON :User(EmailAddress)
boggle
  • 221
  • 3
  • 3