mirror of
https://github.com/lancedb/lancedb.git
synced 2026-12-03 07:45:17 +00:00
feat(query): allow unordered vector and full-text search results (#5328)
## Summary Callers that consume every selected hit can relax the final output order with `ordered_results(false)` in Rust, `ordered_results(False)` in Python, or `orderedResults(false)` in TypeScript. Ranked output remains the default. The option preserves top-k selection, each hit’s score and projected columns, and the existing rules for ties at the cutoff. Embedded passes eligible queries to the Lance scanner. Enterprise sends `"ordered": false` for eligible searches and omits the field by default. Explicit sorting, positive offsets, batch vector queries and inputs to hybrid fusion or reranking keep their existing order. ## Validation A managed Linux preview passed 8 Rust wire cases, 4 Embedded Python cases, 10 HTTP mock cases and all 52 TypeScript query tests. The preview used a candidate Lance revision. Project CI on this PR validates the integration with the official Lance `v16.0.0-beta.6`. Uses the official Lance dependency from #5327, now merged into `main`.
This commit is contained in:
@@ -41,6 +41,26 @@ describe("Search subquery filters", () => {
|
||||
tmpDir.removeCallback();
|
||||
});
|
||||
|
||||
it.each(["vector", "fts"])(
|
||||
"%s search preserves the selected rows when delivery order is relaxed",
|
||||
async (kind) => {
|
||||
const search = () =>
|
||||
(kind === "vector"
|
||||
? chunks.vectorSearch([0, 0])
|
||||
: chunks.search("cat", "fts")
|
||||
).limit(1);
|
||||
const ranked = await search().toArray();
|
||||
expect(ranked.map((row) => row[documentIdColumn])).toEqual(["AAA"]);
|
||||
const scoreColumn = kind === "vector" ? "_distance" : "_score";
|
||||
const hits = (rows: typeof ranked) =>
|
||||
rows.map((row) => [row[documentIdColumn], row[scoreColumn]]);
|
||||
expect(typeof ranked[0][scoreColumn]).toBe("number");
|
||||
expect(hits(await search().orderedResults(false).toArray())).toEqual(
|
||||
hits(ranked),
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
it.each(["vector", "fts"])(
|
||||
"%s search prefilters by keys from another table",
|
||||
async (kind) => {
|
||||
|
||||
@@ -845,6 +845,16 @@ export class StandardQueryBase<
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Require vector or full-text hits in rank order (the default).
|
||||
* Set false when you only need the selected rows and scores. The engine
|
||||
* may then return the hits in any order. Explicit sort keys take precedence.
|
||||
*/
|
||||
orderedResults(ordered: boolean): this {
|
||||
this.doCall((inner) => inner.orderedResults(ordered));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sort the results by the specified column(s).
|
||||
* @returns This query builder.
|
||||
|
||||
@@ -173,6 +173,11 @@ impl Query {
|
||||
self.inner = self.inner.clone().use_rte(enable);
|
||||
}
|
||||
|
||||
#[napi]
|
||||
pub fn ordered_results(&mut self, ordered: bool) {
|
||||
self.inner = self.inner.clone().ordered_results(ordered);
|
||||
}
|
||||
|
||||
#[napi]
|
||||
pub fn order_by(&mut self, ordering: Option<Vec<ColumnOrdering>>) -> napi::Result<()> {
|
||||
let ordering = ordering.map(|ordering| {
|
||||
@@ -411,6 +416,11 @@ impl VectorQuery {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[napi]
|
||||
pub fn ordered_results(&mut self, ordered: bool) {
|
||||
self.inner = self.inner.clone().ordered_results(ordered);
|
||||
}
|
||||
|
||||
#[napi]
|
||||
pub fn order_by(&mut self, ordering: Option<Vec<ColumnOrdering>>) -> napi::Result<()> {
|
||||
let ordering = ordering.map(|ordering| {
|
||||
|
||||
Reference in New Issue
Block a user