Full Text Search
An xpb plugin for PocketBase that adds configurable SQLite FTS5 full text search and an API endpoint for searching configured collections.

Fork
This repository was forked from pocketbuilds/fts.
This fork adds a settings UI through pb-plugin-base, stores one configuration per collection in _plugins_fts, and supports field weights, optional snippets and highlights, and configurable prefix queries and indexes. Runtime configuration is managed through PocketBase rather than pocketbuilds.toml.
Compatibility
| Dependency |
Version |
| PocketBase |
v0.42.3 |
| xpb |
v0.1.0 |
| pb-plugin-base (settings UI) |
v0.3.0 |
| Go |
1.27.1+ |
PocketBase now lives at ganigeorgiev.com/pocketbase/pocketbase. Custom Go applications must use the ganigeorgiev.com/pocketbase/* imports alongside xpb v0.1.0. PocketBase's Superuser UI extension API is experimental; recheck compatibility before upgrading to a newer minor release.
Installation
Install the current xpb CLI:
go install github.com/pocketbuilds/xpb/cmd/xpb@v0.1.0
Build PocketBase with the plugin:
xpb build v0.42.3 --with github.com/mjadobson/pb-plugin-fts@v0.5.0
Setup
Install pb-plugin-base in the same PocketBase build to expose the FTS settings UI:
xpb build v0.42.3 \
--with github.com/mjadobson/pb-plugin-base@v0.3.0 \
--with github.com/mjadobson/pb-plugin-fts@v0.5.0
Open Settings → Plugins → Full Text Search and add one configuration for each collection you want to index.
Each configuration is stored as its own record in the system collection _plugins_fts. When _plugins_fts is created for the first time, existing enabled fts configuration in the legacy shared _plugins collection is imported automatically. Collection creation and import are transactional, so a failed import is retried on the next startup. The legacy row is left untouched, but it is never imported again after _plugins_fts has been created; deleting all new FTS configurations therefore remains persistent across restarts.
The plugin reloads its active configuration whenever a row in _plugins_fts is created, updated, or deleted, and when collections change. Removing a configuration also removes the generated FTS table and triggers for that collection.
Plugin Config
collection_name
The PocketBase collection to index. Only one FTS configuration is allowed per collection.
fields
Optional array of field names to index. Leave it empty to index all public fields. The id field is always added automatically.
field_weights
Optional object mapping indexed field names to BM25 ranking weights. Omitted indexed fields, including id, default to 1.
tokenizer
Optional FTS5 tokenizer. Defaults to porter.
allow_snippets
Enables snippet= auxiliary output for the collection. Defaults to false.
allow_highlights
Enables highlight= auxiliary output for the collection. Defaults to false.
min_prefix_query_length
Minimum term length required to keep a prefix query such as moon*. Shorter prefix terms are downgraded to normal terms. The default 0 disables prefix-query behaviour.
prefixes
Optional FTS5 prefix index lengths such as [2, 3]. Prefix indexes can improve prefix-query performance at the cost of index size.
A stored configuration is equivalent to:
{
"collection_name": "news",
"fields": ["title", "body"],
"field_weights": {
"title": 10,
"body": 1
},
"allow_snippets": true,
"allow_highlights": true,
"min_prefix_query_length": 3,
"prefixes": [2, 3],
"tokenizer": "porter"
}
FTS API
GET /api/collections/{collection}/records/fts
When no sort query parameter is provided, the plugin orders results by SQLite FTS5 rank. If field_weights is configured, it uses a weighted bm25(...) rank so matches in more important fields rise to the top.
Prefix queries like moon* are downgraded to normal term searches by default. To allow them efficiently, set min_prefix_query_length and configure suitable prefixes values for the collection. For example, with min_prefix_query_length: 3, mo* becomes mo while moon* remains a prefix query.
You can also request SQLite FTS5 auxiliary output for matched rows:
highlight=field1,field2 adds _fts_highlight_<field> values to each returned record
snippet=field1,field2 adds _fts_snippet_<field> values to each returned record
highlightBefore / highlightAfter customize highlight() markup and default to <b> / </b>
snippetBefore / snippetAfter customize snippet() markup and default to <b> / </b>
snippetEllipsis customizes the snippet truncation marker and defaults to ...
snippetTokens controls snippet length and must be between 1 and 64 (default 16)
These auxiliary values are only computed for the paged result set returned by the current search, not for the full collection.
They are disabled by default and must be explicitly enabled per collection with allow_snippets and allow_highlights.
Using the FTS Api Endpoint
JavaScript SDK
// One-off use of the endpoint
const pb = new PocketBase("https://example.com");
const collectionName = "news";
const result = await pb.send(`/api/collections/${collectionName}/records/fts`, {
query: {
page: 1,
perPage: 20,
sort: "+created", // keep blank to use sort by fts rank
filter: 'status = true && created > "2022-08-01 10:00:00"',
// Use MATCH value
// https://sqlite.org/fts5.html
search: "ghosts OR aliens",
highlight: "title",
snippet: "body",
},
});
// Use beforeSend to route collection list requests through FTS.
const pb = new PocketBase("https://example.com");
const re = new RegExp("/api/collections/[^/]+/records$");
pb.beforeSend = (url, options) => {
if (re.test(url) && options.method === "GET") {
url += "/fts";
}
return { url, options };
};
const result = await pb.collection("news").getList(1, 20, {
sort: "+created", // omit to use FTS rank
filter: 'status = true && created > "2022-08-01 10:00:00"',
search: "ghosts OR aliens",
highlight: "title",
snippet: "body",
});
Development
Build PocketBase with this local checkout and pb-plugin-base using the versions above:
make build-pocketbase-binary
This writes ./pocketbase. The settings host remains an optional build dependency; the FTS API can also run without pb-plugin-base.
Run the compatibility checks:
go test ./...
go test -race ./...
go vet ./...
node --check ui/main.js