mirror of
https://github.com/lancedb/lancedb.git
synced 2026-12-03 07:45:17 +00:00
docs(node): clarify full-text search filtering (#3813)
## Summary - Replace the website's TypeScript full-text filtering examples with a snippet generated from `docs/web-tests/ts/full_text_search.test.ts`. In a full-text-only query, `where(...)` filters rows before the search selects candidates. - Update the guide and regenerate the Node API reference. Vector and hybrid queries also prefilter by default and can use `VectorQuery.postfilter()`. In hybrid search, it filters candidates from both searches before reranking. ## Root cause The website's TypeScript examples used the legacy `vectordb` method `.prefilter(...)`, which is absent from `@lancedb/lancedb`. The Node API reference left full-text and hybrid filter timing implicit. ## Validation - Ran `full_text_search.test.ts`, `filtering.test.ts`, and `search_examples.test.ts` in `docs/web-tests/ts` with the locked npm SDK. All eight tests passed, as did a targeted TypeScript check of those files. - Ran the exact snippet imported by the guide against the locked npm SDK and the SDK built from this branch. Both runs returned the expected full-text match and confirmed prefiltering and postfiltering behavior for vector and hybrid queries. - Built the Node native binding, compiled the SDK, and regenerated the TypeDoc reference. Biome passed for the SDK, SDK examples, and the changed website test. The full website test directory produced the same existing Biome and TypeScript diagnostics as `main`. Fixes #2237 <!-- lance-gatekeeper-fix:v1 agent=1e80a282d1024d8eadf8e0ff33d83c9d generation=1 --> --------- Co-authored-by: Gatefixer <313497061+lancedb-gatefixer[bot]@users.noreply.github.com> Co-authored-by: Xuanwo <github@xuanwo.io>
This commit is contained in:
co-authored by
Xuanwo
parent
b3a8e6f08d
commit
43d28def80
@@ -495,6 +495,10 @@ where(predicate): this
|
||||
|
||||
A filter statement to be applied to this query.
|
||||
|
||||
Full-text-only queries apply the filter before retrieving matches. Vector
|
||||
and hybrid queries also prefilter by default. Call
|
||||
[VectorQuery.postfilter](VectorQuery.md#postfilter) to filter their candidates after retrieval.
|
||||
|
||||
The filter may be an SQL query string or an expression built with `col`,
|
||||
which preserves case-sensitive column names. For example:
|
||||
|
||||
@@ -524,6 +528,14 @@ on the filter column(s).
|
||||
Calling this multiple times combines the filters with a logical AND rather
|
||||
than replacing the previous filter.
|
||||
|
||||
```ts
|
||||
const results = await table
|
||||
.search("puppy", "fts")
|
||||
.where("category = 'pet'")
|
||||
.limit(1)
|
||||
.toArray();
|
||||
```
|
||||
|
||||
#### Inherited from
|
||||
|
||||
`StandardQueryBase.where`
|
||||
|
||||
@@ -569,6 +569,10 @@ where(predicate): this
|
||||
|
||||
A filter statement to be applied to this query.
|
||||
|
||||
Full-text-only queries apply the filter before retrieving matches. Vector
|
||||
and hybrid queries also prefilter by default. Call
|
||||
[VectorQuery.postfilter](VectorQuery.md#postfilter) to filter their candidates after retrieval.
|
||||
|
||||
The filter may be an SQL query string or an expression built with `col`,
|
||||
which preserves case-sensitive column names. For example:
|
||||
|
||||
@@ -598,6 +602,14 @@ on the filter column(s).
|
||||
Calling this multiple times combines the filters with a logical AND rather
|
||||
than replacing the previous filter.
|
||||
|
||||
```ts
|
||||
const results = await table
|
||||
.search("puppy", "fts")
|
||||
.where("category = 'pet'")
|
||||
.limit(1)
|
||||
.toArray();
|
||||
```
|
||||
|
||||
#### Inherited from
|
||||
|
||||
`StandardQueryBase.where`
|
||||
|
||||
@@ -614,32 +614,22 @@ An Arrow Schema describing the output columns.
|
||||
postfilter(): VectorQuery
|
||||
```
|
||||
|
||||
If this is called then filtering will happen after the vector search instead of
|
||||
before.
|
||||
Apply the filter after retrieving search candidates. For a hybrid query,
|
||||
this filters both the vector and full-text candidates before reranking.
|
||||
|
||||
By default filtering will be performed before the vector search. This is how
|
||||
filtering is typically understood to work. This prefilter step does add some
|
||||
additional latency. Creating a scalar index on the filter column(s) can
|
||||
often improve this latency. However, sometimes a filter is too complex or scalar
|
||||
indices cannot be applied to the column. In these cases postfiltering can be
|
||||
used instead of prefiltering to improve latency.
|
||||
Vector and hybrid queries prefilter by default. A scalar index on the
|
||||
filter columns can reduce prefiltering latency. Postfiltering can help when
|
||||
the predicate is too complex for a scalar index or the column cannot be indexed.
|
||||
|
||||
Post filtering applies the filter to the results of the vector search. This means
|
||||
we only run the filter on a much smaller set of data. However, it can cause the
|
||||
query to return fewer than `limit` results (or even no results) if none of the nearest
|
||||
results match the filter.
|
||||
|
||||
Post filtering happens during the "refine stage" (described in more detail in
|
||||
Postfiltering can return fewer than `limit` results, including zero, when
|
||||
the top candidates fail the filter. For vector search, this filtering
|
||||
happens during the refine stage, so increasing [VectorQuery.refineFactor](VectorQuery.md#refinefactor)
|
||||
can help recover matches.
|
||||
|
||||
#### Returns
|
||||
|
||||
[`VectorQuery`](VectorQuery.md)
|
||||
|
||||
#### See
|
||||
|
||||
[VectorQuery#refineFactor](VectorQuery.md#refinefactor)). This means that setting a higher refine
|
||||
factor can often help restore some of the results lost by post filtering.
|
||||
|
||||
***
|
||||
|
||||
### refineFactor()
|
||||
@@ -890,6 +880,10 @@ where(predicate): this
|
||||
|
||||
A filter statement to be applied to this query.
|
||||
|
||||
Full-text-only queries apply the filter before retrieving matches. Vector
|
||||
and hybrid queries also prefilter by default. Call
|
||||
[VectorQuery.postfilter](VectorQuery.md#postfilter) to filter their candidates after retrieval.
|
||||
|
||||
The filter may be an SQL query string or an expression built with `col`,
|
||||
which preserves case-sensitive column names. For example:
|
||||
|
||||
@@ -919,6 +913,14 @@ on the filter column(s).
|
||||
Calling this multiple times combines the filters with a logical AND rather
|
||||
than replacing the previous filter.
|
||||
|
||||
```ts
|
||||
const results = await table
|
||||
.search("puppy", "fts")
|
||||
.where("category = 'pet'")
|
||||
.limit(1)
|
||||
.toArray();
|
||||
```
|
||||
|
||||
#### Inherited from
|
||||
|
||||
`StandardQueryBase.where`
|
||||
|
||||
@@ -43,3 +43,28 @@ test("full text search", async () => {
|
||||
// --8<-- [end:full_text_search]
|
||||
});
|
||||
}, 10_000);
|
||||
|
||||
test("full-text search prefilters with where", async () => {
|
||||
await withTempDirectory(async (databaseDir) => {
|
||||
const db = await lancedb.connect(databaseDir);
|
||||
|
||||
// --8<-- [start:fts_prefiltering]
|
||||
const table = await db.createTable("documents", [
|
||||
{ text: "Puppy puppy puppy", category: "guide" },
|
||||
{ text: "Frodo was a happy puppy", category: "pet" },
|
||||
{ text: "There are several kittens playing", category: "pet" },
|
||||
]);
|
||||
await table.createIndex("text", { config: lancedb.Index.fts() });
|
||||
|
||||
const results = await table
|
||||
.search("puppy", "fts")
|
||||
.where("category = 'pet'")
|
||||
.limit(1)
|
||||
.toArray();
|
||||
// --8<-- [end:fts_prefiltering]
|
||||
|
||||
const unfiltered = await table.search("puppy", "fts").limit(1).toArray();
|
||||
expect(unfiltered.map((row) => row.category)).toEqual(["guide"]);
|
||||
expect(results.map((row) => row.text)).toEqual(["Frodo was a happy puppy"]);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -14,6 +14,7 @@ import {
|
||||
PyFtsIncrementalIndex,
|
||||
} from '/snippets/search.mdx'
|
||||
import { PyFtsBooleanQueries } from '/snippets/doc_function_examples.mdx'
|
||||
import { TsFtsPrefiltering } from '/snippets/full_text_search.mdx'
|
||||
|
||||
import { PyTextSearch, TsTextSearchBody, TsTextSearchTail } from '/snippets/search_examples.mdx';
|
||||
import { RsExampleBody as RsTextSearchBody, RsExampleTail as RsTextSearchTail } from '/snippets/search_text_search.mdx';
|
||||
@@ -367,11 +368,15 @@ table.create_fts_index(
|
||||
|
||||
### Filtering Options {#filtering-options}
|
||||
|
||||
Use `where(...)` to restrict text results to rows that match a predicate.
|
||||
Prefiltering applies the restriction before retrieval; postfiltering removes
|
||||
nonmatching candidates afterward and can leave fewer results.
|
||||
|
||||
With pre-filtering:
|
||||
Prefiltering restricts eligible rows before the search selects candidates.
|
||||
Postfiltering removes nonmatching candidates after retrieval and can return
|
||||
fewer results than requested.
|
||||
|
||||
With TypeScript full-text-only queries, `where(...)` applies the filter before
|
||||
retrieval. The TypeScript example below uses the `db` connection from the setup
|
||||
above to create an indexed table with a `category` column.
|
||||
|
||||
#### Prefiltering
|
||||
|
||||
<CodeGroup>
|
||||
|
||||
@@ -379,15 +384,9 @@ With pre-filtering:
|
||||
{PyFtsPrefiltering}
|
||||
</CodeBlock>
|
||||
|
||||
```typescript TypeScript icon="square-js"
|
||||
await tbl
|
||||
.search("puppy")
|
||||
.select(["id", "doc"])
|
||||
.limit(10)
|
||||
.where("meta='foo'")
|
||||
.prefilter(true)
|
||||
.toArray();
|
||||
```
|
||||
<CodeBlock filename="TypeScript" language="typescript" icon="square-js">
|
||||
{TsFtsPrefiltering}
|
||||
</CodeBlock>
|
||||
|
||||
```rust Rust icon="Rust"
|
||||
table
|
||||
@@ -401,23 +400,17 @@ table
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
With post-filtering:
|
||||
#### Postfiltering
|
||||
|
||||
TypeScript supports `VectorQuery.postfilter()` for vector and hybrid searches.
|
||||
On a hybrid query, it filters candidates from both the vector and full-text
|
||||
searches before reranking. See [hybrid search](/search/hybrid-search#prefilter-vs-postfilter).
|
||||
|
||||
<CodeGroup>
|
||||
<CodeBlock filename="Python" language="python" icon="python">
|
||||
{PyFtsPostfiltering}
|
||||
</CodeBlock>
|
||||
|
||||
```typescript TypeScript icon="square-js"
|
||||
await tbl
|
||||
.search("apple")
|
||||
.select(["id", "doc"])
|
||||
.limit(10)
|
||||
.where("meta='foo'")
|
||||
.prefilter(false)
|
||||
.toArray();
|
||||
```
|
||||
|
||||
```rust Rust icon="Rust"
|
||||
table
|
||||
.query()
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
{/* Auto-generated by scripts/mdx_snippets_gen.py. Do not edit manually. */}
|
||||
|
||||
export const TsFtsPrefiltering = "const table = await db.createTable(\"documents\", [\n { text: \"Puppy puppy puppy\", category: \"guide\" },\n { text: \"Frodo was a happy puppy\", category: \"pet\" },\n { text: \"There are several kittens playing\", category: \"pet\" },\n]);\nawait table.createIndex(\"text\", { config: lancedb.Index.fts() });\n\nconst results = await table\n .search(\"puppy\", \"fts\")\n .where(\"category = 'pet'\")\n .limit(1)\n .toArray();\n";
|
||||
|
||||
export const TsFullTextSearch = "const result = await tbl\n .query()\n .nearestToText(\"apple\")\n .select([\"id\", \"doc\"])\n .limit(10)\n .toArray();\nexpect(result.length).toBe(10);\n";
|
||||
|
||||
|
||||
+22
-16
@@ -781,6 +781,10 @@ export class StandardQueryBase<
|
||||
/**
|
||||
* A filter statement to be applied to this query.
|
||||
*
|
||||
* Full-text-only queries apply the filter before retrieving matches. Vector
|
||||
* and hybrid queries also prefilter by default. Call
|
||||
* {@link VectorQuery.postfilter} to filter their candidates after retrieval.
|
||||
*
|
||||
* The filter may be an SQL query string or an expression built with `col`,
|
||||
* which preserves case-sensitive column names. For example:
|
||||
* @example
|
||||
@@ -797,6 +801,15 @@ export class StandardQueryBase<
|
||||
*
|
||||
* Calling this multiple times combines the filters with a logical AND rather
|
||||
* than replacing the previous filter.
|
||||
*
|
||||
* @example Filter a full-text search
|
||||
* ```ts
|
||||
* const results = await table
|
||||
* .search("puppy", "fts")
|
||||
* .where("category = 'pet'")
|
||||
* .limit(1)
|
||||
* .toArray();
|
||||
* ```
|
||||
*/
|
||||
where(predicate: string | Expr): this {
|
||||
this.doCall((inner: NativeQueryType) => {
|
||||
@@ -1171,24 +1184,17 @@ export class VectorQuery extends StandardQueryBase<NativeVectorQuery> {
|
||||
}
|
||||
|
||||
/**
|
||||
* If this is called then filtering will happen after the vector search instead of
|
||||
* before.
|
||||
* Apply the filter after retrieving search candidates. For a hybrid query,
|
||||
* this filters both the vector and full-text candidates before reranking.
|
||||
*
|
||||
* By default filtering will be performed before the vector search. This is how
|
||||
* filtering is typically understood to work. This prefilter step does add some
|
||||
* additional latency. Creating a scalar index on the filter column(s) can
|
||||
* often improve this latency. However, sometimes a filter is too complex or scalar
|
||||
* indices cannot be applied to the column. In these cases postfiltering can be
|
||||
* used instead of prefiltering to improve latency.
|
||||
* Vector and hybrid queries prefilter by default. A scalar index on the
|
||||
* filter columns can reduce prefiltering latency. Postfiltering can help when
|
||||
* the predicate is too complex for a scalar index or the column cannot be indexed.
|
||||
*
|
||||
* Post filtering applies the filter to the results of the vector search. This means
|
||||
* we only run the filter on a much smaller set of data. However, it can cause the
|
||||
* query to return fewer than `limit` results (or even no results) if none of the nearest
|
||||
* results match the filter.
|
||||
*
|
||||
* Post filtering happens during the "refine stage" (described in more detail in
|
||||
* @see {@link VectorQuery#refineFactor}). This means that setting a higher refine
|
||||
* factor can often help restore some of the results lost by post filtering.
|
||||
* Postfiltering can return fewer than `limit` results, including zero, when
|
||||
* the top candidates fail the filter. For vector search, this filtering
|
||||
* happens during the refine stage, so increasing {@link VectorQuery.refineFactor}
|
||||
* can help recover matches.
|
||||
*/
|
||||
postfilter(): VectorQuery {
|
||||
this.doVectorCall((inner) => inner.postfilter());
|
||||
|
||||
Reference in New Issue
Block a user