21.18. Legacy indexing

[Note]Note

This documents the legacy indexing in Neo4j, which is no longer the preferred way to handle indexes. Consider looking at Section 21.13, “Indexing”.

An index can contain either nodes or relationships.

[Note]Note

To create an index with default configuration, simply start using it by adding nodes/relationships to it. It will then be automatically created for you.

What default configuration means depends on how you have configured your database. If you haven’t changed any indexing configuration, it means the indexes will be using a Lucene-based backend.

All the examples below show you how to do operations on node indexes, but all of them are just as applicable to relationship indexes. Simple change the "node" part of the URL to "relationship".

If you want to customize the index settings, see the section called “Create node index with configuration”.

Create node index

[Note]Note

Instead of creating the index this way, you can simply start to use it, and it will be created automatically with default configuration.

Example request

  • POST http://localhost:7474/db/data/index/node/
  • Accept: application/json; charset=UTF-8
  • Content-Type: application/json
{
  "name" : "favorites"
}

Example response

  • 201: Created
  • Content-Type: application/json; charset=UTF-8
  • Location: http://localhost:7474/db/data/index/node/favorites/
{
  "template" : "http://localhost:7474/db/data/index/node/favorites/{key}/{value}"
}

Create node index with configuration

This request is only necessary if you want to customize the index settings. If you are happy with the defaults, you can just start indexing nodes/relationships, as non-existent indexes will automatically be created as you do. See Section 36.10, “Configuration and fulltext indexes” for more information on index configuration.

Example request

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

Example response

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

Delete node index

Example request

  • DELETE http://localhost:7474/db/data/index/node/kvnode
  • Accept: application/json; charset=UTF-8

Example response

  • 204: No Content

List node indexes

Example request

  • GET http://localhost:7474/db/data/index/node/
  • Accept: application/json; charset=UTF-8

Example response

  • 200: OK
  • Content-Type: application/json; charset=UTF-8
{
  "node_auto_index" : {
    "template" : "http://localhost:7474/db/data/index/node/node_auto_index/{key}/{value}",
    "provider" : "lucene",
    "type" : "exact"
  },
  "favorites" : {
    "template" : "http://localhost:7474/db/data/index/node/favorites/{key}/{value}",
    "provider" : "lucene",
    "type" : "exact"
  }
}

Add node to index

Associates a node with the given key/value pair in the given index.

[Note]Note

Spaces in the URI have to be encoded as %20.

[Caution]Caution

This does not overwrite previous entries. If you index the same key/value/item combination twice, two index entries are created. To do update-type operations, you need to delete the old entry before adding a new one.

Example request

  • POST http://localhost:7474/db/data/index/node/favorites
  • Accept: application/json; charset=UTF-8
  • Content-Type: application/json
{
  "value" : "some value",
  "uri" : "http://localhost:7474/db/data/node/75",
  "key" : "some-key"
}

Example response

  • 201: Created
  • Content-Type: application/json; charset=UTF-8
  • Location: http://localhost:7474/db/data/index/node/favorites/some-key/some%20value/75
{
  "extensions" : { },
  "outgoing_relationships" : "http://localhost:7474/db/data/node/75/relationships/out",
  "labels" : "http://localhost:7474/db/data/node/75/labels",
  "traverse" : "http://localhost:7474/db/data/node/75/traverse/{returnType}",
  "all_typed_relationships" : "http://localhost:7474/db/data/node/75/relationships/all/{-list|&|types}",
  "self" : "http://localhost:7474/db/data/node/75",
  "property" : "http://localhost:7474/db/data/node/75/properties/{key}",
  "properties" : "http://localhost:7474/db/data/node/75/properties",
  "outgoing_typed_relationships" : "http://localhost:7474/db/data/node/75/relationships/out/{-list|&|types}",
  "incoming_relationships" : "http://localhost:7474/db/data/node/75/relationships/in",
  "create_relationship" : "http://localhost:7474/db/data/node/75/relationships",
  "paged_traverse" : "http://localhost:7474/db/data/node/75/paged/traverse/{returnType}{?pageSize,leaseTime}",
  "all_relationships" : "http://localhost:7474/db/data/node/75/relationships/all",
  "incoming_typed_relationships" : "http://localhost:7474/db/data/node/75/relationships/in/{-list|&|types}",
  "metadata" : {
    "id" : 75,
    "labels" : [ ]
  },
  "data" : { },
  "indexed" : "http://localhost:7474/db/data/index/node/favorites/some-key/some%20value/75"
}

Remove all entries with a given node from an index

Example request

  • DELETE http://localhost:7474/db/data/index/node/kvnode/80
  • Accept: application/json; charset=UTF-8

Example response

  • 204: No Content

Remove all entries with a given node and key from an index

Example request

  • DELETE http://localhost:7474/db/data/index/node/kvnode/kvkey2/83
  • Accept: application/json; charset=UTF-8

Example response

  • 204: No Content

Remove all entries with a given node, key and value from an index

Example request

  • DELETE http://localhost:7474/db/data/index/node/kvnode/kvkey1/value1/76
  • Accept: application/json; charset=UTF-8

Example response

  • 204: No Content

Find node by exact match

[Note]Note

Spaces in the URI have to be encoded as %20.

Example request

  • GET http://localhost:7474/db/data/index/node/favorites/key/the%2520value
  • Accept: application/json; charset=UTF-8

Example response

  • 200: OK
  • Content-Type: application/json; charset=UTF-8
[ {
  "indexed" : "http://localhost:7474/db/data/index/node/favorites/key/the%2520value/91",
  "outgoing_relationships" : "http://localhost:7474/db/data/node/91/relationships/out",
  "labels" : "http://localhost:7474/db/data/node/91/labels",
  "data" : { },
  "traverse" : "http://localhost:7474/db/data/node/91/traverse/{returnType}",
  "all_typed_relationships" : "http://localhost:7474/db/data/node/91/relationships/all/{-list|&|types}",
  "self" : "http://localhost:7474/db/data/node/91",
  "property" : "http://localhost:7474/db/data/node/91/properties/{key}",
  "properties" : "http://localhost:7474/db/data/node/91/properties",
  "outgoing_typed_relationships" : "http://localhost:7474/db/data/node/91/relationships/out/{-list|&|types}",
  "incoming_relationships" : "http://localhost:7474/db/data/node/91/relationships/in",
  "extensions" : { },
  "create_relationship" : "http://localhost:7474/db/data/node/91/relationships",
  "paged_traverse" : "http://localhost:7474/db/data/node/91/paged/traverse/{returnType}{?pageSize,leaseTime}",
  "all_relationships" : "http://localhost:7474/db/data/node/91/relationships/all",
  "incoming_typed_relationships" : "http://localhost:7474/db/data/node/91/relationships/in/{-list|&|types}",
  "metadata" : {
    "id" : 91,
    "labels" : [ ]
  }
} ]

Find node by query

The query language used here depends on what type of index you are querying. The default index type is Lucene, in which case you should use the Lucene query language here. Below an example of a fuzzy search over multiple keys.

See: http://lucene.apache.org/core/3_6_2/queryparsersyntax.html

Getting the results with a predefined ordering requires adding the parameter

order=ordering

where ordering is one of index, relevance or score. In this case an additional field will be added to each result, named score, that holds the float value that is the score reported by the query result.

Example request

  • GET http://localhost:7474/db/data/index/node/bobTheIndex?query=Name:Build~0.1%20AND%20Gender:Male
  • Accept: application/json; charset=UTF-8

Example response

  • 200: OK
  • Content-Type: application/json; charset=UTF-8
[ {
  "outgoing_relationships" : "http://localhost:7474/db/data/node/90/relationships/out",
  "labels" : "http://localhost:7474/db/data/node/90/labels",
  "data" : {
    "Name" : "Builder"
  },
  "traverse" : "http://localhost:7474/db/data/node/90/traverse/{returnType}",
  "all_typed_relationships" : "http://localhost:7474/db/data/node/90/relationships/all/{-list|&|types}",
  "self" : "http://localhost:7474/db/data/node/90",
  "property" : "http://localhost:7474/db/data/node/90/properties/{key}",
  "properties" : "http://localhost:7474/db/data/node/90/properties",
  "outgoing_typed_relationships" : "http://localhost:7474/db/data/node/90/relationships/out/{-list|&|types}",
  "incoming_relationships" : "http://localhost:7474/db/data/node/90/relationships/in",
  "extensions" : { },
  "create_relationship" : "http://localhost:7474/db/data/node/90/relationships",
  "paged_traverse" : "http://localhost:7474/db/data/node/90/paged/traverse/{returnType}{?pageSize,leaseTime}",
  "all_relationships" : "http://localhost:7474/db/data/node/90/relationships/all",
  "incoming_typed_relationships" : "http://localhost:7474/db/data/node/90/relationships/in/{-list|&|types}",
  "metadata" : {
    "id" : 90,
    "labels" : [ ]
  }
} ]