18.10. Configurable Automatic Indexing

18.10.1. Create an auto index for nodes with specific configuration
18.10.2. Create an auto index for relationships with specific configuration

Out of the box auto-indexing supports exact matches since they are created with the default configuration (http://docs.neo4j.org/chunked/snapshot/indexing-create.html) the first time you access them. However it is possible to intervene in the lifecycle of the server before any auto indexes are created to change their configuration.

This approach cannot be used on databases that already have auto-indexes established. To change the auto-index configuration existing indexes would have to be deleted first, so be careful!

[Caution]Caution

This technique works, but it is not particularly pleasant. Future versions of Neo4j may remove this loophole in favour of a better structured feature for managing auto-indexing configurations.

Auto-indexing must be enabled through configuration before we can create or configure them. Firstly ensure that you’ve added some config like this into your server’s neo4j.properties file:

node_auto_indexing=true
relationship_auto_indexing=true
node_keys_indexable=name,phone
relationship_keys_indexable=since

The node_auto_indexing and relationship_auto_indexing turn auto-indexing on for nodes and relationships respectively. The node_keys_indexable key allows you to specify a comma-separated list of node property keys to be indexed. The relationship_keys_indexable does the same for relationship property keys.

Next start the server as usual by invoking the start script as described in Section 17.1, “Server Installation”.

Next we have to pre-empt the creation of an auto-index, by telling the server to create an apparently manual index which has the same name as the node (or relationship) auto-index. For example, in this case we’ll create a node auto index whose name is node_auto_index, like so:

18.10.1. Create an auto index for nodes with specific configuration

Example request

  • POST http://localhost:7474/db/data/index/node/
  • Accept: application/json
  • Content-Type: application/json
{"name":"node_auto_index", "config":{"type":"fulltext","provider":"lucene"}}

Example response

  • 201: Created
  • Content-Type: application/json
  • Location: http://localhost:7474/db/data/index/node/node_auto_index/
{
  "template" : "http://localhost:7474/db/data/index/node/node_auto_index/{key}/{value}",
  "provider" : "lucene",
  "type" : "fulltext"
}

If you require configured auto-indexes for relationships, the approach is similar:

18.10.2. Create an auto index for relationships with specific configuration

Example request

  • POST http://localhost:7474/db/data/index/relationship/
  • Accept: application/json
  • Content-Type: application/json
{"name":"relationship_auto_index", "config":{"type":"fulltext","provider":"lucene"}}

Example response

  • 201: Created
  • Content-Type: application/json
  • Location: http://localhost:7474/db/data/index/relationship/relationship_auto_index/
{
  "template" : "http://localhost:7474/db/data/index/relationship/relationship_auto_index/{key}/{value}",
  "provider" : "lucene",
  "type" : "fulltext"
}

In case you’re curious how this works, on the server side it triggers the creation of an index which happens to have the same name as the auto index that the database would create for itself. Now when we interact with the database, the index thinks the index is already is created so the state machine skips over that step and just gets on with normal day-to-day auto-indexing.

[Caution]Caution

You have to do this early in your server lifecycle, before any normal auto indexes are created.