# Search and paging

> Find users, groups and any other object by their attributes, sorted the way you want, a page at a time.

Every list in the directory takes the same search: free text across a type's attributes, a filter built from
conditions on any of them, a sort on one or more, and a page size. Find every inactive user, everyone whose access
ends this month, or the groups whose name starts with `eng`, with one call.

## Before you begin

- **Privilege:** `RealmViewer` is enough; a search returns only what the caller may read. See
  [Who may do what](../../attributes/identity-model/#who-may-do-what).
- **Where:** the API, with your session in `$CVYUH_SESSION` (see [Call the API](../../../develop/api/)).

## How it works

A list is a `GET` on the type: `/objects/v1/user` for users, `/objects/v1/group` for groups, and the same for an
agent or a type of your own. It takes these query parameters:

| Parameter | What it does |
|---|---|
| `filter` | A JSON search document: `text`, a free-text match, and `filter`, a tree of conditions. Both are optional |
| `sort` | One or more attributes, comma-separated, each with `-` for descending: `-accountExpiry,lastName` |
| `page`, `pageSize` | Which page, from 1, and how many on it: 20 unless you say, at most 100 |
| `cursor` | The `nextCursor` the last page gave. Sending it always pages by cursor (below) |
| `totalPolicy` | `none` to skip counting the matches, which makes a large list faster |
| `expand` | Links to include in each result, such as `memberOf/*`; at most 10,000 per link attribute |

The answer is a page: `data`, the objects themselves, then `pageSize` and, as they apply, `page`, `totalCount`,
`totalPages`, `hasMore` and `nextCursor`.

**Pages or a cursor.** A realm pages by number unless it's set to page by cursor; either way, a request that sends
`cursor` is paged by cursor. Paged by number, the answer counts the matches (`totalCount`, `totalPages`). Paged by
cursor, it says only whether there's more (`hasMore`) and where to go on from (`nextCursor`).

**Free text** (`text`) matches any part of a type's text attributes, ignoring case: `"text": "chen"` finds Maya
Chen by her last name or her email. **Conditions match case exactly:** `=` and `like` treat `Eng%` and `eng%` as
different, so match the case you stored, or use free text.

**A condition** names an attribute, an operator and a value: `{ "field": "status", "op": "=", "value": "inactive" }`.
Combine conditions with `{ "type": "and", "rules": [ … ] }`, `"or"`, or `"not"` around a single one, nested as deep
as you need.

| Operators | For |
|---|---|
| `=`, `!=` | Any attribute |
| `in`, `not in` | Text, with a list of values: `["london", "paris"]` |
| `like`, `not like` | Text, with `%` as the wildcard |
| `>`, `>=`, `<`, `<=` | Numbers and times |
| `between` | Times, with `[from, to]`; either end may be `null` for open |
| `is null`, `is not null` | Whether an attribute is set |

An attribute that isn't set matches no condition but `is null`: a user without a `department` matches neither
`department = Finance` nor `department != Finance`.
| `contains` | A list of text holding the value |

## Set it up

`curl -G --data-urlencode` encodes the search document for the address.

Every inactive user, by last name:

```sh
curl -G https://foobar.cvyuh.com/api/v0/am/objects/v1/user \
  -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
  --data-urlencode 'sort=lastName' \
  --data-urlencode 'filter={"filter":{"field":"status","op":"=","value":"inactive"}}'
```

Every user whose access ends before a date, soonest first:

```sh
curl -G https://foobar.cvyuh.com/api/v0/am/objects/v1/user \
  -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
  --data-urlencode 'sort=accountExpiry' \
  --data-urlencode 'filter={"filter":{"type":"and","rules":[
      {"field":"accountExpiry","op":"is not null"},
      {"field":"accountExpiry","op":"<","value":"<RFC 3339 time>"}]}}'
```

Every group whose name starts with `eng`:

```sh
curl -G https://foobar.cvyuh.com/api/v0/am/objects/v1/group \
  -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
  --data-urlencode 'filter={"filter":{"field":"displayName","op":"like","value":"Eng%"}}'
```

To keep a long search out of the address, send the same parameters as a JSON body with the `QUERY` method in place of
`GET`:

```sh
curl -X QUERY https://foobar.cvyuh.com/api/v0/am/objects/v1/user \
  -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
  -H "Content-Type: application/json" \
  -d '{ "sort": "lastName", "pageSize": 50,
        "filter": { "text": "chen" } }'
```

## Limits and defaults

| What | Default or limit |
|---|---|
| `pageSize` | 20; at most 100, and a larger value is cut to 100 |
| `page` | From 1; a lower value reads as 1 |
| Paging | By number, unless the realm is set to page by cursor or the request sends `cursor` |
| Links per `expand` | At most 10,000 per link attribute; past that the read is refused `413` |
| Nested `expand` | 4 levels |
| Case | Free text ignores case; `=`, `in` and `like` don't |

## When a search fails

| Answer | Code | What it means | What to do |
|---|---|---|---|
| `400` | `INVALID_REQUEST` | An unknown field, an operator that doesn't fit the field, a link used as a filter field, or a malformed search document | Check the field against the type, and the operator against the table above |
| `403` | `FORBIDDEN` | The caller may not read the type | Ask for `RealmViewer` |
| `413` | `PAYLOAD_TOO_LARGE` | An `expand` would return more than 10,000 links | Leave out `expand`, and page the links on `/objects/v1/{type}/{id}/relationships/{link}` |

## Troubleshooting

| Symptom | Cause | Fix |
|---|---|---|
| A search for `eng%` finds nothing | `like` and `=` match case exactly | Write the case you stored (`Eng%`), or use `text` |
| A `!=` condition leaves out users you expected | Users without the attribute match no condition but `is null` | Add `or` with `is null` on the same field |
| `400` "unknown filter field" for an attribute you write | The attribute isn't on the type, or it's a link | Add the attribute to the type; for links, list them on the object |
| `totalCount` is missing | The realm pages by cursor, you sent `cursor`, or you set `totalPolicy=none` | Page by number to count |

## Good to know

- **Secret values can't be searched.** Passwords, sealed attributes and values only a root administrator may read
  are left out of filters, sort and free text, so a search can't be used to guess them.
- **Links aren't filter fields.** To find a group's members, list them on the group:
  `/objects/v1/group/engineering/relationships/member`, which takes the same parameters.
- **An unknown field, or an operator that doesn't fit it, is refused.** The realm answers `400` rather than ignoring
  part of your search.
- **An attribute you add is searchable at once.** Each new attribute joins the filter fields as soon as the type is
  saved. See [Attributes and validation](../../attributes/attributes-and-validation/).

## Related

- [Add and change users](../add-and-change-users/) and [Add groups and members](../../groups/add-groups-and-members/):
  what you'll search most.
- [Offboard a leaver](../../guides/offboard-a-leaver/): finds a user's sessions with the same filters.
