20.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 20.19. Final Graph

Example request

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

Example response

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

Create relationship

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

Figure 20.20. Starting Graph

Figure 20.21. Final Graph

Example request

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

Example response

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

Create a relationship with properties

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

Figure 20.22. Starting Graph

Figure 20.23. Final Graph

Example request

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

Example response

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

Delete relationship

Figure 20.24. Starting Graph

Figure 20.25. Final Graph

Example request

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

Example response

  • 204: No Content

Get all properties on a relationship

Figure 20.26. Final Graph

Example request

  • GET http://localhost:7474/db/data/relationship/25/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 20.27. Starting Graph

Figure 20.28. Final Graph

Example request

  • PUT http://localhost:7474/db/data/relationship/30/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 20.29. Final Graph

Example request

  • GET http://localhost:7474/db/data/relationship/26/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 20.30. Starting Graph

Figure 20.31. Final Graph

Example request

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

Example response

  • 204: No Content

Get all relationships

Figure 20.32. Final Graph

Example request

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

Example response

  • 200: OK
  • Content-Type: application/json; charset=UTF-8
[ {
  "extensions" : { },
  "metadata" : {
    "id" : 193,
    "type" : "HATES"
  },
  "data" : { },
  "start" : "http://localhost:7474/db/data/node/285",
  "property" : "http://localhost:7474/db/data/relationship/193/properties/{key}",
  "self" : "http://localhost:7474/db/data/relationship/193",
  "end" : "http://localhost:7474/db/data/node/288",
  "type" : "HATES",
  "properties" : "http://localhost:7474/db/data/relationship/193/properties"
}, {
  "extensions" : { },
  "metadata" : {
    "id" : 192,
    "type" : "LIKES"
  },
  "data" : { },
  "start" : "http://localhost:7474/db/data/node/287",
  "property" : "http://localhost:7474/db/data/relationship/192/properties/{key}",
  "self" : "http://localhost:7474/db/data/relationship/192",
  "end" : "http://localhost:7474/db/data/node/285",
  "type" : "LIKES",
  "properties" : "http://localhost:7474/db/data/relationship/192/properties"
}, {
  "extensions" : { },
  "metadata" : {
    "id" : 191,
    "type" : "LIKES"
  },
  "data" : { },
  "start" : "http://localhost:7474/db/data/node/285",
  "property" : "http://localhost:7474/db/data/relationship/191/properties/{key}",
  "self" : "http://localhost:7474/db/data/relationship/191",
  "end" : "http://localhost:7474/db/data/node/286",
  "type" : "LIKES",
  "properties" : "http://localhost:7474/db/data/relationship/191/properties"
} ]

Get incoming relationships

Figure 20.33. Final Graph

Example request

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

Example response

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

Get outgoing relationships

Figure 20.34. Final Graph

Example request

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

Example response

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

Get typed relationships

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

Figure 20.35. Final Graph

Example request

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

Example response

  • 200: OK
  • Content-Type: application/json; charset=UTF-8
[ {
  "extensions" : { },
  "metadata" : {
    "id" : 178,
    "type" : "HATES"
  },
  "data" : { },
  "start" : "http://localhost:7474/db/data/node/260",
  "property" : "http://localhost:7474/db/data/relationship/178/properties/{key}",
  "self" : "http://localhost:7474/db/data/relationship/178",
  "end" : "http://localhost:7474/db/data/node/263",
  "type" : "HATES",
  "properties" : "http://localhost:7474/db/data/relationship/178/properties"
}, {
  "extensions" : { },
  "metadata" : {
    "id" : 177,
    "type" : "LIKES"
  },
  "data" : { },
  "start" : "http://localhost:7474/db/data/node/262",
  "property" : "http://localhost:7474/db/data/relationship/177/properties/{key}",
  "self" : "http://localhost:7474/db/data/relationship/177",
  "end" : "http://localhost:7474/db/data/node/260",
  "type" : "LIKES",
  "properties" : "http://localhost:7474/db/data/relationship/177/properties"
}, {
  "extensions" : { },
  "metadata" : {
    "id" : 176,
    "type" : "LIKES"
  },
  "data" : { },
  "start" : "http://localhost:7474/db/data/node/260",
  "property" : "http://localhost:7474/db/data/relationship/176/properties/{key}",
  "self" : "http://localhost:7474/db/data/relationship/176",
  "end" : "http://localhost:7474/db/data/node/261",
  "type" : "LIKES",
  "properties" : "http://localhost:7474/db/data/relationship/176/properties"
} ]

Get relationships on a node without relationships

Figure 20.36. Final Graph

Example request

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

Example response

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