﻿# cuML nearest-neighbor results: preserve fitted row positions separately from document IDs

Map cuML nearest-neighbor indices back through the exact fitted population order instead of treating numerical positions as dataframe labels or durable document identities.

Author: Retrivium editorial team
Updated: 2026-11-04
Canonical URL: https://retrivium.ai/journal/fg2-cuml-neighbor-indices-population-lineage/
Language: en

## Define the fitted population before interpreting results

A retrieval experiment fits a GPU neighbor model on embedding rows, then joins returned indices against a document table that has been sorted independently. The [cuML NearestNeighbors reference](https://docs.nvidia.com/cuml/26.10/api/generated/cuml.neighbors.NearestNeighbors/) describes fitting and querying a numerical population. Preserve the precise embedding-row order together with document identities at fit time. A valid numerical neighbor can otherwise be attached to a different document after an unrelated table sort, filter or deduplication step.

## Distinguish positions from dataframe labels

The [scikit-learn neighbor reference](https://scikit-learn.org/stable/modules/generated/sklearn.neighbors.NearestNeighbors.html) identifies neighbor indices as positions in the population matrix. This is an important interface distinction for a compatible dataframe workflow. A source index label might be a document identifier, a repeated label or an arbitrary offset from an earlier transformation. Do not assume it is the same namespace as the fitted matrix's row positions merely because both are represented as integers in an exported result.

## Preserve query identity independently of neighbors

Each output row also belongs to a particular query. Keep query IDs through batching and output conversion so a row of distances is associated with the intended query as well as the correct population mapping. Validate the distance and neighbor arrays together. A correct population lookup still produces an incorrect retrieval response if its list of neighbors is attached to another query after asynchronous preparation, reshaping or reordering of query batches.

## Use positional lookup only against the matching revision

The [cuDF dataframe reference](https://docs.nvidia.com/cudf/latest/cudf/api_docs/dataframe/) distinguishes positional and label-based selection. Apply the returned positions to the saved population mapping rather than a mutable current document table. Record the embedding revision, population identity and metric alongside the index. Rebuilding the model against a changed population requires a corresponding new mapping, even if the array shape and number of documents happen to remain unchanged.

## Test with deliberately unrelated document IDs

Use a small population whose document identifiers do not resemble row positions, then reorder the metadata table and verify unchanged semantic results through the explicit mapping. The guides on [dataset object identities](/journal/data-guide-coco-instance-caption-identities/) and [logical table schemas](/journal/data-guide-frictionless-table-schema/) provide related context. Retrieval validation should check returned document meaning as well as numerical distances. A high-quality neighbor search cannot compensate for an incorrect identity join at the boundary between a numerical model and its source documents.
