Full Text Search
Polecat searches text over an index you declare on a member:
opts.Schema.For<Article>().FullTextIndex(x => x.Body);then query it from LINQ:
var hits = await session.Query<Article>()
.Where(x => x.Body.PlainTextSearch("quick brown fox"))
.ToListAsync();PlainTextSearch requires every term, in any order. PhraseSearch requires them adjacent and in order:
var exact = await session.Query<Article>()
.Where(x => x.Body.PhraseSearch("quick brown fox"))
.ToListAsync();Both names mirror Marten's, so the same query reads the same against either store.
WebStyleSearch takes a search box's raw contents, so you can hand it whatever the user typed:
var hits = await session.Query<Article>()
.Where(x => x.Body.WebStyleSearch("\"quick brown\" fox -turtle"))
.ToListAsync();- bare words are required
"quoted text"is a phrase- a leading
-excludes - a bare
orseparates alternatives
It mirrors Marten's operator, which uses PostgreSQL's websearch_to_tsquery. Two differences are worth knowing: Polecat does not stem (see below), and or splits at the top level — a b or c is (a AND b) OR (c), where PostgreSQL binds it as a AND (b OR c). The simpler rule is the one that can be explained to someone typing into a box; if you need real boolean precedence, compose PlainTextSearch and PhraseSearch with C#'s own && and ||, where the precedence is the language's rather than ours.
A query of nothing but exclusions matches nothing — "not this" is not a search.
PrefixSearch matches from the start of a term, which is what a search-as-you-type box wants — the last word a user has typed is a prefix of what they mean, not a word:
var suggestions = await session.Query<Article>()
.Where(x => x.Body.PrefixSearch("qui")) // finds "quick"
.ToListAsync();Each word is a prefix and all of them are required, so PrefixSearch("qui bro") wants a document with a term starting qui and another starting bro. This mirrors Marten's operator of the same name, which appends PostgreSQL's :* to each word of a to_tsquery. A whole term is a prefix of itself, so PrefixSearch is a superset of PlainTextSearch.
From the start of a term, not anywhere inside one
PrefixSearch("uic") does not find quick. The token table stores whole terms, so a substring search against it is not a slow query — it is an empty result that looks like an answer. Marten reaches that capability through a separate ngram index and Fisher through a trigram tokenizer; Polecat has neither yet.
An empty or all-punctuation prefix matches nothing rather than everything, the same answer PlainTextSearch gives an empty search — a search box on first render should not return the whole table. That also disposes of LIKE pattern syntax: %, _ and [ are punctuation to the tokenizer, so they are stripped before the prefix is ever compared and cannot act as wildcards.
This is Polecat's own index, not SQL Server's full-text engine
Worth knowing up front, because it sets expectations that nothing else will.
SQL Server has a full-text engine, and Polecat deliberately does not use it. It is absent from the official mssql/server container images, it is refused outright in master, it cannot index a computed column — and a Polecat document body is JSON — and it populates asynchronously, so a document written and immediately searched may not be found.
Polecat maintains its own inverted index instead: one row per token in a side table beside the document table, kept in step by a trigger. The consequences that matter to you:
- A write is searchable the moment it commits. No population lag, no waiting, no flaky tests.
- It runs anywhere Polecat runs — the stock container, Azure SQL Edge, Azure SQL Database.
- Declaring an index on existing documents backfills them. The index covers rows written before it existed, so you do not reindex by hand.
- The token table and its trigger are part of your schema, not something conjured at runtime. They appear in
db-dumpandAdvanced.ToDatabaseScript(),AssertDatabaseMatchesConfigurationAsync()compares them, and underAutoCreate.Nonea missing one is refused rather than silently created — so a database built from a generated script answers searches, where before it would have accepted writes and answered every search empty. The backfill is the exception and is not part of the schema: it writes rows, so it runs after the schema converges.
And the cost, stated plainly:
By default, tokenization is literal
Terms are lowercased and split on whitespace and punctuation, and that is all. running does not match run, and configuring retries does not match configure retry — unless you opt into stemming. There is no thesaurus and no language-aware word breaking either way, so mice never matches mouse.
Marten's regConfig overloads have no counterpart here — a PostgreSQL text-search configuration means nothing against an index Polecat tokenizes itself — so they are deliberately absent rather than accepted and quietly ignored.
Ranking
Where answers which documents match. For how well, there is a ranked search scored with Okapi BM25:
var best = await session.FullTextSearchAsync<Article>(x => x.Body, "fox", limit: 10);
var scored = await session.FullTextSearchWithScoresAsync<Article>(x => x.Body, "fox", limit: 10);
var confident = scored.Where(m => m.Score > 1.0).Select(m => m.Document);Larger is more relevant. A document mentioning a term three times in a short body outranks one mentioning it once in a long one — term frequency up, length normalization down, which is what BM25 is for.
Tuning BM25
The two BM25 parameters default to the conventional values — k1 = 1.2 for term-frequency saturation and b = 0.75 for length normalization — and both ranked calls take a FullTextSearchOptions to change them:
// A title field: short by nature, so do not penalize length at all.
var titles = await session.FullTextSearchAsync<Article>(
x => x.Title, "fox", options: new FullTextSearchOptions(B: 0));
// Ignore term frequency entirely — a document's score becomes the sum of its terms' IDF.
var idfOnly = await session.FullTextSearchAsync<Article>(
x => x.Body, "fox", options: new FullTextSearchOptions(K1: 0));k1 must not be negative and b must be within [0, 1]; anything else is refused at construction with an ArgumentOutOfRangeException naming the parameter, rather than silently producing a ranking that looks plausible and is not. k1 = 0 and b = 0 are both legal and both meaningful.
They are per call rather than per index on purpose: one corpus is often queried two ways, and binding the constants to the index would force a second index to say so. They bind as SQL parameters, so a different k1 reuses the same query plan.
Scores from different options are not comparable
This is on top of BM25 scores already not being comparable across corpora. A relevance floor tuned against the defaults is meaningless against b = 0. Ordering within one result set is what these are safe for.
A hybrid search scores its text leg with the defaults. HybridSearchOptions is shared across the Critter Stack and cannot carry Polecat-only constants.
Scores compare within one result set, not between two
BM25's inverse-document-frequency term depends on the corpus, so a document's score moves as other documents are written. Use it to order results, or as a relative floor inside one search. Do not store it as a measure of relevance or compare it across searches.
Ranking derives its statistics from the token rows at query time rather than from a maintained statistics table, so a ranked search reads the token table for that member. This is correct before it is fast; a large corpus with tight latency requirements is a reason to measure.
Filtering a ranked search
Both ranked calls take an optional filter, the same LINQ predicate vector search takes:
var best = await session.FullTextSearchAsync<Article>(
x => x.Body, "fox", limit: 10, filter: x => x.Team == "red");It is applied before the limit, so you get the best limit of the filtered set rather than the filtered remains of the best limit — the same reason it matters there. A Where on the LINQ operator is still the right tool when you do not need the ranking; this exists so a ranked search can be scoped without losing its ordering.
Stemming
Declare an index with FullTextTokenizer.Porter and its terms are reduced to their stems, so different forms of a word match each other:
opts.Schema.For<Article>().FullTextIndex(FullTextTokenizer.Porter, x => x.Body);
// matches a body of "Configure retry for the outbox"
var hits = await session.Query<Article>()
.Where(x => x.Body.PlainTextSearch("configuring retries"))
.ToListAsync();The stemmer is Martin Porter's English stemmer, the same one SQLite's FTS5 porter tokenizer uses, so Polecat and Fisher stem identically. It applies to every search operator against that index, and to ranked search.
The tokenizer is per index, so a member holding identifiers or codes can stay literal while prose stems:
opts.Schema.For<Article>()
.FullTextIndex(x => x.Sku) // literal
.FullTextIndex(FullTextTokenizer.Porter, x => x.Body); // stemmedWhat to know before you turn it on:
- Only plain English words are stemmed. A term is stemmed when it is 3 to 64 lowercase ASCII letters; anything else (digits, accented letters, very short or very long terms) is stored and searched as written. So
cafésdoes not matchcafé, where SQLite, which folds accents first, would. - Prefix searches are stemmed too, as SQLite's are.
PrefixSearch("configuring")findsconfigure, andPrefixSearch("config")finds it as well. The edge case is a partial word whose stem is not a prefix of the stored stem:PrefixSearch("configuri")finds nothing. - Writes cost more. Stemming runs inside the trigger that maintains the index, at roughly 160 microseconds a term, so about a sixth of a second for a thousand-word document. Searching costs nothing extra.
- Changing an existing index's tokenizer is safe. Stemmed and literal terms are stored under different keys, so the next schema migration re-tokenizes every document already written, and removes the terms the old tokenizer produced. Without that, a newly stemmed index would quietly miss every document written before the change.
- The stemmer is part of your schema. It is two T-SQL functions per schema,
pc_porter_stemandpc_porter_cv, created and compared like any other schema object: they appear in a generated script, andAutoCreate.Nonerefuses a database that is missing them.
How the two halves are kept in agreement
A query term is stemmed in C#, and a stored term is stemmed by T-SQL inside the trigger. If they ever disagreed, a search would not fail — it would quietly return nothing. So both are tested against the same independent reference: SQLite's own Porter stemmer over 23,549 dictionary words, with the T-SQL version run inside SQL Server.
Several members
FullTextIndex takes more than one member, and each is searched independently:
opts.Schema.For<Article>().FullTextIndex(x => x.Title, x => x.Body);
var hits = await session.Query<Article>()
.Where(x => x.Title.PlainTextSearch("fox") || x.Body.PlainTextSearch("fox"))
.ToListAsync();Long text and long tokens
A member is indexed in full, however long it is. Long text is what full-text search is for.
A single token is capped at 255 characters, the width of the term column. A longer one, like a base64 blob or a long identifier with no separators in it, is clipped to its first 255 characters, and a search term is clipped the same way, so searching for the whole value still finds the document. The cost is that two tokens sharing their first 255 characters match each other. Clipping is deliberate: dropping the token would save the document and leave a value it does contain unfindable, with no error.
What separates terms is every character .NET's char.IsWhiteSpace accepts, plus the punctuation list. The stored position counts terms rather than characters, so a phrase search finds quick brown in quick, brown and across a line break.
Text outside Western European languages
The term column is nvarchar, so a term in any script is stored and found as written. The tokenizer only knows about whitespace and punctuation though, which matters for languages that do not put spaces between words: a run of Chinese or Japanese is one term, so 東京 does not find 東京都に住む, though 東京都に住む does. Comparison follows the database's collation. The default SQL_Latin1_General_CP1_CI_AS gives some rarer characters no weight at all, so CJK Extension A characters compare equal to each other under it, and it does not tell hiragana from katakana. A collation such as Latin1_General_100_CI_AS separates the first.
A token table created by 5.47 or earlier has a varchar term column, which kept only what the database's code page could hold: 東京 was stored as ?? and could not be found. Polecat leaves such a table working as it was rather than changing it during a request, because that is a long operation on a large table. The same goes for the doc_id index, without which every write to the type scans the token table. See the migration guide.
Rebuilding the index
Stored tokens are only ever as good as the tokenizer that wrote them. When the tokenizer changes, as it did for line breaks and tabs, new writes pick that up straight away and existing documents keep what they have until they are next written. Rebuild to bring them up to date:
await store.Advanced.RebuildFullTextIndexAsync<Article>();It writes each row back unchanged, which fires the maintaining trigger, so there is one tokenizer in the system rather than two, and a concurrent write to the same document is ordinary row locking rather than a race. The document, its version and its timestamps do not change, searches keep working while it runs, and it is safe to run on a live store or to run again. Cancelling stops between batches and what ran stays done. Under database-per-tenant tenancy it covers every database.
A batch is one transaction and has CommandTimeout to finish. The default of 100 documents suits documents of several thousand characters: over 5,000 character documents it took about 2.3 seconds a batch, and 8,000 of them took about three minutes. For much longer documents, lower it with RebuildFullTextIndexAsync<Article>(batchSize: 10).
Polecat does not do this for you at startup, and that is on purpose. A document with some tokens looks finished to the backfill however stale they are, because telling otherwise means re-tokenizing it to compare. A document with none, like a member over 4000 characters written by an older version, would be visible to it, but the backfill runs inside whichever request touches the type first. Work on that scale should be something you schedule rather than something a deployment does to you.
Declaring a new index, or changing an index's tokenizer, is different: there is nothing to preserve, so the backfill covers every document, however long, in the first request to touch the type. It works in batches of 100 documents, each with CommandTimeout to finish, so a large store makes that request slow and does not make it fail, and it does not stop if the request is cancelled. If the process dies partway, ordinary documents are picked up by the next first use, and a rebuild is what covers any over 4000 characters that were left.
Apply the schema change first
The tokenizer is in the trigger. Under AutoCreate.None nothing replaces an older trigger, and rebuilding through it would recreate the tokens you are getting rid of. The same goes for a token table that still has a varchar term column, under any setting, because nothing widens that one at first use. So the rebuild refuses with an InvalidOperationException until db-patch or ApplyAllConfiguredChangesToDatabaseAsync() has been run.
An empty search matches nothing
A search whose text has no terms — empty, whitespace, or punctuation alone — returns no documents rather than all of them. A search box the user has not typed into should not return the entire table, and Marten's plainto_tsquery('') behaves the same way.
Conjoined multi-tenancy
Every full-text path is tenant-scoped, and so is the index behind it. A document id is only unique per tenant under conjoined tenancy — the document table puts tenant_id in its primary key — so the token table carries the tenant too, and each of PlainTextSearch, PhraseSearch, PrefixSearch, WebStyleSearch, FullTextSearchWithScoresAsync and the text leg of a hybrid search sees only the session tenant's documents.
The token table is maintained by a trigger and backfilled when the index is declared, and both are keyed on (doc_id, tenant_id) rather than on doc_id alone. They were not always, and #625 had three consequences worth knowing if you are upgrading across it:
PhraseSearchcould assemble a phrase from two tenants' documents, returning a document that does not contain the phrase.- An ordinary write in one tenant deleted another tenant's tokens, dropping that document out of every full-text path until it was next written.
- Declaring the index over existing rows could skip a tenant whose id was already indexed by another.
TIP
Upgrading repairs itself. A changed trigger body is reconciled like any other schema change, and the backfill runs whenever storage is ensured, so the corrected trigger replaces the old one and the corrected backfill restores the tokens the old one destroyed. Nothing has to be rebuilt by hand.
A single-tenant store is unaffected — its document table has no tenant_id column at all, and an id is a complete key on its own.
Combining with vector search
Full-text search and vector search answer different questions: one finds the words you asked for, the other finds meaning near what you asked for. Hybrid search fuses both.
In the other stores
Fisher: Full Text Search, over SQLite's FTS5. Marten: Full Text Searching, over PostgreSQL's tsvector.

JasperFx provides formal support for Polecat and other Critter Stack libraries. Please check our