Skip to content

hsearch

Tantivy-backed BM25 index access method with WAL-logged storage.

hsearch · Tantivy-backed BM25 index access method with WAL-logged storage.

Overview

IDExtensionPackageVersionCategoryLicenseLanguage
101889hsearchhsearch0.4.1FTSAGPL-3.0-onlyRust

PostgreSQL · 18

Usage

Sources:

hsearch provides a Tantivy-backed bm25 index access method for PostgreSQL 18 while storing index bytes in PostgreSQL pages protected by generic WAL.

Enablement

Install hsearch 0.4.1 for PostgreSQL 18, add it to shared_preload_libraries, and restart before creating the extension:

shared_preload_libraries = 'hsearch'
CREATE EXTENSION hsearch;

The extension is non-relocatable and superuser-only. Its SQL objects live in the hyper schema.

Build and Query an Index

The first indexed column is the stable key named by key_field; other indexed expressions are searchable text fields.

CREATE TABLE items (
  id      varchar(24) PRIMARY KEY,
  name    text,
  summary text
);

CREATE INDEX items_bm25 ON items
USING bm25 (
  id,
  (name::hyper.ngram(2,5,'ascii_folding=true')),
  (summary::hyper.ngram(2,5,'ascii_folding=true'))
) WITH (key_field='id');

SELECT id, hyper.score(id) AS score
FROM items
WHERE name &&& 'postgres'
ORDER BY score DESC
LIMIT 20;

&&& requires all generated ngrams and rechecks heap visibility. Rows inserted in the current transaction become searchable at commit.

Operations and Limits

The index is WAL-logged for crash recovery and physical replication, but it remains a derived structure: use REINDEX or hyper.reindex_all() after corruption or when operational policy calls for rebuilding. hsearch.max_matches caps candidates per scan key (default 1,000); increasing it improves deep filtered recall at higher memory and latency cost. Unlogged tables and tokenizer settings other than (2,5,'ascii_folding=true') are not supported in 0.4.1.

Table reader