18.11. Unique Indexes

18.11.1. Get or create unique node (create)
18.11.2. Get or create unique node (existing)
18.11.3. Create a unique node or return fail (create)
18.11.4. Create a unique node or return fail (fail)
18.11.5. Get or create unique relationship (create)
18.11.6. Get or create unique relationship (existing)
18.11.7. Create a unique relationship or return fail (create)
18.11.8. Create a unique relationship or return fail (fail)

For uniqueness enforcements, there are two modes:

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

18.11.1. Get or create unique node (create)

Node are created if they don’t exist in the unique index already.

Figure 18.57. Final Graph


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

18.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.

Figure 18.58. Final Graph


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

18.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.

Figure 18.59. Final Graph


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

18.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.

Figure 18.60. Final Graph


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

18.11.5. Get or create unique relationship (create)

Add a relationship to an index unless a relationship already exists for the given mapping. Here, no previous relationship is found in the index, a new one is created and indexed.

Figure 18.61. Final Graph


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" : "Mattias",
  "uri" : "http://localhost:7474/db/data/relationship/98"
}

Example response

  • 201: Created
  • Content-Type: application/json
  • Location: http://localhost:7474/db/data/index/relationship/knowledge/name/Mattias/98
{
  "extensions" : {
  },
  "start" : "http://localhost:7474/db/data/node/185",
  "property" : "http://localhost:7474/db/data/relationship/98/properties/{key}",
  "self" : "http://localhost:7474/db/data/relationship/98",
  "properties" : "http://localhost:7474/db/data/relationship/98/properties",
  "type" : "knowledge",
  "end" : "http://localhost:7474/db/data/node/186",
  "data" : {
  },
  "indexed" : "http://localhost:7474/db/data/index/relationship/knowledge/name/Mattias/98"
}

18.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.

Figure 18.62. Final Graph


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/189",
  "end" : "http://localhost:7474/db/data/node/190",
  "type" : "KNOWS"
}

Example response

  • 200: OK
  • Content-Type: application/json
{
  "extensions" : {
  },
  "start" : "http://localhost:7474/db/data/node/187",
  "property" : "http://localhost:7474/db/data/relationship/99/properties/{key}",
  "self" : "http://localhost:7474/db/data/relationship/99",
  "properties" : "http://localhost:7474/db/data/relationship/99/properties",
  "type" : "KNOWS",
  "end" : "http://localhost:7474/db/data/node/188",
  "data" : {
  },
  "indexed" : "http://localhost:7474/db/data/index/relationship/rels/name/Peter/99"
}

18.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.

Figure 18.63. Final Graph


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/191",
  "end" : "http://localhost:7474/db/data/node/192",
  "type" : "KNOWS"
}

Example response

  • 201: Created
  • Content-Type: application/json
  • Location: http://localhost:7474/db/data/index/relationship/rels/name/Tobias/100
{
  "extensions" : {
  },
  "start" : "http://localhost:7474/db/data/node/191",
  "property" : "http://localhost:7474/db/data/relationship/100/properties/{key}",
  "self" : "http://localhost:7474/db/data/relationship/100",
  "properties" : "http://localhost:7474/db/data/relationship/100/properties",
  "type" : "KNOWS",
  "end" : "http://localhost:7474/db/data/node/192",
  "data" : {
    "name" : "Tobias"
  },
  "indexed" : "http://localhost:7474/db/data/index/relationship/rels/name/Tobias/100"
}

18.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.

Figure 18.64. Final Graph


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/195",
  "end" : "http://localhost:7474/db/data/node/196",
  "type" : "KNOWS"
}

Example response

  • 409: Conflict
  • Content-Type: application/json
{
  "extensions" : {
  },
  "start" : "http://localhost:7474/db/data/node/193",
  "property" : "http://localhost:7474/db/data/relationship/101/properties/{key}",
  "self" : "http://localhost:7474/db/data/relationship/101",
  "properties" : "http://localhost:7474/db/data/relationship/101/properties",
  "type" : "KNOWS",
  "end" : "http://localhost:7474/db/data/node/194",
  "data" : {
  },
  "indexed" : "http://localhost:7474/db/data/index/relationship/rels/name/Peter/101"
}