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. Get or create unique relationship (create)
22.11.6. Get or create unique relationship (existing)
22.11.7. Create a unique relationship or return fail (create)
22.11.8. Create a unique relationship or return fail (fail)

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
  • Content-Type: application/json
{
  "key" : "name",
  "value" : "Tobias",
  "properties" : {
    "name" : "Tobias",
    "sequence" : 1
  }
}

Example response

  • 201: Created
  • Content-Type: application/json
  • Location: http://localhost:7474/db/data/index/node/people/name/Tobias/18
{
  "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" : {
    "sequence" : 1,
    "name" : "Tobias"
  },
  "indexed" : "http://localhost:7474/db/data/index/node/people/name/Tobias/18"
}

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
  • Content-Type: application/json
{
  "key" : "name",
  "value" : "Peter",
  "properties" : {
    "name" : "Peter",
    "sequence" : 2
  }
}

Example response

  • 200: OK
  • Content-Type: application/json
  • 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
  • Content-Type: application/json
{
  "key" : "name",
  "value" : "Tobias",
  "properties" : {
    "name" : "Tobias",
    "sequence" : 1
  }
}

Example response

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

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
  • Content-Type: application/json
{
  "key" : "name",
  "value" : "Peter",
  "properties" : {
    "name" : "Peter",
    "sequence" : 2
  }
}

Example response

  • 409: Conflict
  • Content-Type: application/json
{
  "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. 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
  • Content-Type: application/json
{
  "key" : "name",
  "value" : "Tobias",
  "start" : "http://localhost:7474/db/data/node/425",
  "end" : "http://localhost:7474/db/data/node/426",
  "type" : "knowledge"
}

Example response

  • 201: Created
  • Content-Type: application/json
  • Location: http://localhost:7474/db/data/index/relationship/knowledge/name/Tobias/239
{
  "extensions" : {
  },
  "start" : "http://localhost:7474/db/data/node/425",
  "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/426",
  "data" : {
    "name" : "Tobias"
  },
  "indexed" : "http://localhost:7474/db/data/index/relationship/knowledge/name/Tobias/239"
}

22.11.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
  • Content-Type: application/json
{
  "key" : "name",
  "value" : "Peter",
  "start" : "http://localhost:7474/db/data/node/429",
  "end" : "http://localhost:7474/db/data/node/430",
  "type" : "KNOWS"
}

Example response

  • 200: OK
  • Content-Type: application/json
{
  "extensions" : {
  },
  "start" : "http://localhost:7474/db/data/node/427",
  "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/428",
  "data" : {
  },
  "indexed" : "http://localhost:7474/db/data/index/relationship/rels/name/Peter/240"
}

22.11.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
  • Content-Type: application/json
{
  "key" : "name",
  "value" : "Tobias",
  "start" : "http://localhost:7474/db/data/node/437",
  "end" : "http://localhost:7474/db/data/node/438",
  "type" : "KNOWS"
}

Example response

  • 201: Created
  • Content-Type: application/json
  • Location: http://localhost:7474/db/data/index/relationship/rels/name/Tobias/243
{
  "extensions" : {
  },
  "start" : "http://localhost:7474/db/data/node/437",
  "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/438",
  "data" : {
    "name" : "Tobias"
  },
  "indexed" : "http://localhost:7474/db/data/index/relationship/rels/name/Tobias/243"
}

22.11.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
  • Content-Type: application/json
{
  "key" : "name",
  "value" : "Peter",
  "start" : "http://localhost:7474/db/data/node/419",
  "end" : "http://localhost:7474/db/data/node/420",
  "type" : "KNOWS"
}

Example response

  • 409: Conflict
  • Content-Type: application/json
{
  "extensions" : {
  },
  "start" : "http://localhost:7474/db/data/node/417",
  "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/418",
  "data" : {
  },
  "indexed" : "http://localhost:7474/db/data/index/relationship/rels/name/Peter/236"
}