Developers / Guides

Guide

Search

Build a search from the catalogue, count it, read it a page at a time, and come back to its results for 90 days.

A search finds the entities that meet every condition you send, in the layers you name. Take the fields and operators from the catalogue, count the search, then read its rows a page at a time. Every search that finds something is saved for 90 days, so you can page through it again, filter within it or leave its results out of the next search. The API reference describes every field of the request and the row; this guide says how they fit together.

The catalogue

GET /v1/catalogue lists what a search over the layers you name can use: fields, each with its key, type, operators, options or an optionsToken, narrowing, levels, history (its past is kept) and, where the field is measured across cohorts, benchmark with the cohorts it offers; sortOptions; resultColumns, what a row's values hold; narrowingFields; and relations. Name the same layers the search will, or send layers=subscribed for every layer the consumer is subscribed to. Core fields such as name, postcode, region, localAuthority, country and level come first, then each layer's fields under its key, such as cqc.rating.

A field with a long list of values, such as region, carries an optionsToken in place of options: read the values with GET /v1/catalogue/options/{token}, with the same layers. Read the catalogue again when surface or release changes.

GET /v1/catalogue?layers=cqc,companies_house: one field, trimmed
{
  "key": "region",
  "label": "Region",
  "type": "enum",
  "layer": null,
  "optionsToken": "region",
  "operators": ["equals", "not_equals", "in", "not_in", "is_set", "is_not_set"],
  "narrowing": true,
  "levels": ["organisation", "premises"]
}

Operators by field type

A condition is a field, an operator and a value. The operators a field takes follow its type, and the catalogue lists them on each field. A search takes 1 to 20 conditions, and a condition the catalogue does not allow is refused with 400 CONDITIONS_INVALID.

Type Operators Value
enum equals, not_equals, in, not_in, is_set, is_not_set an option, or a list of options for in and not_in
string equals, not_equals, in, not_in, contains, starts_with, is_set, is_not_set text, or a list for in and not_in
number equals, not_equals, gt, gte, lt, lte, between, is_set, is_not_set a number, or [from, to] for between
boolean equals, is_set, is_not_set true or false
array contains, not_contains, is_set, is_not_set one item
date older_than_days, newer_than_days, before, after, is_set, is_not_set a number of days, or a date YYYY-MM-DD

Leave value out for is_set and is_not_set. On name, contains matches words in any order. On postcode, starts_with takes an outward code or a district and equals a whole postcode.

Conditions
[
  { "field": "region", "operator": "equals", "value": "South East" },
  { "field": "cqc.rating", "operator": "in", "value": ["Requires Improvement", "Inadequate"] },
  { "field": "cqc.beds", "operator": "between", "value": [20, 60] },
  { "field": "name", "operator": "contains", "value": "oakfield" }
]

Regions and local authorities

region and localAuthority take the names the catalogue lists as options, and an earlier name of the same area is accepted too. region is the ONS region of the entity's postcode. localAuthority is its ONS area or the register's own words, which can name a county such as Kent. is_not_set finds an entity with no place. GET /v1/changelog says when the reading of a place name changes.

When a field's values are tidied

A field's values may be tidied: one spelling for several, or a word that comes to count as no value. A condition naming a value as it was finds what it became, and a value the catalogue listed is shown in deprecatedValues for 90 days before it goes.

Narrowing

At least one condition must narrow the search: a positive operator on one of the catalogue's narrowingFields. The negative operators (not_equals, not_in, not_contains, is_not_set) never narrow, and level on its own never narrows. A search with no narrowing condition is refused with 400 NARROWING_REQUIRED, and the message lists the fields that would narrow it.

Reading a condition through a relation

via reads a condition on related entities instead of the entity itself: premises on an organisation reads the premises it operates, and operator on a premises reads the organisation that operates it. The catalogue's relations lists those the layers in scope offer. A positive operator is met when any related entity meets it; a negative one when at least one related entity has the field and none meets the positive form. A condition through a relation narrows when its field does.

A search with via must say, with a positive level condition, which entity type it is for: organisation for premises, premises for operator. Otherwise it is refused with 400 VIA_NOT_CONNECTED, which says what to add. Active companies running at least one CQC location rated Requires Improvement or Inadequate, in one search:

Request
{
  "layers": ["cqc", "companies_house"],
  "conditions": [
    { "field": "level", "operator": "equals", "value": "organisation" },
    { "field": "companies_house.status", "operator": "equals", "value": "Active" },
    {
      "field": "cqc.rating",
      "operator": "in",
      "value": ["Requires Improvement", "Inadequate"],
      "via": "premises"
    }
  ],
  "sort": "name",
  "limit": 50
}

Count first

POST /v1/search/count takes the same conditions, layers, max and exclude and returns how many the search would find and the most reading them could cost, without a row. cost.results is how many results the search would save, up to max; cost.perRow is what a row costs and cost.atMost the two multiplied. A count costs tokens, and charged says what it took. What each call costs is on the pricing page.

Response
{
  "data": { "total": 214, "capped": false, "cost": { "results": 214, "perRow": 1, "atMost": 214 } }
}

Pages and cursors

POST /v1/search returns the first page: up to limit rows, 50 by default and at most 100. Send the same body with the page's next as cursor for the next page, until next is null; with a cursor, the rest of the body is ignored. A cursor works only for the search, and the consumer, that made it; any other is refused with 400 SEARCH_CURSOR_INVALID. meta says where you are: limit, returned, total, counted up to 100,000 with capped past it, page and pages.

charged says what the page cost. Without enough tokens the call is refused with 402 TOKENS_EXHAUSTED and nothing is returned or saved.

Response, trimmed
{
  "data": ["..."],
  "next": "ss1.ss_Q7cWm2Rk9TzLp4Xv.100",
  "meta": { "limit": 50, "returned": 50, "total": 214, "capped": false, "page": 2, "pages": 5 },
  "charged": 50
}

What a row carries

A row is an entity as a search shows it: its entityId, level and name; its value for each result column in values; its primaryRecord, the register record to store if you keep a register id for the entity; and the layers in scope that hold data about it. A row gives a place, a local authority or an outward code, never a street address.

searchFacts says what the row matched on: each searched field with the entity's values that met the condition, at most 20. Through a relation the values are the related entities', and matched and of say how many of them met it. pending is true when something has arrived for the entity since the release the page comes from; the entity read says what.

A row
{
  "entityId": "prm_pdFPkhx7RuGEeFl8Q1pP",
  "level": "premises",
  "name": "BRACKENFOLD HOUSE",
  "values": {
    "kind": "premises",
    "place": "Kent",
    "operator": "BRACKENFOLD CARE LTD",
    "sources": "Care Quality Commission",
    "facts": "Overall rating: Requires Improvement; Beds: 32",
    "group": null,
    "status": "active",
    "cqc.rating": "Requires Improvement",
    "cqc.beds": 32
  },
  "primaryRecord": { "layer": "cqc", "source": "locations", "externalId": "1-2000000001" },
  "layers": ["cqc"],
  "searchFacts": [
    { "field": "cqc.rating", "label": "Overall rating", "values": ["Requires Improvement"] }
  ]
}

Saved searches

Every search that finds something is saved for 90 days with its first max results in the sort's order: 10,000 when you leave max out, at most 100,000. The response's search carries its id, label, conditions, how many results it saved and matched, and what its pages have cost. Give it a label when you run it, or later with PUT /v1/searches/{searchId}.

  • GET /v1/searches lists this consumer's saved searches, newest first.
  • GET /v1/searches/{searchId} returns the results again, a page at a time, as the entities are now: each row says whether it still meets the conditions (matches), and results no longer in the data come back in missing.
  • DELETE /v1/searches/{searchId} deletes one before its 90 days are up.

Each consumer sees only its own. One that has expired, or is another consumer's, returns 404 SAVED_SEARCH_NOT_FOUND.

Rows for entities you hold

POST /v1/search/rows returns a row for each of up to 100 entity ids you send, as the entities are now. Send the conditions the ids came from, or none, and each entry says whether its row still meets them; an id no longer held comes back in missing. A row is charged as a search's row is.

Rows
{ "entityIds": ["prm_pdFPkhx7RuGEeFl8Q1pP"], "conditions": [], "layers": ["cqc"] }

Leaving entities out

exclude on a search leaves out entities you have already seen: up to 10,000 entity ids and up to 20 of this consumer's saved searches, 100,000 entities in all. What it leaves out is never counted or charged. More than that is refused with 400 EXCLUDE_TOO_LARGE.

To find one entity by an identifier a user pastes in, such as a company number, a UPRN or a CQC id, use POST /v1/search/lookup instead. It returns one row without searchFacts, or null.

Exclude
{
  "layers": ["cqc"],
  "conditions": [{ "field": "cqc.rating", "operator": "equals", "value": "Inadequate" }],
  "exclude": { "entityIds": ["prm_pdFPkhx7RuGEeFl8Q1pP"], "searches": ["ss_Q7cWm2Rk9TzLp4Xv"] }
}

What a page carries

Every page of rows carries the duties of the licences its data is under; a count carries none. Show them wherever the rows are shown.

  • attributions: the line of each layer the rows hold data from. Empty on an empty page.
  • notices: what a licence asks be said with the rows, such as the CQC's non-endorsement.
  • currency: each layer with a currency duty that the rows hold data from, with the date its data is current to.

Enrichment-only layers are never in the catalogue, so never a condition or a column, and a search never lists addresses or searches by a person's name. The licences guide says what each duty asks and the errors guide lists every code a search can return.

On a page of rows
{
  "attributions": [
    {
      "layer": "cqc",
      "text": "Contains Care Quality Commission data. Not endorsed by the Care Quality Commission."
    }
  ],
  "notices": ["Not endorsed by the Care Quality Commission."],
  "currency": []
}