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

# clusterize

> Cluster rows by vector similarity.

`clusterize` assigns each row of a vector column to a cluster of similar rows and returns a column of `BIGINT` cluster IDs. `clus` is an equivalent alias.

Two rows are linked when their cosine similarity is at least `min_similarity`. Rows that are linked, directly or through a chain of other rows, share a cluster.

## Signature

```chowder-sql theme={"theme":"css-variables","languages":{"custom":["/languages/chowder-sql.json"]}}
clusterize(embedding [, min_similarity [, k]])
clus(embedding [, min_similarity [, k]])
```

| Parameter | Type | Default | Description |
| - | - | - | - |
| `embedding` | `FLOAT[N]` | — | Vector to compare. |
| `min_similarity` | Constant numeric | `0.9` | Minimum cosine similarity for two rows to be linked. |
| `k` | Constant non-negative integer | `16` | Number of nearest rows checked for links per row. |

## Example

```chowder-sql theme={"theme":"css-variables","languages":{"custom":["/languages/chowder-sql.json"]}}
WITH labeled AS (
    SELECT
        title,
        clusterize(summary_embedding.output, 0.95, 100) AS cluster -- Similarity threshold 0.95; 100 candidate neighbors
    FROM read_parquet('hf://buckets/chowderai/chowder-demo/parquet/data/cmu-movie-summaries-embeddings/*.parquet')
)
SELECT
    cluster,
    COUNT(*) AS movie_count, -- Count movies, including remakes with the same title
    array_agg(DISTINCT title) AS titles
FROM labeled
GROUP BY cluster
HAVING COUNT(*) > 1
ORDER BY movie_count DESC, cluster
LIMIT 5;
```

`HAVING` removes single-row clusters, and `LIMIT` bounds the summary, not the rows being clustered.

## Return type

`BIGINT`, numbered from `0` with no gaps. IDs label clusters within one run and can change between runs.

## Behavior

* `clusterize` finds each row's candidate neighbors with approximate nearest-neighbor search over an HNSW index, the standard approach for fast vector search over large relations. Results can occasionally differ from an exact search.
* The HNSW index is cached in memory. Repeated queries over the same in-memory table in one engine reuse it, even with a different `k`. See [Caching](https://docs.chowder.ai/concepts/caching.md#reuse-nearest-neighbor-indexes).
* A row with no links forms its own cluster.
* Null or zero-magnitude embeddings each form their own cluster.
* Clusters grow through chains of links, so lowering `min_similarity` can merge clusters through rows in between.
* Clusters come from the rows in the query's input. A `WHERE` filter or a `LIMIT` in a subquery changes which rows are clustered. A `LIMIT` in the same `SELECT` as `clusterize` doesn't.

## Related operations

* Use [`neighbors`](https://docs.chowder.ai/reference/column-functions/neighbors) to list each row's nearest rows with their similarity.
* Use [`sim`](https://docs.chowder.ai/reference/column-functions/sim) to compare two vectors directly.


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