21.9. Relationships

Relationships are a first class citizen in the Neo4j REST API. They can be accessed either stand-alone or through the nodes they are attached to.

The general pattern to get relationships from a node is:

GET http://localhost:7474/db/data/node/123/relationships/{dir}/{-list|&|types}

Where dir is one of all, in, out and types is an ampersand-separated list of types. See the examples below for more information.

Get Relationship by ID

Figure 21.19. Final Graph

Example request

  • GET http://localhost:7474/db/data/relationship/46
  • Accept: application/json; charset=UTF-8

Example response

  • 200: OK
  • Content-Type: application/json; charset=UTF-8
{
  "extensions" : { },
  "metadata" : {
    "id" : 46,
    "type" : "know"
  },
  "start" : "http://localhost:7474/db/data/node/70",
  "property" : "http://localhost:7474/db/data/relationship/46/properties/{key}",
  "self" : "http://localhost:7474/db/data/relationship/46",
  "end" : "http://localhost:7474/db/data/node/71",
  "type" : "know",
  "properties" : "http://localhost:7474/db/data/relationship/46/properties",
  "data" : { }
}

Create relationship

Upon successful creation of a relationship, the new relationship is returned.

Figure 21.20. Starting Graph

Figure 21.21. Final Graph

Example request

  • POST http://localhost:7474/db/data/node/0/relationships
  • Accept: application/json; charset=UTF-8
  • Content-Type: application/json
{
  "to" : "http://localhost:7474/db/data/node/1",
  "type" : "LOVES"
}

Example response

  • 201: Created
  • Content-Type: application/json; charset=UTF-8
  • Location: http://localhost:7474/db/data/relationship/1
{
  "extensions" : { },
  "metadata" : {
    "id" : 1,
    "type" : "LOVES"
  },
  "property" : "http://localhost:7474/db/data/relationship/1/properties/{key}",
  "start" : "http://localhost:7474/db/data/node/0",
  "self" : "http://localhost:7474/db/data/relationship/1",
  "end" : "http://localhost:7474/db/data/node/1",
  "type" : "LOVES",
  "properties" : "http://localhost:7474/db/data/relationship/1/properties",
  "data" : { }
}

Create a relationship with properties

Upon successful creation of a relationship, the new relationship is returned.

Figure 21.22. Starting Graph

Figure 21.23. Final Graph

Example request

  • POST http://localhost:7474/db/data/node/10/relationships
  • Accept: application/json; charset=UTF-8
  • Content-Type: application/json
{
  "to" : "http://localhost:7474/db/data/node/11",
  "type" : "LOVES",
  "data" : {
    "foo" : "bar"
  }
}

Example response

  • 201: Created
  • Content-Type: application/json; charset=UTF-8
  • Location: http://localhost:7474/db/data/relationship/8
{
  "extensions" : { },
  "metadata" : {
    "id" : 8,
    "type" : "LOVES"
  },
  "property" : "http://localhost:7474/db/data/relationship/8/properties/{key}",
  "start" : "http://localhost:7474/db/data/node/10",
  "self" : "http://localhost:7474/db/data/relationship/8",
  "end" : "http://localhost:7474/db/data/node/11",
  "type" : "LOVES",
  "properties" : "http://localhost:7474/db/data/relationship/8/properties",
  "data" : {
    "foo" : "bar"
  }
}

Delete relationship

Figure 21.24. Starting Graph

Figure 21.25. Final Graph

Example request

  • DELETE http://localhost:7474/db/data/relationship/39
  • Accept: application/json; charset=UTF-8

Example response

  • 204: No Content

Get all properties on a relationship

Figure 21.26. Final Graph

Example request

  • GET http://localhost:7474/db/data/relationship/42/properties
  • Accept: application/json; charset=UTF-8

Example response

  • 200: OK
  • Content-Type: application/json; charset=UTF-8
{
  "cost" : "high",
  "since" : "1day"
}

Set all properties on a relationship

Figure 21.27. Starting Graph

Figure 21.28. Final Graph

Example request

  • PUT http://localhost:7474/db/data/relationship/47/properties
  • Accept: application/json; charset=UTF-8
  • Content-Type: application/json
{
  "happy" : false
}

Example response

  • 204: No Content

Get single property on a relationship

Figure 21.29. Final Graph

Example request

  • GET http://localhost:7474/db/data/relationship/43/properties/cost
  • Accept: application/json; charset=UTF-8

Example response

  • 200: OK
  • Content-Type: application/json; charset=UTF-8
"high"

Set single property on a relationship

Figure 21.30. Starting Graph

Figure 21.31. Final Graph

Example request

  • PUT http://localhost:7474/db/data/relationship/41/properties/cost
  • Accept: application/json; charset=UTF-8
  • Content-Type: application/json
"deadly"

Example response

  • 204: No Content

Get all relationships

Figure 21.32. Final Graph

Example request

  • GET http://localhost:7474/db/data/node/108/relationships/all
  • Accept: application/json; charset=UTF-8

Example response

  • 200: OK
  • Content-Type: application/json; charset=UTF-8
[ {
  "extensions" : { },
  "metadata" : {
    "id" : 35,
    "type" : "HATES"
  },
  "data" : { },
  "start" : "http://localhost:7474/db/data/node/108",
  "property" : "http://localhost:7474/db/data/relationship/35/properties/{key}",
  "self" : "http://localhost:7474/db/data/relationship/35",
  "end" : "http://localhost:7474/db/data/node/111",
  "type" : "HATES",
  "properties" : "http://localhost:7474/db/data/relationship/35/properties"
}, {
  "extensions" : { },
  "metadata" : {
    "id" : 34,
    "type" : "LIKES"
  },
  "data" : { },
  "start" : "http://localhost:7474/db/data/node/110",
  "property" : "http://localhost:7474/db/data/relationship/34/properties/{key}",
  "self" : "http://localhost:7474/db/data/relationship/34",
  "end" : "http://localhost:7474/db/data/node/108",
  "type" : "LIKES",
  "properties" : "http://localhost:7474/db/data/relationship/34/properties"
}, {
  "extensions" : { },
  "metadata" : {
    "id" : 33,
    "type" : "LIKES"
  },
  "data" : { },
  "start" : "http://localhost:7474/db/data/node/108",
  "property" : "http://localhost:7474/db/data/relationship/33/properties/{key}",
  "self" : "http://localhost:7474/db/data/relationship/33",
  "end" : "http://localhost:7474/db/data/node/109",
  "type" : "LIKES",
  "properties" : "http://localhost:7474/db/data/relationship/33/properties"
} ]

Get incoming relationships

Figure 21.33. Final Graph

Example request

  • GET http://localhost:7474/db/data/node/127/relationships/in
  • Accept: application/json; charset=UTF-8

Example response

  • 200: OK
  • Content-Type: application/json; charset=UTF-8
[ {
  "extensions" : { },
  "metadata" : {
    "id" : 45,
    "type" : "LIKES"
  },
  "data" : { },
  "start" : "http://localhost:7474/db/data/node/129",
  "property" : "http://localhost:7474/db/data/relationship/45/properties/{key}",
  "self" : "http://localhost:7474/db/data/relationship/45",
  "end" : "http://localhost:7474/db/data/node/127",
  "type" : "LIKES",
  "properties" : "http://localhost:7474/db/data/relationship/45/properties"
} ]

Get outgoing relationships

Figure 21.34. Final Graph

Example request

  • GET http://localhost:7474/db/data/node/152/relationships/out
  • Accept: application/json; charset=UTF-8

Example response

  • 200: OK
  • Content-Type: application/json; charset=UTF-8
[ {
  "extensions" : { },
  "metadata" : {
    "id" : 61,
    "type" : "HATES"
  },
  "data" : { },
  "start" : "http://localhost:7474/db/data/node/152",
  "property" : "http://localhost:7474/db/data/relationship/61/properties/{key}",
  "self" : "http://localhost:7474/db/data/relationship/61",
  "end" : "http://localhost:7474/db/data/node/155",
  "type" : "HATES",
  "properties" : "http://localhost:7474/db/data/relationship/61/properties"
}, {
  "extensions" : { },
  "metadata" : {
    "id" : 59,
    "type" : "LIKES"
  },
  "data" : { },
  "start" : "http://localhost:7474/db/data/node/152",
  "property" : "http://localhost:7474/db/data/relationship/59/properties/{key}",
  "self" : "http://localhost:7474/db/data/relationship/59",
  "end" : "http://localhost:7474/db/data/node/153",
  "type" : "LIKES",
  "properties" : "http://localhost:7474/db/data/relationship/59/properties"
} ]

Get typed relationships

Note that the "&" needs to be encoded like "%26" for example when using cURL from the terminal.

Figure 21.35. Final Graph

Example request

  • GET http://localhost:7474/db/data/node/83/relationships/all/LIKES&HATES
  • Accept: application/json; charset=UTF-8

Example response

  • 200: OK
  • Content-Type: application/json; charset=UTF-8
[ {
  "extensions" : { },
  "metadata" : {
    "id" : 20,
    "type" : "HATES"
  },
  "data" : { },
  "start" : "http://localhost:7474/db/data/node/83",
  "property" : "http://localhost:7474/db/data/relationship/20/properties/{key}",
  "self" : "http://localhost:7474/db/data/relationship/20",
  "end" : "http://localhost:7474/db/data/node/86",
  "type" : "HATES",
  "properties" : "http://localhost:7474/db/data/relationship/20/properties"
}, {
  "extensions" : { },
  "metadata" : {
    "id" : 19,
    "type" : "LIKES"
  },
  "data" : { },
  "start" : "http://localhost:7474/db/data/node/85",
  "property" : "http://localhost:7474/db/data/relationship/19/properties/{key}",
  "self" : "http://localhost:7474/db/data/relationship/19",
  "end" : "http://localhost:7474/db/data/node/83",
  "type" : "LIKES",
  "properties" : "http://localhost:7474/db/data/relationship/19/properties"
}, {
  "extensions" : { },
  "metadata" : {
    "id" : 18,
    "type" : "LIKES"
  },
  "data" : { },
  "start" : "http://localhost:7474/db/data/node/83",
  "property" : "http://localhost:7474/db/data/relationship/18/properties/{key}",
  "self" : "http://localhost:7474/db/data/relationship/18",
  "end" : "http://localhost:7474/db/data/node/84",
  "type" : "LIKES",
  "properties" : "http://localhost:7474/db/data/relationship/18/properties"
} ]

Get relationships on a node without relationships

Figure 21.36. Final Graph

Example request

  • GET http://localhost:7474/db/data/node/141/relationships/all
  • Accept: application/json; charset=UTF-8

Example response

  • 200: OK
  • Content-Type: application/json; charset=UTF-8
[ ]