> ## 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.

# neighbors

> Find the nearest neighbors of each row by vector similarity.

`neighbors` finds the `k` most similar other rows for each row of a vector column and returns a column of lists, nearest first.

## Signature

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

| Parameter | Type | Description |
| - | - | - |
| `embedding` | `FLOAT[N]` | Vector used to compute cosine similarity. |
| `k` | Constant non-negative integer | Maximum number of other rows to return. |
| `projection` | `*`, column, or `(columns)` | Columns carried into each nested neighbor value. |

The projection is required so a query does not accidentally copy large unused columns.

## Example

```chowder-sql theme={"theme":"css-variables","languages":{"custom":["/languages/chowder-sql.json"]}}
SELECT
    movie_id,
    title,
    neighbors(
        summary_embedding.output, -- Reuse the saved vector embedding
        3, -- Three neighbors per movie
        (title, release_year) -- Fields to include with each neighbor
    ) AS similar_movies
FROM read_parquet('hf://buckets/chowderai/chowder-demo/parquet/data/cmu-movie-summaries-embeddings/*.parquet');
```

## Return type

An `ARRAY<STRUCT>` with one struct per neighbor. Each struct contains `similarity` followed by the projected columns. For the example above:

```text theme={"theme":"css-variables","languages":{"custom":["/languages/chowder-sql.json"]}}
ARRAY<STRUCT<
    similarity FLOAT,
    title VARCHAR,
    release_year INT
>>
```

## Behavior

* `neighbors` uses 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).
* The current row is excluded by row identity, not by comparing similarity to `1`.
* Fewer than `k` available rows produces a shorter list.
* Null or zero-magnitude embeddings produce an empty list.
* Neighbors come from the rows in the query's input. A `WHERE` filter or a `LIMIT` in a subquery changes which rows are compared. A `LIMIT` in the same `SELECT` as `neighbors` doesn't.

## Flatten neighbors

Compute neighbors in a CTE, then use `unnest` to return one row per neighbor. Filter after the CTE so neighbors still come from the whole input:

```chowder-sql theme={"theme":"css-variables","languages":{"custom":["/languages/chowder-sql.json"]}}
WITH movies AS (
    SELECT
        movie_id,
        title,
        neighbors(
            summary_embedding.output,
            3,
            (title, release_year)
        ) AS similar_movies
    FROM read_parquet('hf://buckets/chowderai/chowder-demo/parquet/data/cmu-movie-summaries-embeddings/*.parquet')
)
SELECT
    title,
    unnest(similar_movies) AS neighbor
FROM movies
WHERE movie_id = 53085; -- Toy Story
```

## Related operations

* Use a [nearest-neighbor join](https://docs.chowder.ai/guides/nearest-neighbor-joins.md) to find the closest matches between two relations, such as a query set and a corpus.
* 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.