Prev |  | Next

## 20.19. Unique Indexing

| Note
As of Neo4j 2.0, unique constraints have been added. These make Neo4j enforce the uniqueness, guaranteeing that uniqueness is maintained. See Section 3.7.2, “Constraints” for details about this. For most cases, the unique constraints should be used rather than the features described below.

For uniqueness enforcements, there are two modes:

- URL Parameter `uniqueness=get_or_create`: Create a new node/relationship and index it if no existing one can be found. If an existing node/relationship is found, discard the sent data and return the existing node/relationship.
- URL Parameter `uniqueness=create_or_fail`: Create a new node/relationship if no existing one can be found in the index. If an existing node/relationship is found, return a conflict error.

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

### 20.19.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/97`

    {
      "extensions" : {
      },
      "labels" : "http://localhost:7474/db/data/node/97/labels",
      "outgoing_relationships" : "http://localhost:7474/db/data/node/97/relationships/out",
      "traverse" : "http://localhost:7474/db/data/node/97/traverse/{returnType}",
      "all_typed_relationships" : "http://localhost:7474/db/data/node/97/relationships/all/{-list|&|types}",
      "self" : "http://localhost:7474/db/data/node/97",
      "property" : "http://localhost:7474/db/data/node/97/properties/{key}",
      "outgoing_typed_relationships" : "http://localhost:7474/db/data/node/97/relationships/out/{-list|&|types}",
      "properties" : "http://localhost:7474/db/data/node/97/properties",
      "incoming_relationships" : "http://localhost:7474/db/data/node/97/relationships/in",
      "create_relationship" : "http://localhost:7474/db/data/node/97/relationships",
      "paged_traverse" : "http://localhost:7474/db/data/node/97/paged/traverse/{returnType}{?pageSize,leaseTime}",
      "all_relationships" : "http://localhost:7474/db/data/node/97/relationships/all",
      "incoming_typed_relationships" : "http://localhost:7474/db/data/node/97/relationships/in/{-list|&|types}",
      "data" : {
        "sequence" : 1,
        "name" : "Tobias"
      },
      "indexed" : "http://localhost:7474/db/data/index/node/people/name/Tobias/97"
    }

### 20.19.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/87`

    {
      "extensions" : {
      },
      "labels" : "http://localhost:7474/db/data/node/87/labels",
      "outgoing_relationships" : "http://localhost:7474/db/data/node/87/relationships/out",
      "traverse" : "http://localhost:7474/db/data/node/87/traverse/{returnType}",
      "all_typed_relationships" : "http://localhost:7474/db/data/node/87/relationships/all/{-list|&|types}",
      "self" : "http://localhost:7474/db/data/node/87",
      "property" : "http://localhost:7474/db/data/node/87/properties/{key}",
      "outgoing_typed_relationships" : "http://localhost:7474/db/data/node/87/relationships/out/{-list|&|types}",
      "properties" : "http://localhost:7474/db/data/node/87/properties",
      "incoming_relationships" : "http://localhost:7474/db/data/node/87/relationships/in",
      "create_relationship" : "http://localhost:7474/db/data/node/87/relationships",
      "paged_traverse" : "http://localhost:7474/db/data/node/87/paged/traverse/{returnType}{?pageSize,leaseTime}",
      "all_relationships" : "http://localhost:7474/db/data/node/87/relationships/all",
      "incoming_typed_relationships" : "http://localhost:7474/db/data/node/87/relationships/in/{-list|&|types}",
      "data" : {
        "sequence" : 1,
        "name" : "Peter"
      },
      "indexed" : "http://localhost:7474/db/data/index/node/people/name/Peter/87"
    }

### 20.19.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/96`

    {
      "extensions" : {
      },
      "labels" : "http://localhost:7474/db/data/node/96/labels",
      "outgoing_relationships" : "http://localhost:7474/db/data/node/96/relationships/out",
      "traverse" : "http://localhost:7474/db/data/node/96/traverse/{returnType}",
      "all_typed_relationships" : "http://localhost:7474/db/data/node/96/relationships/all/{-list|&|types}",
      "self" : "http://localhost:7474/db/data/node/96",
      "property" : "http://localhost:7474/db/data/node/96/properties/{key}",
      "outgoing_typed_relationships" : "http://localhost:7474/db/data/node/96/relationships/out/{-list|&|types}",
      "properties" : "http://localhost:7474/db/data/node/96/properties",
      "incoming_relationships" : "http://localhost:7474/db/data/node/96/relationships/in",
      "create_relationship" : "http://localhost:7474/db/data/node/96/relationships",
      "paged_traverse" : "http://localhost:7474/db/data/node/96/paged/traverse/{returnType}{?pageSize,leaseTime}",
      "all_relationships" : "http://localhost:7474/db/data/node/96/relationships/all",
      "incoming_typed_relationships" : "http://localhost:7474/db/data/node/96/relationships/in/{-list|&|types}",
      "data" : {
        "sequence" : 1,
        "name" : "Tobias"
      },
      "indexed" : "http://localhost:7474/db/data/index/node/people/name/Tobias/96"
    }

### 20.19.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" : {
      },
      "labels" : "http://localhost:7474/db/data/node/81/labels",
      "outgoing_relationships" : "http://localhost:7474/db/data/node/81/relationships/out",
      "traverse" : "http://localhost:7474/db/data/node/81/traverse/{returnType}",
      "all_typed_relationships" : "http://localhost:7474/db/data/node/81/relationships/all/{-list|&|types}",
      "self" : "http://localhost:7474/db/data/node/81",
      "property" : "http://localhost:7474/db/data/node/81/properties/{key}",
      "outgoing_typed_relationships" : "http://localhost:7474/db/data/node/81/relationships/out/{-list|&|types}",
      "properties" : "http://localhost:7474/db/data/node/81/properties",
      "incoming_relationships" : "http://localhost:7474/db/data/node/81/relationships/in",
      "create_relationship" : "http://localhost:7474/db/data/node/81/relationships",
      "paged_traverse" : "http://localhost:7474/db/data/node/81/paged/traverse/{returnType}{?pageSize,leaseTime}",
      "all_relationships" : "http://localhost:7474/db/data/node/81/relationships/all",
      "incoming_typed_relationships" : "http://localhost:7474/db/data/node/81/relationships/in/{-list|&|types}",
      "data" : {
        "sequence" : 1,
        "name" : "Peter"
      },
      "indexed" : "http://localhost:7474/db/data/index/node/people/name/Peter/81"
    }

### 20.19.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/92",
      "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/92`

    {
      "extensions" : {
      },
      "labels" : "http://localhost:7474/db/data/node/92/labels",
      "outgoing_relationships" : "http://localhost:7474/db/data/node/92/relationships/out",
      "traverse" : "http://localhost:7474/db/data/node/92/traverse/{returnType}",
      "all_typed_relationships" : "http://localhost:7474/db/data/node/92/relationships/all/{-list|&|types}",
      "self" : "http://localhost:7474/db/data/node/92",
      "property" : "http://localhost:7474/db/data/node/92/properties/{key}",
      "outgoing_typed_relationships" : "http://localhost:7474/db/data/node/92/relationships/out/{-list|&|types}",
      "properties" : "http://localhost:7474/db/data/node/92/properties",
      "incoming_relationships" : "http://localhost:7474/db/data/node/92/relationships/in",
      "create_relationship" : "http://localhost:7474/db/data/node/92/relationships",
      "paged_traverse" : "http://localhost:7474/db/data/node/92/paged/traverse/{returnType}{?pageSize,leaseTime}",
      "all_relationships" : "http://localhost:7474/db/data/node/92/relationships/all",
      "incoming_typed_relationships" : "http://localhost:7474/db/data/node/92/relationships/in/{-list|&|types}",
      "data" : {
      },
      "indexed" : "http://localhost:7474/db/data/index/node/favorites/some-key/some%20value/92"
    }

### 20.19.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/95",
      "key" : "some-key"
    }

Example response

- `409:` `Conflict`
- `Content-Type:` `application/json; charset=UTF-8`

    {
      "extensions" : {
      },
      "labels" : "http://localhost:7474/db/data/node/94/labels",
      "outgoing_relationships" : "http://localhost:7474/db/data/node/94/relationships/out",
      "traverse" : "http://localhost:7474/db/data/node/94/traverse/{returnType}",
      "all_typed_relationships" : "http://localhost:7474/db/data/node/94/relationships/all/{-list|&|types}",
      "self" : "http://localhost:7474/db/data/node/94",
      "property" : "http://localhost:7474/db/data/node/94/properties/{key}",
      "outgoing_typed_relationships" : "http://localhost:7474/db/data/node/94/relationships/out/{-list|&|types}",
      "properties" : "http://localhost:7474/db/data/node/94/properties",
      "incoming_relationships" : "http://localhost:7474/db/data/node/94/relationships/in",
      "create_relationship" : "http://localhost:7474/db/data/node/94/relationships",
      "paged_traverse" : "http://localhost:7474/db/data/node/94/paged/traverse/{returnType}{?pageSize,leaseTime}",
      "all_relationships" : "http://localhost:7474/db/data/node/94/relationships/all",
      "incoming_typed_relationships" : "http://localhost:7474/db/data/node/94/relationships/in/{-list|&|types}",
      "data" : {
        "some-key" : "some value"
      },
      "indexed" : "http://localhost:7474/db/data/index/node/favorites/some-key/some%20value/94"
    }

### 20.19.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
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/230",
      "end" : "http://localhost:7474/db/data/node/231",
      "type" : "knowledge"
    }

Example response

- `201:` `Created`
- `Content-Type:` `application/json; charset=UTF-8`
- `Location:` `http://localhost:7474/db/data/index/relationship/MyIndex/name/Tobias/169`

    {
      "extensions" : {
      },
      "start" : "http://localhost:7474/db/data/node/230",
      "property" : "http://localhost:7474/db/data/relationship/169/properties/{key}",
      "self" : "http://localhost:7474/db/data/relationship/169",
      "properties" : "http://localhost:7474/db/data/relationship/169/properties",
      "type" : "knowledge",
      "end" : "http://localhost:7474/db/data/node/231",
      "data" : {
        "name" : "Tobias"
      },
      "indexed" : "http://localhost:7474/db/data/index/relationship/MyIndex/name/Tobias/169"
    }

### 20.19.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/234",
      "end" : "http://localhost:7474/db/data/node/235",
      "type" : "KNOWS"
    }

Example response

- `200:` `OK`
- `Content-Type:` `application/json; charset=UTF-8`

    {
      "extensions" : {
      },
      "start" : "http://localhost:7474/db/data/node/232",
      "property" : "http://localhost:7474/db/data/relationship/170/properties/{key}",
      "self" : "http://localhost:7474/db/data/relationship/170",
      "properties" : "http://localhost:7474/db/data/relationship/170/properties",
      "type" : "KNOWS",
      "end" : "http://localhost:7474/db/data/node/233",
      "data" : {
      },
      "indexed" : "http://localhost:7474/db/data/index/relationship/rels/name/Peter/170"
    }

### 20.19.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/242",
      "end" : "http://localhost:7474/db/data/node/243",
      "type" : "KNOWS"
    }

Example response

- `201:` `Created`
- `Content-Type:` `application/json; charset=UTF-8`
- `Location:` `http://localhost:7474/db/data/index/relationship/rels/name/Tobias/173`

    {
      "extensions" : {
      },
      "start" : "http://localhost:7474/db/data/node/242",
      "property" : "http://localhost:7474/db/data/relationship/173/properties/{key}",
      "self" : "http://localhost:7474/db/data/relationship/173",
      "properties" : "http://localhost:7474/db/data/relationship/173/properties",
      "type" : "KNOWS",
      "end" : "http://localhost:7474/db/data/node/243",
      "data" : {
        "name" : "Tobias"
      },
      "indexed" : "http://localhost:7474/db/data/index/relationship/rels/name/Tobias/173"
    }

### 20.19.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/222",
      "end" : "http://localhost:7474/db/data/node/223",
      "type" : "KNOWS"
    }

Example response

- `409:` `Conflict`
- `Content-Type:` `application/json; charset=UTF-8`

    {
      "extensions" : {
      },
      "start" : "http://localhost:7474/db/data/node/220",
      "property" : "http://localhost:7474/db/data/relationship/165/properties/{key}",
      "self" : "http://localhost:7474/db/data/relationship/165",
      "properties" : "http://localhost:7474/db/data/relationship/165/properties",
      "type" : "KNOWS",
      "end" : "http://localhost:7474/db/data/node/221",
      "data" : {
      },
      "indexed" : "http://localhost:7474/db/data/index/relationship/rels/name/Peter/165"
    }

### 20.19.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
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/164"
    }

Example response

- `201:` `Created`
- `Content-Type:` `application/json; charset=UTF-8`
- `Location:` `http://localhost:7474/db/data/index/relationship/rels/name/Peter/164`

    {
      "extensions" : {
      },
      "start" : "http://localhost:7474/db/data/node/218",
      "property" : "http://localhost:7474/db/data/relationship/164/properties/{key}",
      "self" : "http://localhost:7474/db/data/relationship/164",
      "properties" : "http://localhost:7474/db/data/relationship/164/properties",
      "type" : "KNOWS",
      "end" : "http://localhost:7474/db/data/node/219",
      "data" : {
      },
      "indexed" : "http://localhost:7474/db/data/index/relationship/rels/name/Peter/164"
    }

### 20.19.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/167"
    }

Example response

- `409:` `Conflict`
- `Content-Type:` `application/json; charset=UTF-8`

    {
      "extensions" : {
      },
      "start" : "http://localhost:7474/db/data/node/224",
      "property" : "http://localhost:7474/db/data/relationship/166/properties/{key}",
      "self" : "http://localhost:7474/db/data/relationship/166",
      "properties" : "http://localhost:7474/db/data/relationship/166/properties",
      "type" : "KNOWS",
      "end" : "http://localhost:7474/db/data/node/225",
      "data" : {
      },
      "indexed" : "http://localhost:7474/db/data/index/relationship/rels/name/Peter/166"
    }

Copyright © 2014 Neo Technology

Prev | Up | Next
| Home |