22.11. Unique Indexes

22.11.1. Get or create unique node (create)
22.11.2. Get or create unique node (existing)
22.11.3. Create a unique node or return fail (create)
22.11.4. Create a unique node or return fail (fail)
22.11.5. Add an existing node to unique index (not indexed)
22.11.6. Add an existing node to unique index (already indexed)
22.11.7. Get or create unique relationship (create)
22.11.8. Get or create unique relationship (existing)
22.11.9. Create a unique relationship or return fail (create)
22.11.10. Create a unique relationship or return fail (fail)
22.11.11. Add an existing relationship to a unique index (not indexed)
22.11.12. Add an existing relationship to a unique index (already indexed)

For uniqueness enforcements, there are two modes:

For more information, see Section 17.6, “Creating unique nodes”.

22.11.1. Get or create unique node (create)

The node is created if it doesn’t exist in the unique index already.

Example request

  • POST http://localhost:7474/db/data/index/node/people?uniqueness=get_or_create
  • Accept: application/json; charset=UTF-8
  • Content-Type: application/json
{
  "key" : "name",
  "value" : "Tobias",
  "properties" : {
    "name" : "Tobias",
    "sequence" : 1
  }
}

Example response

  • 201: Created
  • Content-Type: application/json; charset=UTF-8
  • Location: http://localhost:7474/db/data/index/node/people/name/Tobias/20
{
  "extensions" : {
  },
  "paged_traverse" : "http://localhost:7474/db/data/node/20/paged/traverse/{returnType}{?pageSize,leaseTime}",
  "outgoing_relationships" : "http://localhost:7474/db/data/node/20/relationships/out",
  "traverse" : "http://localhost:7474/db/data/node/20/traverse/{returnType}",
  "all_typed_relationships" : "http://localhost:7474/db/data/node/20/relationships/all/{-list|&|types}",
  "property" : "http://localhost:7474/db/data/node/20/properties/{key}",
  "all_relationships" : "http://localhost:7474/db/data/node/20/relationships/all",
  "self" : "http://localhost:7474/db/data/node/20",
  "outgoing_typed_relationships" : "http://localhost:7474/db/data/node/20/relationships/out/{-list|&|types}",
  "properties" : "http://localhost:7474/db/data/node/20/properties",
  "incoming_relationships" : "http://localhost:7474/db/data/node/20/relationships/in",
  "incoming_typed_relationships" : "http://localhost:7474/db/data/node/20/relationships/in/{-list|&|types}",
  "create_relationship" : "http://localhost:7474/db/data/node/20/relationships",
  "data" : {
    "sequence" : 1,
    "name" : "Tobias"
  },
  "indexed" : "http://localhost:7474/db/data/index/node/people/name/Tobias/20"
}

22.11.2. Get or create unique node (existing)

Here, a node is not created but the existing unique node returned, since another node is indexed with the same data already. The node data returned is then that of the already existing node.

Example request

  • POST http://localhost:7474/db/data/index/node/people?uniqueness=get_or_create
  • Accept: application/json; charset=UTF-8
  • Content-Type: application/json
{
  "key" : "name",
  "value" : "Peter",
  "properties" : {
    "name" : "Peter",
    "sequence" : 2
  }
}

Example response

  • 200: OK
  • Content-Type: application/json; charset=UTF-8
  • Location: http://localhost:7474/db/data/index/node/people/name/Peter/12
{
  "extensions" : {
  },
  "paged_traverse" : "http://localhost:7474/db/data/node/12/paged/traverse/{returnType}{?pageSize,leaseTime}",
  "outgoing_relationships" : "http://localhost:7474/db/data/node/12/relationships/out",
  "traverse" : "http://localhost:7474/db/data/node/12/traverse/{returnType}",
  "all_typed_relationships" : "http://localhost:7474/db/data/node/12/relationships/all/{-list|&|types}",
  "property" : "http://localhost:7474/db/data/node/12/properties/{key}",
  "all_relationships" : "http://localhost:7474/db/data/node/12/relationships/all",
  "self" : "http://localhost:7474/db/data/node/12",
  "outgoing_typed_relationships" : "http://localhost:7474/db/data/node/12/relationships/out/{-list|&|types}",
  "properties" : "http://localhost:7474/db/data/node/12/properties",
  "incoming_relationships" : "http://localhost:7474/db/data/node/12/relationships/in",
  "incoming_typed_relationships" : "http://localhost:7474/db/data/node/12/relationships/in/{-list|&|types}",
  "create_relationship" : "http://localhost:7474/db/data/node/12/relationships",
  "data" : {
    "sequence" : 1,
    "name" : "Peter"
  },
  "indexed" : "http://localhost:7474/db/data/index/node/people/name/Peter/12"
}

22.11.3. Create a unique node or return fail (create)

Here, in case of an already existing node, an error should be returned. In this example, no existing indexed node is found and a new node is created.

Example request

  • POST http://localhost:7474/db/data/index/node/people?uniqueness=create_or_fail
  • Accept: application/json; charset=UTF-8
  • Content-Type: application/json
{
  "key" : "name",
  "value" : "Tobias",
  "properties" : {
    "name" : "Tobias",
    "sequence" : 1
  }
}

Example response

  • 201: Created
  • Content-Type: application/json; charset=UTF-8
  • Location: http://localhost:7474/db/data/index/node/people/name/Tobias/19
{
  "extensions" : {
  },
  "paged_traverse" : "http://localhost:7474/db/data/node/19/paged/traverse/{returnType}{?pageSize,leaseTime}",
  "outgoing_relationships" : "http://localhost:7474/db/data/node/19/relationships/out",
  "traverse" : "http://localhost:7474/db/data/node/19/traverse/{returnType}",
  "all_typed_relationships" : "http://localhost:7474/db/data/node/19/relationships/all/{-list|&|types}",
  "property" : "http://localhost:7474/db/data/node/19/properties/{key}",
  "all_relationships" : "http://localhost:7474/db/data/node/19/relationships/all",
  "self" : "http://localhost:7474/db/data/node/19",
  "outgoing_typed_relationships" : "http://localhost:7474/db/data/node/19/relationships/out/{-list|&|types}",
  "properties" : "http://localhost:7474/db/data/node/19/properties",
  "incoming_relationships" : "http://localhost:7474/db/data/node/19/relationships/in",
  "incoming_typed_relationships" : "http://localhost:7474/db/data/node/19/relationships/in/{-list|&|types}",
  "create_relationship" : "http://localhost:7474/db/data/node/19/relationships",
  "data" : {
    "sequence" : 1,
    "name" : "Tobias"
  },
  "indexed" : "http://localhost:7474/db/data/index/node/people/name/Tobias/19"
}

22.11.4. Create a unique node or return fail (fail)

Here, in case of an already existing node, an error should be returned. In this example, an existing node indexed with the same data is found and an error is returned.

Example request

  • POST http://localhost:7474/db/data/index/node/people?uniqueness=create_or_fail
  • Accept: application/json; charset=UTF-8
  • Content-Type: application/json
{
  "key" : "name",
  "value" : "Peter",
  "properties" : {
    "name" : "Peter",
    "sequence" : 2
  }
}

Example response

  • 409: Conflict
  • Content-Type: application/json; charset=UTF-8
{
  "extensions" : {
  },
  "paged_traverse" : "http://localhost:7474/db/data/node/6/paged/traverse/{returnType}{?pageSize,leaseTime}",
  "outgoing_relationships" : "http://localhost:7474/db/data/node/6/relationships/out",
  "traverse" : "http://localhost:7474/db/data/node/6/traverse/{returnType}",
  "all_typed_relationships" : "http://localhost:7474/db/data/node/6/relationships/all/{-list|&|types}",
  "property" : "http://localhost:7474/db/data/node/6/properties/{key}",
  "all_relationships" : "http://localhost:7474/db/data/node/6/relationships/all",
  "self" : "http://localhost:7474/db/data/node/6",
  "outgoing_typed_relationships" : "http://localhost:7474/db/data/node/6/relationships/out/{-list|&|types}",
  "properties" : "http://localhost:7474/db/data/node/6/properties",
  "incoming_relationships" : "http://localhost:7474/db/data/node/6/relationships/in",
  "incoming_typed_relationships" : "http://localhost:7474/db/data/node/6/relationships/in/{-list|&|types}",
  "create_relationship" : "http://localhost:7474/db/data/node/6/relationships",
  "data" : {
    "sequence" : 1,
    "name" : "Peter"
  },
  "indexed" : "http://localhost:7474/db/data/index/node/people/name/Peter/6"
}

22.11.5. Add an existing node to unique index (not indexed)

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

In this example, we are using create_or_fail uniqueness.

Example request

  • POST http://localhost:7474/db/data/index/node/favorites?uniqueness=create_or_fail
  • Accept: application/json; charset=UTF-8
  • Content-Type: application/json
{
  "value" : "some value",
  "uri" : "http://localhost:7474/db/data/node/16",
  "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/16
{
  "extensions" : {
  },
  "paged_traverse" : "http://localhost:7474/db/data/node/16/paged/traverse/{returnType}{?pageSize,leaseTime}",
  "outgoing_relationships" : "http://localhost:7474/db/data/node/16/relationships/out",
  "traverse" : "http://localhost:7474/db/data/node/16/traverse/{returnType}",
  "all_typed_relationships" : "http://localhost:7474/db/data/node/16/relationships/all/{-list|&|types}",
  "property" : "http://localhost:7474/db/data/node/16/properties/{key}",
  "all_relationships" : "http://localhost:7474/db/data/node/16/relationships/all",
  "self" : "http://localhost:7474/db/data/node/16",
  "outgoing_typed_relationships" : "http://localhost:7474/db/data/node/16/relationships/out/{-list|&|types}",
  "properties" : "http://localhost:7474/db/data/node/16/properties",
  "incoming_relationships" : "http://localhost:7474/db/data/node/16/relationships/in",
  "incoming_typed_relationships" : "http://localhost:7474/db/data/node/16/relationships/in/{-list|&|types}",
  "create_relationship" : "http://localhost:7474/db/data/node/16/relationships",
  "data" : {
  },
  "indexed" : "http://localhost:7474/db/data/index/node/favorites/some-key/some%20value/16"
}

22.11.6. Add an existing node to unique index (already indexed)

In this case, the node already exists in the index, and thus we get a HTTP 409 status response, as we have set the uniqueness to create_or_fail.

Example request

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

Example response

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

22.11.7. Get or create unique relationship (create)

Create a unique relationship in an index. If a relationship matching the given key and value already exists in the index, it will be returned. If not, a new relationship will be created.

[Note]Note

The type and direction of the relationship is not regarded when determining uniqueness.

Example request

  • POST http://localhost:7474/db/data/index/relationship/MyIndex/?uniqueness=get_or_create
  • Accept: application/json; charset=UTF-8
  • Content-Type: application/json
{
  "key" : "name",
  "value" : "Tobias",
  "start" : "http://localhost:7474/db/data/node/22",
  "end" : "http://localhost:7474/db/data/node/23",
  "type" : "knowledge"
}

Example response

  • 201: Created
  • Content-Type: application/json; charset=UTF-8
  • Location: http://localhost:7474/db/data/index/relationship/MyIndex/name/Tobias/5
{
  "extensions" : {
  },
  "start" : "http://localhost:7474/db/data/node/22",
  "property" : "http://localhost:7474/db/data/relationship/5/properties/{key}",
  "self" : "http://localhost:7474/db/data/relationship/5",
  "properties" : "http://localhost:7474/db/data/relationship/5/properties",
  "type" : "knowledge",
  "end" : "http://localhost:7474/db/data/node/23",
  "data" : {
    "name" : "Tobias"
  },
  "indexed" : "http://localhost:7474/db/data/index/relationship/MyIndex/name/Tobias/5"
}

22.11.8. Get or create unique relationship (existing)

Here, in case of an already existing relationship, the sent data is ignored and the existing relationship returned.

Example request

  • POST http://localhost:7474/db/data/index/relationship/rels?uniqueness=get_or_create
  • Accept: application/json; charset=UTF-8
  • Content-Type: application/json
{
  "key" : "name",
  "value" : "Peter",
  "start" : "http://localhost:7474/db/data/node/26",
  "end" : "http://localhost:7474/db/data/node/27",
  "type" : "KNOWS"
}

Example response

  • 200: OK
  • Content-Type: application/json; charset=UTF-8
{
  "extensions" : {
  },
  "start" : "http://localhost:7474/db/data/node/24",
  "property" : "http://localhost:7474/db/data/relationship/6/properties/{key}",
  "self" : "http://localhost:7474/db/data/relationship/6",
  "properties" : "http://localhost:7474/db/data/relationship/6/properties",
  "type" : "KNOWS",
  "end" : "http://localhost:7474/db/data/node/25",
  "data" : {
  },
  "indexed" : "http://localhost:7474/db/data/index/relationship/rels/name/Peter/6"
}

22.11.9. Create a unique relationship or return fail (create)

Here, in case of an already existing relationship, an error should be returned. In this example, no existing relationship is found and a new relationship is created.

Example request

  • POST http://localhost:7474/db/data/index/relationship/rels?uniqueness=create_or_fail
  • Accept: application/json; charset=UTF-8
  • Content-Type: application/json
{
  "key" : "name",
  "value" : "Tobias",
  "start" : "http://localhost:7474/db/data/node/34",
  "end" : "http://localhost:7474/db/data/node/35",
  "type" : "KNOWS"
}

Example response

  • 201: Created
  • Content-Type: application/json; charset=UTF-8
  • Location: http://localhost:7474/db/data/index/relationship/rels/name/Tobias/9
{
  "extensions" : {
  },
  "start" : "http://localhost:7474/db/data/node/34",
  "property" : "http://localhost:7474/db/data/relationship/9/properties/{key}",
  "self" : "http://localhost:7474/db/data/relationship/9",
  "properties" : "http://localhost:7474/db/data/relationship/9/properties",
  "type" : "KNOWS",
  "end" : "http://localhost:7474/db/data/node/35",
  "data" : {
    "name" : "Tobias"
  },
  "indexed" : "http://localhost:7474/db/data/index/relationship/rels/name/Tobias/9"
}

22.11.10. Create a unique relationship or return fail (fail)

Here, in case of an already existing relationship, an error should be returned. In this example, an existing relationship is found and an error is returned.

Example request

  • POST http://localhost:7474/db/data/index/relationship/rels?uniqueness=create_or_fail
  • Accept: application/json; charset=UTF-8
  • Content-Type: application/json
{
  "key" : "name",
  "value" : "Peter",
  "start" : "http://localhost:7474/db/data/node/18",
  "end" : "http://localhost:7474/db/data/node/19",
  "type" : "KNOWS"
}

Example response

  • 409: Conflict
  • Content-Type: application/json; charset=UTF-8
{
  "extensions" : {
  },
  "start" : "http://localhost:7474/db/data/node/16",
  "property" : "http://localhost:7474/db/data/relationship/3/properties/{key}",
  "self" : "http://localhost:7474/db/data/relationship/3",
  "properties" : "http://localhost:7474/db/data/relationship/3/properties",
  "type" : "KNOWS",
  "end" : "http://localhost:7474/db/data/node/17",
  "data" : {
  },
  "indexed" : "http://localhost:7474/db/data/index/relationship/rels/name/Peter/3"
}

22.11.11. Add an existing relationship to a unique index (not indexed)

If a relationship matching the given key and value already exists in the index, it will be returned. If not, an HTTP 409 (conflict) status will be returned in this case, as we are using create_or_fail.

It’s possible to use get_or_create uniqueness as well.

[Note]Note

The type and direction of the relationship is not regarded when determining uniqueness.

Example request

  • POST http://localhost:7474/db/data/index/relationship/rels?uniqueness=create_or_fail
  • Accept: application/json; charset=UTF-8
  • Content-Type: application/json
{
  "key" : "name",
  "value" : "Peter",
  "uri" : "http://localhost:7474/db/data/relationship/2"
}

Example response

  • 201: Created
  • Content-Type: application/json; charset=UTF-8
  • Location: http://localhost:7474/db/data/index/relationship/rels/name/Peter/2
{
  "extensions" : {
  },
  "start" : "http://localhost:7474/db/data/node/14",
  "property" : "http://localhost:7474/db/data/relationship/2/properties/{key}",
  "self" : "http://localhost:7474/db/data/relationship/2",
  "properties" : "http://localhost:7474/db/data/relationship/2/properties",
  "type" : "KNOWS",
  "end" : "http://localhost:7474/db/data/node/15",
  "data" : {
  },
  "indexed" : "http://localhost:7474/db/data/index/relationship/rels/name/Peter/2"
}

22.11.12. Add an existing relationship to a unique index (already indexed)

Example request

  • POST http://localhost:7474/db/data/index/relationship/rels?uniqueness=create_or_fail
  • Accept: application/json; charset=UTF-8
  • Content-Type: application/json
{
  "key" : "name",
  "value" : "Peter",
  "uri" : "http://localhost:7474/db/data/relationship/4"
}

Example response

  • 409: Conflict
  • Content-Type: application/json; charset=UTF-8
{
  "extensions" : {
  },
  "start" : "http://localhost:7474/db/data/node/20",
  "property" : "http://localhost:7474/db/data/relationship/4/properties/{key}",
  "self" : "http://localhost:7474/db/data/relationship/4",
  "properties" : "http://localhost:7474/db/data/relationship/4/properties",
  "type" : "KNOWS",
  "end" : "http://localhost:7474/db/data/node/21",
  "data" : {
  },
  "indexed" : "http://localhost:7474/db/data/index/relationship/rels/name/Peter/4"
}