Skip to content

Queries

A query describes the result you want: where to start, which relations to follow and how to name the values.

Example

Starting from people, return each name along with the name of their organizations:

{
  "meta": {
    "type": "https://example.org/types/Person",
    "name": "People and their organizations",
    "description": "Used by the staff page"
  },
  "structure": [
    {
      "propertyName": "name",
      "path": "https://example.org/properties/name",
      "required": true
    },
    {
      "propertyName": "organizations",
      "path": "https://example.org/properties/affiliation",
      "structure": [
        { "propertyName": "organization", "path": "https://example.org/properties/name" }
      ]
    }
  ]
}

Result:

[
  {
    "name": "Ada Lovelace",
    "organizations": [ { "organization": "Analytical Society" } ]
  }
]

Query format

meta

Key Required Description
type Yes IRI of the type to start from
name, description No Free text, used when listing queries

structure items

Key Description
propertyName Key in the result. Defaults to the last property of the path.
path What to follow (see below)
structure Nested items, evaluated on each instance reached
required Drops the result if there is no value (see how values are evaluated)
filter Condition on the value
singleValue FIRST takes the first value. CONCAT joins plain values with ", ".
ensureOrder Keeps the order of the links in the payload, including duplicates
sort Accepted and stored, but not evaluated. Results are ordered by the ID of the root instance.

Paths

A path is one of these:

{ "path": "https://example.org/properties/name" }

Follow a link backwards and keep only certain types:

{
  "path": {
    "property": "https://example.org/properties/author",
    "reverse": true,
    "typeFilter": ["https://example.org/types/Dataset"]
  }
}
{
  "path": [
    "https://example.org/properties/affiliation",
    "https://example.org/properties/name"
  ]
}

Use "@id" as a property to return an instance's ID.

ensureOrder only applies to forward hops, as reverse hops have no defined order. Without ensureOrder, the values and nested objects of an item are returned without duplicates.

Filters

{ "filter": { "op": "CONTAINS", "value": "brain" } }
op Matches when the value...
EQUALS equals the given text
CONTAINS contains it
STARTS_WITH starts with it
ENDS_WITH ends with it
REGEX matches the regular expression in full
IS_EMPTY is empty

Filters are case-insensitive. A list matches if one of its elements matches.

To supply the value at execution time, use a parameter:

{ "filter": { "op": "EQUALS", "parameter": "lastName" } }

Pass it as a request parameter, for example ?lastName=Lovelace. parameter takes precedence over value. A filter that ends up with no value doesn't restrict the result.

How filters and required apply

Level A failing filter A missing required value
Top level Drops the whole result Drops the whole result
Nested Removes the value Removes the nested object

Empty values, lists and objects are left out of the result.

Working with queries

Action Request Permission
Run without saving POST /v4/queries?stage=... Any user. Results are restricted.
Save or update PUT /v4/queries/{uuid}?space=... CREATE to add, WRITE to update
Read GET /v4/queries/{uuid} READ in its space
List GET /v4/queries?type=...&search=... Lists readable spaces only
Execute a saved query GET /v4/queries/{uuid}/instances?stage=... READ in its space
Move to another space PUT /v4/queries/{uuid}/spaces/{space} WRITE in both
Delete DELETE /v4/queries/{uuid} DELETE in its space

space is only needed when saving a query for the first time. If that space doesn't exist yet, it is created if you may manage it.

search looks in the query's name and description.

Execution parameters

Parameter Description
stage Required: IN_PROGRESS or RELEASED
size, marker, returnTotalResults Pagination
instanceId Evaluates the query for this root instance only
restrictToSpaces Limits the result to these spaces
any other parameter Value for a filter parameter

What results contain

Results contain only what you may read: instances in spaces you can read, plus instances you are invited to in the in-progress stage. An instance you can't read is treated as if it didn't exist, at every level of the query.

Performance

Without a graph database, queries are evaluated directly in PostgreSQL. They are always consistent with the data, but become slower for types with many instances, especially if the top level has filters or required items. If you don't need the total, setting returnTotalResults=false helps.

With Neo4j, only the instances of the requested page are evaluated. Exceptions are top-level required items with a nested structure and top-level filters or required items on a path with several steps: they are evaluated for every instance of the type to find the page. Filters and required items on direct properties of the top level don't have this cost. Always set a size - without it, every result is returned at once.