19.8. 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 19.19. Final Graph

Example request

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

Example response

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

Create relationship

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

Figure 19.20. 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/9",
  "type" : "LOVES"
}

Example response

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

Create a relationship with properties

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

Figure 19.21. Starting Graph

Figure 19.22. Final Graph

Example request

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

Example response

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

Delete relationship

Figure 19.23. Starting Graph

Figure 19.24. Final Graph

Example request

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

Example response

  • 204: No Content

Get all properties on a relationship

Figure 19.25. Final Graph

Example request

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

Example response

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

Set all properties on a relationship

Figure 19.26. Starting Graph

Figure 19.27. Final Graph

Example request

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

Example request

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

Figure 19.30. Final Graph

Example request

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

Example response

  • 204: No Content

Get all relationships

Figure 19.31. Final Graph

Example request

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

Example response

  • 200: OK
  • Content-Type: application/json; charset=UTF-8
[ {
  "start" : "http://localhost:7474/db/data/node/73",
  "data" : {
  },
  "self" : "http://localhost:7474/db/data/relationship/29",
  "property" : "http://localhost:7474/db/data/relationship/29/properties/{key}",
  "properties" : "http://localhost:7474/db/data/relationship/29/properties",
  "type" : "LIKES",
  "extensions" : {
  },
  "end" : "http://localhost:7474/db/data/node/74"
}, {
  "start" : "http://localhost:7474/db/data/node/75",
  "data" : {
  },
  "self" : "http://localhost:7474/db/data/relationship/30",
  "property" : "http://localhost:7474/db/data/relationship/30/properties/{key}",
  "properties" : "http://localhost:7474/db/data/relationship/30/properties",
  "type" : "LIKES",
  "extensions" : {
  },
  "end" : "http://localhost:7474/db/data/node/73"
}, {
  "start" : "http://localhost:7474/db/data/node/73",
  "data" : {
  },
  "self" : "http://localhost:7474/db/data/relationship/31",
  "property" : "http://localhost:7474/db/data/relationship/31/properties/{key}",
  "properties" : "http://localhost:7474/db/data/relationship/31/properties",
  "type" : "HATES",
  "extensions" : {
  },
  "end" : "http://localhost:7474/db/data/node/76"
} ]

Get incoming relationships

Figure 19.32. Final Graph

Example request

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

Example response

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

Get outgoing relationships

Figure 19.33. Final Graph

Example request

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

Example response

  • 200: OK
  • Content-Type: application/json; charset=UTF-8
[ {
  "start" : "http://localhost:7474/db/data/node/117",
  "data" : {
  },
  "self" : "http://localhost:7474/db/data/relationship/55",
  "property" : "http://localhost:7474/db/data/relationship/55/properties/{key}",
  "properties" : "http://localhost:7474/db/data/relationship/55/properties",
  "type" : "LIKES",
  "extensions" : {
  },
  "end" : "http://localhost:7474/db/data/node/118"
}, {
  "start" : "http://localhost:7474/db/data/node/117",
  "data" : {
  },
  "self" : "http://localhost:7474/db/data/relationship/57",
  "property" : "http://localhost:7474/db/data/relationship/57/properties/{key}",
  "properties" : "http://localhost:7474/db/data/relationship/57/properties",
  "type" : "HATES",
  "extensions" : {
  },
  "end" : "http://localhost:7474/db/data/node/120"
} ]

Get typed relationships

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

Figure 19.34. Final Graph

Example request

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

Example response

  • 200: OK
  • Content-Type: application/json; charset=UTF-8
[ {
  "start" : "http://localhost:7474/db/data/node/48",
  "data" : {
  },
  "self" : "http://localhost:7474/db/data/relationship/14",
  "property" : "http://localhost:7474/db/data/relationship/14/properties/{key}",
  "properties" : "http://localhost:7474/db/data/relationship/14/properties",
  "type" : "LIKES",
  "extensions" : {
  },
  "end" : "http://localhost:7474/db/data/node/49"
}, {
  "start" : "http://localhost:7474/db/data/node/50",
  "data" : {
  },
  "self" : "http://localhost:7474/db/data/relationship/15",
  "property" : "http://localhost:7474/db/data/relationship/15/properties/{key}",
  "properties" : "http://localhost:7474/db/data/relationship/15/properties",
  "type" : "LIKES",
  "extensions" : {
  },
  "end" : "http://localhost:7474/db/data/node/48"
}, {
  "start" : "http://localhost:7474/db/data/node/48",
  "data" : {
  },
  "self" : "http://localhost:7474/db/data/relationship/16",
  "property" : "http://localhost:7474/db/data/relationship/16/properties/{key}",
  "properties" : "http://localhost:7474/db/data/relationship/16/properties",
  "type" : "HATES",
  "extensions" : {
  },
  "end" : "http://localhost:7474/db/data/node/51"
} ]

Get relationships on a node without relationships

Figure 19.35. Final Graph

Example request

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

Example response

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