mirror of
https://github.com/lancedb/lancedb.git
synced 2026-11-13 14:16:54 +00:00
## Summary Fixes the problems a page-by-page review of the docs found before launch, after checking each one against the current source and, where noted, the code. The matching fixes for the dataset and Geneva pages, which live in `lancedb/docs`, are in lancedb/docs#402. **Tables** - `integrations/embedding/cohere`: each input-type description was split across two table rows and wrapped in literal quotes (`"`search_document`"`). Now one row per type. - `integrations/embedding/gemini`: removes the same stray quotes from the task-type table. - `integrations/ai/langchain`: the MMR search table's `kwargs` row was missing a cell, so its description sat under Type. - Six reranker pages (answerdotai, cohere, colbert, jina, openai, voyageai): closes the backtick before `_relevance_score`. - `indexing/vector-index`: finishes a cell cut off at "with scalar". - `sql/dql/dedupe`: labels the empty header cell in the resolution table. **Corrections** - `curation/sampling`: sampling works in LanceDB Embedded through the Python SDK, so the page is available in both, with the SQL sections marked Enterprise. - `feature-engineering/index`, `add-computed-column`: automatic refresh is on by default for each computed column and takes effect once a deployment enables its scheduler. `refresh-column` already said so; these two implied it was off. - `indexing/scalar-index`, `indexing/index`: `LABEL_LIST` accelerates `array_has_all`/`array_has_any`; `array_contains_*` doesn't exist. Also merges scalar-index's two "nested fields" sections into one. - `integrations/embedding/ibm`: lists the four current models from `CURRENT_MODELS` (it listed a deprecated one) and explains that the default stays a legacy ID so existing tables reload with their model. - `integrations/reranking/openai`: the class is `OpenaiReranker`, not `OpenAI()`. - `integrations/ai/synthetic-data-kit`: the CLI is `synthetic-data-kit` throughout, each step runs one command, each step passes the file it writes to the next, and `save-as` uses `-f`/`-o`. - `integrations/data/dlt`: `secrets.toml` defines `api_token`, which the pipeline reads, and the run command runs the file the page creates. - `search/vector-search`: the multivector walkthrough connected to Enterprise in step 1 and a local path in step 2. It now runs locally throughout, with a note on connecting to Enterprise, and is renamed so it doesn't read as a duplicate of the short section above. **Structure and wording** - `integrations/embedding/nvidia`: adds headings, and drops test-suite details aimed at contributors. - `integrations/ai/genkit`: makes the retrieval test a real heading and describes the query step. - `integrations/data/voxel51`: replaces a raw MkDocs `!!! note` with a `<Note>`. - `sql/ddl/table`, `sql/ddl/job`: "Errors and limits" is a real heading, not bold text. - `curation/windowing`: sidebar title matches the curation overview ("Rank and Deduplicate"). - `deployment/storage`, `basics/agentic-skills`: fixes a garbled sentence and a stale reference to the quickstart. **Checked:** no new Vale alerts on the changed pages. Assembled together with lancedb/docs#402, the site passes `mint validate` and `mint broken-links --check-anchors --check-redirects`. Every changed page was reviewed in a local preview. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
161 lines
5.7 KiB
Plaintext
161 lines
5.7 KiB
Plaintext
---
|
|
title: "Ranking and deduplication"
|
|
sidebarTitle: "Rank and Deduplicate"
|
|
description: "Choose records with explicit ranking rules and deterministic tie handling."
|
|
icon: chart-simple
|
|
keywords: ["window", "deduplication", "row_number", "rank", "train test split", "per class"]
|
|
availability:
|
|
embedded: unavailable
|
|
enterprise: available
|
|
---
|
|
|
|
Window functions calculate values across related rows while keeping each row
|
|
in the result. Use them to rank documents, keep one record per item, or compare
|
|
a record with the one before it.
|
|
|
|
## Example data {#example-data}
|
|
|
|
Connect with an [Enterprise SQL client](/basics/sql). The examples use a small
|
|
document table and a separate label table. Expand the setup below to create them.
|
|
|
|
<Accordion title="Create the example tables">
|
|
|
|
`curation_documents` has six records describing four documents.
|
|
`curation_labels` has one label for each of three documents. `row_id` identifies
|
|
a record; `doc_id` identifies the document it describes.
|
|
|
|
Run each statement separately in a scratch database or namespace. Create these
|
|
tables once for the whole section, using fresh names if they already exist.
|
|
|
|
```sql
|
|
CREATE TABLE curation_documents (
|
|
row_id BIGINT, doc_id BIGINT, version BIGINT,
|
|
source VARCHAR, caption VARCHAR, score DOUBLE
|
|
);
|
|
INSERT INTO curation_documents VALUES
|
|
(1, 10, 1, 'web', NULL, 0.80),
|
|
(2, 10, 2, 'web', 'A cat on a chair', 0.95),
|
|
(3, 20, 1, 'books', 'A dog in a field', 0.90),
|
|
(4, 30, 1, 'web', NULL, 0.70),
|
|
(5, 30, 1, 'web', 'A cat by a window', 0.85),
|
|
(6, 40, 1, 'books', 'A bird in a tree', 0.60);
|
|
|
|
CREATE TABLE curation_labels (doc_id BIGINT, label VARCHAR);
|
|
INSERT INTO curation_labels VALUES
|
|
(10, 'cat'), (20, 'dog'), (30, 'cat');
|
|
```
|
|
|
|
`score` is a sample quality score, not a search result score. Document 10 has
|
|
two versions. Document 30 has two records at the same version. Document 40
|
|
has no label. These cases let you check the effect of each operation.
|
|
|
|
</Accordion>
|
|
|
|
## Keep one record per document {#dedup}
|
|
|
|
`row_number()` numbers rows within each group. `PARTITION BY` defines the group,
|
|
and `ORDER BY` determines which record comes first. This query selects the
|
|
latest version of each document, using `row_id` to break ties:
|
|
|
|
```sql
|
|
WITH ranked AS (
|
|
SELECT *, row_number() OVER (
|
|
PARTITION BY doc_id ORDER BY version DESC, row_id DESC
|
|
) AS rn
|
|
FROM curation_documents
|
|
)
|
|
SELECT row_id, doc_id, version, caption
|
|
FROM ranked
|
|
WHERE rn = 1
|
|
ORDER BY doc_id;
|
|
```
|
|
|
|
This returns row IDs **2, 3, 5 and 6**. For document 30, rows 4 and 5 have the
|
|
same version; the example deliberately chooses the larger `row_id`.
|
|
Without a complete ordering, the selected row among ties is not determined by
|
|
the query.
|
|
|
|
Use `rank()` instead of `row_number()` when tied records should all survive,
|
|
and order only by the fields that define a tie. Adding a unique tie-breaker
|
|
would make every rank distinct. The outer query filters the calculated rank
|
|
because window functions are evaluated after the inner query's `WHERE` clause.
|
|
|
|
## Select the highest-scoring records per class {#per-group}
|
|
|
|
Deduplicate first, then rank the labeled documents within each class:
|
|
|
|
```sql
|
|
WITH latest AS (
|
|
SELECT *, row_number() OVER (
|
|
PARTITION BY doc_id ORDER BY version DESC, row_id DESC
|
|
) AS version_rank
|
|
FROM curation_documents
|
|
), ranked AS (
|
|
SELECT d.doc_id, l.label, d.score,
|
|
row_number() OVER (
|
|
PARTITION BY l.label ORDER BY d.score DESC, d.doc_id
|
|
) AS class_rank
|
|
FROM latest AS d
|
|
JOIN curation_labels AS l ON l.doc_id = d.doc_id
|
|
WHERE d.version_rank = 1
|
|
)
|
|
SELECT doc_id, label, score
|
|
FROM ranked
|
|
WHERE class_rank <= 1
|
|
ORDER BY label;
|
|
```
|
|
|
|
The result is **document 10 for cat and document 20 for dog**. Change the cap
|
|
from `1` to the maximum number you want per class. A class with fewer records
|
|
cannot supply the full cap.
|
|
|
|
This selects by score, not at random. For a random subset, use
|
|
[sampling](/curation/sampling) instead of ordering by score or ID.
|
|
|
|
## Training and evaluation splits {#splits}
|
|
|
|
Ranking can divide ordered records into groups, but ordering by ID does not
|
|
make those groups random or representative. `ntile()` produces approximately
|
|
equal-sized buckets within a partition; small classes may not populate every
|
|
bucket.
|
|
|
|
Choose the split policy for the task. Keep related records, such as revisions
|
|
of one document or frames from one video, in the same split to avoid leakage.
|
|
A time-based evaluation may instead require a cutoff date. Save the final
|
|
assignments or resulting datasets so later updates do not silently change the
|
|
evaluation population.
|
|
|
|
## Compare neighboring records {#offset}
|
|
|
|
`lag()` and `lead()` read neighboring rows in the specified order. For example,
|
|
inspect how scores change across a document's records:
|
|
|
|
```sql
|
|
SELECT doc_id, row_id, score,
|
|
lag(score) OVER (
|
|
PARTITION BY doc_id ORDER BY version, row_id
|
|
) AS previous_score
|
|
FROM curation_documents
|
|
ORDER BY doc_id, version, row_id;
|
|
```
|
|
|
|
The first record in each document has no previous score. In this dataset,
|
|
row 2 follows a score of 0.80 and row 5 follows a score of 0.70.
|
|
|
|
<span id="running"></span>
|
|
## Window query performance {#cost}
|
|
|
|
Window operations can require sorting. Filter out records you do not need
|
|
before ranking, and inspect the
|
|
[query plan](/search/explain) when cost matters.
|
|
|
|
For cumulative counts, sums and other running summaries, use an aggregate
|
|
with an explicit window order and frame. The
|
|
[window reference](/sql/dql/window#window) covers those expressions.
|
|
|
|
## Continue with the curated data {#reference}
|
|
|
|
[Profile the selected records](/curation/aggregate) and
|
|
[save the result](/curation#next) for downstream use.
|
|
See [Window functions](/sql/dql/window) for the full syntax and function list.
|