> ## Documentation Index
> Fetch the complete documentation index at: https://docs.mixpeek.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Taxonomies

> Label documents by matching them against a reference collection, at ingest, at query time or as a backfill, and run any retriever as an ingest step.

A taxonomy labels documents by matching them against a reference collection: a product catalog, a set of enrolled faces, a list of categories. It works like a SQL join on embedding similarity instead of key equality. For each document, a retriever you choose searches the reference collection, and the fields of the best match (a SKU, a name, a category) are copied onto the document. You can run a taxonomy as documents are processed, inside a retriever at query time, or over documents that already exist.

## Create a taxonomy

This flat taxonomy matches each document against `$REFERENCE_COLLECTION_ID` with the retriever `$RETRIEVER_ID` and copies `name` and `department` from the best match.

```bash cURL theme={null}
curl -X POST "https://api.mixpeek.com/v1/taxonomies" \
  -H "Authorization: Bearer $MIXPEEK_API_KEY" \
  -H "X-Namespace: $NAMESPACE_ID" \
  -H "Content-Type: application/json" \
  -d '{
    "taxonomy_name": "employee_faces",
    "config": {
      "taxonomy_type": "flat",
      "retriever_id": "'"$RETRIEVER_ID"'",
      "input_mappings": [
        { "input_key": "query", "source_type": "payload", "path": "text" }
      ],
      "source_collection": {
        "collection_id": "'"$REFERENCE_COLLECTION_ID"'",
        "enrichment_fields": [
          { "field_path": "name", "merge_mode": "replace" },
          { "field_path": "department", "merge_mode": "replace" }
        ]
      }
    }
  }'
```

```python Python theme={null}
import os
import requests

resp = requests.post(
    "https://api.mixpeek.com/v1/taxonomies",
    headers={
        "Authorization": f"Bearer {os.environ['MIXPEEK_API_KEY']}",
        "X-Namespace": os.environ["NAMESPACE_ID"],
    },
    json={
        "taxonomy_name": "employee_faces",
        "config": {
            "taxonomy_type": "flat",
            "retriever_id": os.environ["RETRIEVER_ID"],
            "input_mappings": [
                {"input_key": "query", "source_type": "payload", "path": "text"}
            ],
            "source_collection": {
                "collection_id": os.environ["REFERENCE_COLLECTION_ID"],
                "enrichment_fields": [
                    {"field_path": "name", "merge_mode": "replace"},
                    {"field_path": "department", "merge_mode": "replace"},
                ],
            },
        },
    },
)
print(resp.json()["taxonomy_id"])
```

```javascript JavaScript theme={null}
const resp = await fetch("https://api.mixpeek.com/v1/taxonomies", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.MIXPEEK_API_KEY}`,
    "X-Namespace": process.env.NAMESPACE_ID,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    taxonomy_name: "employee_faces",
    config: {
      taxonomy_type: "flat",
      retriever_id: process.env.RETRIEVER_ID,
      input_mappings: [{ input_key: "query", source_type: "payload", path: "text" }],
      source_collection: {
        collection_id: process.env.REFERENCE_COLLECTION_ID,
        enrichment_fields: [
          { field_path: "name", merge_mode: "replace" },
          { field_path: "department", merge_mode: "replace" },
        ],
      },
    },
  }),
});
console.log((await resp.json()).taxonomy_id);
```

Only `taxonomy_name` and `config` sit at the top level. A flat taxonomy requires `retriever_id`, `input_mappings` and `source_collection` inside `config`.

* **`input_mappings`** builds the retriever's inputs from each document. Each entry is `{ input_key, source_type, path }`. `input_key` must be a key in the retriever's own `input_schema`. `source_type` is `payload` (a document field at `path`), `vector` (a stored vector at `path`), `blob` (the document's source blob URL) or `literal` (a static value in `override`). It never takes a feature URI.
* **`enrichment_fields`** lists the fields to copy from the matched reference document. Every `field_path` must exist in the reference collection's output schema, or the create returns `422` with the fields that do. `merge_mode` is `replace` or `append`. Omit the list to copy the whole payload.

Creating the taxonomy labels nothing yet. Pick one of the three ways to run it below.

## Run a taxonomy

| Mode | When it runs | Set it up with | Use it for |
| - | - | - | - |
| Materialized | After extraction, on every new document | `taxonomy_applications` on the collection | Production search, filters, analytics |
| On demand | At query time | A `taxonomy_enrich` stage in a retriever | Testing, reference data that changes often |
| Retroactive | Once, when you call it | `POST /v1/collections/{collection_id}/apply-taxonomy` | Backfills, reclassifying after a taxonomy change |

### Materialized at ingest

Attach the taxonomy to a collection. `execution_mode` accepts only `materialize`.

```json Collection body excerpt theme={null}
{
  "taxonomy_applications": [
    { "taxonomy_id": "tax_employee_faces", "execution_mode": "materialize" }
  ]
}
```

Labels land on documents about 30 seconds after a batch finishes; the step is debounced so a burst of batches runs it once. Each entry also takes `scroll_filters` (label only matching documents), `target_collection_id`, `execution_phase`, `priority` and, for hierarchical taxonomies, `hierarchical_enrichment_style` (`full_chain`, `best_match` or `combined`).

### On demand in a retriever

Add a `taxonomy_enrich` stage after a stage that returns documents, such as `feature_search`. It labels the documents flowing through the retriever and stores nothing. Parameters and output fields are on [Retriever stages](https://docs.mixpeek.com/docs/retrieve/stages.md#taxonomy-enrich).

```json taxonomy_enrich stage theme={null}
{
  "stage_name": "label_roles",
  "stage_type": "apply",
  "config": {
    "stage_id": "taxonomy_enrich",
    "parameters": { "taxonomy_id": "tax_org_roles", "min_score": 0.4 }
  }
}
```

### Retroactively on existing documents

```bash cURL theme={null}
curl -X POST "https://api.mixpeek.com/v1/collections/$COLLECTION_ID/apply-taxonomy" \
  -H "Authorization: Bearer $MIXPEEK_API_KEY" \
  -H "X-Namespace: $NAMESPACE_ID" \
  -H "Content-Type: application/json" \
  -d '{
    "taxonomy_id": "'"$TAXONOMY_ID"'",
    "scroll_filters": {
      "field": "metadata.needs_reclassification",
      "operator": "eq",
      "value": true
    }
  }'
```

The call returns a `task_id` and `estimated_documents`; follow it with `GET /v1/tasks/{task_id}`. Use it after you change a taxonomy or add reference data, or when you move from a flat to a hierarchical taxonomy. `scroll_filters` limits it to matching documents; `reapply_all` relabels documents that already have a label. See [Apply taxonomy](https://docs.mixpeek.com/docs/api-reference/collection-taxonomies/apply-taxonomy-to-existing-documents.md).

## Hierarchical taxonomies

A hierarchical taxonomy labels in levels, such as brand, then category, then product line. Each node is a reference collection. A document that matches a node is passed down and matched against that node's children, and it collects enrichment fields from every level it matches. A document labeled `Nike > Athletic > Running` carries fields from all three nodes.

```bash cURL theme={null}
curl -X POST "https://api.mixpeek.com/v1/taxonomies" \
  -H "Authorization: Bearer $MIXPEEK_API_KEY" \
  -H "X-Namespace: $NAMESPACE_ID" \
  -H "Content-Type: application/json" \
  -d '{
    "taxonomy_name": "org_roles",
    "config": {
      "taxonomy_type": "hierarchical",
      "retriever_id": "'"$RETRIEVER_ID"'",
      "input_mappings": [
        { "input_key": "query", "source_type": "payload", "path": "text" }
      ],
      "hierarchical_nodes": [
        { "collection_id": "col_employees", "parent_collection_id": null },
        { "collection_id": "col_executives", "parent_collection_id": "col_employees" }
      ]
    }
  }'
```

* The hierarchy is a flat list. Each node names its parent in `parent_collection_id`; `null` marks a root. Order does not matter.
* `collection_id` is the only required field on a node and is its identifier.
* `retriever_id` and `input_mappings` are optional at the `config` level and can be set per node. A child inherits its parent's enrichment fields and can override or extend them.
* Instead of listing nodes, set `inference_strategy` to `schema`, `cluster` or `llm` with `inference_collections`, and Mixpeek builds the nodes. A common path is to infer a first tree, then edit it.

`GET /v1/taxonomies/{taxonomy_id}` returns the taxonomy with its `nodes`; add `?version=` to read an earlier version.

### Build a taxonomy from clusters

`POST /v1/taxonomies/from-documents` turns a [cluster](https://docs.mixpeek.com/docs/enrich/clusters.md) run, or a set of documents you selected, into a new flat taxonomy in one call. It creates a node collection that reuses the existing vectors (no re-embedding), a search retriever over it, the taxonomy and its nodes. Send `cluster_run` (`cluster_id` and `run_id`) for one node per cluster, or `documents` plus `node_label` for one node at the documents' centroid. See [Create a taxonomy from a document selection](https://docs.mixpeek.com/docs/api-reference/taxonomies/create-taxonomy-from-document-selection.md).

## Labels the data does not confirm

A flat taxonomy gives each document its closest node, even a poor fit: a taxonomy whose only node is `ml` would label a recipe `ml`. So each run compares the match scores of all the documents it labels. When the scores split into a high and a low group with a gap of at least 0.2, the low group gets no label, and a label an earlier run gave them is removed. With no gap, or fewer than three documents, every match is kept.

The outcome is recorded under `_internal`, where `<id>` is the taxonomy ID without `tax_`:

| Field | Value |
| - | - |
| `_taxonomy_<id>_confirmed` | `true` when the label was kept, `false` when withheld |
| `_taxonomy_<id>_floor` | The run's cutoff score, or `null` when there was no gap |
| `_taxonomy_<id>_score` | The document's best match score |
| `_taxonomy_<id>_unconfirmed_label` | On a withheld document, the label it would have had |

Filter on `_internal._taxonomy_<id>_confirmed` equal to `false` to review the withheld documents.

## Run any retriever at ingest

A taxonomy copies fields from a matched document. When you need something else written back, such as an LLM classification or a lookup in another collection, attach a **retriever enrichment** to the collection. After each batch, Mixpeek runs the retriever once per document and writes the fields you pick from its results onto the document.

```bash cURL theme={null}
curl -X PATCH "https://api.mixpeek.com/v1/collections/$COLLECTION_ID" \
  -H "Authorization: Bearer $MIXPEEK_API_KEY" \
  -H "X-Namespace: $NAMESPACE_ID" \
  -H "Content-Type: application/json" \
  -d '{
    "retriever_enrichments": [
      {
        "retriever_id": "'"$RETRIEVER_ID"'",
        "input_mappings": [
          { "input_key": "query", "source": { "source_type": "document_field", "path": "content" } }
        ],
        "write_back_fields": [
          { "source_field": "category", "target_field": "_llm_category", "mode": "first" },
          { "source_field": "sentiment", "target_field": "_llm_sentiment", "mode": "first" }
        ],
        "enabled": true
      }
    ]
  }'
```

| Field | What it sets | Default |
| - | - | - |
| `input_mappings` | Retriever inputs, each `{ input_key, source: { source_type, path, value } }` | Required |
| `write_back_fields` | Result fields to copy: `source_field`, `target_field`, `mode` | Required |
| `mode` | `first` (first result), `all_as_array` (list from every result), `concat` (strings joined with `", "`) | `first` |
| `execution_phase` | Post-processing phase, 1 to 4 | `4` |
| `priority` | Order within a phase; higher runs first | `0` |
| `scroll_filters` | Enrich only matching documents | All documents |
| `enabled` | Turn the enrichment on or off | `true` |

> [!WARNING]
>
> A retriever enrichment's `input_mappings` uses a different shape from a taxonomy's. The enrichment form nests the source: `{ input_key, source: { source_type, path, value } }`, with `source_type` `document_field`, `constant` or `source_blob`. The taxonomy form is flat: `{ input_key, source_type, path, override }`, with `source_type` `payload`, `literal`, `vector` or `blob`. The two vocabularies share no values, so a wrong `source_type` is rejected; a flat entry sent to an enrichment fails its `source` requirement.

Retriever enrichments run one document at a time within a collection, so their time grows linearly with document count.

### Post-processing order

After extraction, enrichment steps run in phase order, then by `priority` (higher first) within a phase:

| Phase | Step | Runs |
| - | - | - |
| 1 | Taxonomies | Parallel per document |
| 2 | [Clusters](https://docs.mixpeek.com/docs/enrich/clusters.md) | Batch |
| 3 | [Alerts](https://docs.mixpeek.com/docs/enrich/alerts-and-triggers.md) | Parallel per document |
| 4 | Retriever enrichments (default phase) | Sequential per document |

## Monitor and maintain

* List taxonomies with `POST /v1/taxonomies/list`.
* Follow materialized enrichment with the `collection.documents.written` [webhook event](https://docs.mixpeek.com/docs/enrich/alerts-and-triggers.md).
* Check that a `taxonomy_enrich` stage does not dominate retriever latency in [retriever analytics](https://docs.mixpeek.com/docs/production/monitoring.md).
* Save config changes as numbered versions with `POST /v1/taxonomies/{taxonomy_id}/versions`; every earlier version stays readable.
* Copy only the fields you need at query time into `enrichment_fields`; every field is written onto every labeled document.

## Related

* [Clusters](https://docs.mixpeek.com/docs/enrich/clusters.md): discover categories before you define a taxonomy
* [Retriever stages](https://docs.mixpeek.com/docs/retrieve/stages.md#taxonomy-enrich): the `taxonomy_enrich` stage
* [Collections](https://docs.mixpeek.com/docs/extract/collections.md): where `taxonomy_applications` and `retriever_enrichments` live
* [Create taxonomy](https://docs.mixpeek.com/docs/api-reference/taxonomies/create-taxonomy.md) in the API reference


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.