26.18. Unique Indexing

26.18.1. Get or create unique node (create)
26.18.2. Get or create unique node (existing)
26.18.3. Create a unique node or return fail (create)
26.18.4. Create a unique node or return fail (fail)
26.18.5. Get or create unique relationship (create)
26.18.6. Get or create unique relationship (existing)
26.18.7. Create a unique relationship or return fail (create)
26.18.8. Create a unique relationship or return fail (fail)
[Note]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 uniqueness enforcements, there are two modes:

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

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

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

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

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

26.18.5. Get or create unique relationship (create)

Create a unique relationship in an index.

Example request

  • POST http://localhost:7474/db/data/index/relationship/knowledge/?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/429",
  "end" : "http://localhost:7474/db/data/node/430",
  "type" : "knowledge"
}

Example response

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

26.18.6. 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/433",
  "end" : "http://localhost:7474/db/data/node/434",
  "type" : "KNOWS"
}

Example response

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

26.18.7. 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/441",
  "end" : "http://localhost:7474/db/data/node/442",
  "type" : "KNOWS"
}

Example response

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

26.18.8. 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/423",
  "end" : "http://localhost:7474/db/data/node/424",
  "type" : "KNOWS"
}

Example response

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