# Search

A search is a `POST` whose body holds a `query`: a tree of tests joined by `and`, `or` and `not`. A tree can say what a list of filters cannot.

Open election markets that are still undecided and end before 2027, leaving out the crypto category:

```json
{
  "query": {
    "op": "and",
    "queries": [
      { "op": "equals", "field": "status", "value": ["active", "paused"] },
      {
        "op": "num_between",
        "field": "primaryMidpointMicrodollars",
        "min": 50000,
        "max": 950000
      },
      { "op": "date_lt", "field": "endAt", "value": "2027-01-01T00:00:00Z" },
      {
        "op": "or",
        "queries": [
          { "op": "contains", "field": "question", "value": "election" },
          { "op": "contains", "field": "eventTitle", "value": "election" }
        ]
      },
      {
        "op": "not",
        "query": { "op": "includes", "field": "categories", "value": "crypto" }
      }
    ]
  },
  "ordering": [{ "attribute": "endAt", "direction": "asc" }]
}
```

Send it to [`POST /markets/search`](https://predictparity.com/docs/api/rest#post-markets-search), the first search on the API. That endpoint lists the fields and ordering a market search takes; this page describes what every search shares.

## The query tree

```ts
type SearchRequest = {
  query: QueryNode;
  ordering?: { attribute: string; direction: "asc" | "desc" }[];
  cursor?: string;
};

type QueryNode =
  | { op: "and" | "or"; queries: QueryNode[] }
  | { op: "not"; query: QueryNode }
  | { op: "contains" | "starts_with"; field: string; value: string }
  | { op: "equals"; field: string; value: string | string[] }
  | { op: "includes"; field: string; value: string | string[] }
  | { op: "num_gt" | "num_gte" | "num_lt" | "num_lte"; field: string; value: number }
  | { op: "num_between"; field: string; min: number; max: number }
  | { op: "date_gt" | "date_gte" | "date_lt" | "date_lte"; field: string; value: string }
  | { op: "date_between"; field: string; min: string; max: string };
```

| Operation | A row matches when |
| --- | --- |
| `and` | It matches every one of `queries`. |
| `or` | It matches at least one of `queries`. |
| `not` | It does not match `query`. |
| `contains` | Every word of `value` is in the field, in any order. A word of 3 to 15 characters can be part of a longer word. A shorter or longer word must be a whole word. |
| `starts_with` | The field begins with `value`. |
| `equals` | The whole field is `value`, or any one of a list. |
| `includes` | The field is a list, and it holds `value`, or any one of a list. |
| `num_gt`, `date_gt` | The field is above `value`. |
| `num_gte`, `date_gte` | The field is at or above `value`. |
| `num_lt`, `date_lt` | The field is below `value`. |
| `num_lte`, `date_lte` | The field is at or below `value`. |
| `num_between`, `date_between` | The field is from `min` to `max`, both included. |

- `query` is the whole test. Nothing is added to it.
- A leaf tests one field, named as the response names it.
- Letter case never matters in text. A text value is at most 256 characters, and a longer field matches neither `equals` nor `starts_with`.
- A field that takes one of a fixed set of values, such as a status or a category, is matched exactly, as the endpoint lists the values: `crypto`, never `Crypto`. Any other value is `validation_failed`.
- A date is an ISO 8601 instant, with `Z` or an offset: `2027-01-01T00:00:00Z` or `2026-12-31T19:00:00-05:00`.
- A field that is `null` matches no range.
- A field that holds a list is tested with `includes`, never `equals`. An empty or `null` list holds nothing, so it matches no `includes`, and it matches `not` over one.
- A tree nests at most 8 deep and holds at most 50 terms: one for each word of a `contains`, each value of an `equals` or an `includes`, and every other leaf.
- Anything not recognised is `validation_failed`, naming the node by its path, such as `query.queries.1.field`. That covers an operation, a field, a key of a node, and a test on a field of the wrong kind.

Each search endpoint lists the fields it takes, and the operations each field takes.

## Ordering

`ordering` lists `{ attribute, direction }` entries, applied in turn. An attribute can be listed once, and a `null` attribute comes last in either direction. Each search endpoint lists its attributes, and the order it uses without `ordering`.

## Cursor

A search answers a page, as every [list](https://predictparity.com/docs/api/rest#lists) does. To read the next page, send the same body with `nextCursor` as its `cursor`. A cursor sent with a different `query` or `ordering` is `validation_failed`.

Source: https://predictparity.com/docs/api/search
