diff --git a/docs/src/js/classes/AutoQuery.md b/docs/src/js/classes/AutoQuery.md index 6dbe1c81e..311fc6a68 100644 --- a/docs/src/js/classes/AutoQuery.md +++ b/docs/src/js/classes/AutoQuery.md @@ -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` diff --git a/docs/src/js/classes/Query.md b/docs/src/js/classes/Query.md index e7a23c04d..a4dd3c53f 100644 --- a/docs/src/js/classes/Query.md +++ b/docs/src/js/classes/Query.md @@ -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` diff --git a/docs/src/js/classes/VectorQuery.md b/docs/src/js/classes/VectorQuery.md index 1e2a84f98..d269a2c81 100644 --- a/docs/src/js/classes/VectorQuery.md +++ b/docs/src/js/classes/VectorQuery.md @@ -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` diff --git a/docs/web-tests/ts/full_text_search.test.ts b/docs/web-tests/ts/full_text_search.test.ts index 9777bda7f..d6a3fe3c6 100644 --- a/docs/web-tests/ts/full_text_search.test.ts +++ b/docs/web-tests/ts/full_text_search.test.ts @@ -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"]); + }); +}); diff --git a/docs/web/search/full-text-search.mdx b/docs/web/search/full-text-search.mdx index d49d2561e..70ff4d56f 100644 --- a/docs/web/search/full-text-search.mdx +++ b/docs/web/search/full-text-search.mdx @@ -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 @@ -379,15 +384,9 @@ With pre-filtering: {PyFtsPrefiltering} -```typescript TypeScript icon="square-js" -await tbl -.search("puppy") -.select(["id", "doc"]) -.limit(10) -.where("meta='foo'") -.prefilter(true) -.toArray(); -``` + +{TsFtsPrefiltering} + ```rust Rust icon="Rust" table @@ -401,23 +400,17 @@ table ``` -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). {PyFtsPostfiltering} -```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() diff --git a/docs/web/snippets/full_text_search.mdx b/docs/web/snippets/full_text_search.mdx index 7d71409ee..594c3af49 100644 --- a/docs/web/snippets/full_text_search.mdx +++ b/docs/web/snippets/full_text_search.mdx @@ -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"; diff --git a/nodejs/lancedb/query.ts b/nodejs/lancedb/query.ts index 8cf06999e..1000f8940 100644 --- a/nodejs/lancedb/query.ts +++ b/nodejs/lancedb/query.ts @@ -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 { } /** - * 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());