Module

postprocess.output_formats.lunr_index_generator

Pre-built Lunr.js search index generator for Bengal SSG.

Generates a serialized Lunr index at build time using the Pythonlunrpackage, enabling faster client-side search by avoiding runtime index construction.

The pre-built index is written tosearch-index.jsonalongsideindex.json. Client-side search.js loads the pre-built index vialunr.Index.load()instead of rebuilding from raw page data on every page load.

Configuration:

search:
  lunr:
    prebuilt: true  # Enable pre-built index (default: true)

Performance Impact:

  • Build time: +5-10s for index pre-building
  • Runtime: ~50% faster initial search (no client-side index build)
  • Index size: ~60% smaller (pre-built is more compact)

Related:

  • index_generator.py: Source of page data (index.json)
  • search.js: Client-side search using pre-built or runtime index

Classes

LunrIndexGenerator
Generate pre-built Lunr.js search index at build time. Uses the Python `lunr` package (pure Python…
8

Generate pre-built Lunr.js search index at build time.

Uses the Pythonlunrpackage (pure Python implementation of Lunr.js) to build a serialized search index that can be loaded directly by the client-side Lunr.js library.

Field Boosts (matching search.js):

  • title: 10x (most important)
  • search_keywords: 8x (explicit search terms)
  • description: 5x (page summary)
  • tags: 3x (categorization)
  • section: 2x (content organization)
  • author: 2x (authorship)
  • content: 1x (full text)
  • kind: 1x (content type)

Methods 2

is_available
Check if the lunr Python package is available.
0 bool
def is_available(self) -> bool

Check if the lunr Python package is available.

Returns

bool

True if lunr can be imported, False otherwise

generate
Generate pre-built Lunr index from index.json.
1 Path | None
def generate(self, index_json_path: Path | None = None) -> Path | None

Generate pre-built Lunr index from index.json.

Parameters 1
index_json_path Path | None

Path to index.json. If None, uses default location.

Returns

Path | None

Path to the generated search-index.json, or None if generation failed.

Internal Methods 6
__init__
Initialize the Lunr index generator.
1 None
def __init__(self, site: Site) -> None

Initialize the Lunr index generator.

Parameters 1
site Site

Site instance with rendered pages

_build_documents
Build document list for Lunr indexing. Filters out pages marked for exclusion …
1 list[dict[str, Any]]
def _build_documents(self, pages: list[dict[str, Any]]) -> list[dict[str, Any]]

Build document list for Lunr indexing.

Filters out pages marked for exclusion and transforms page data into the format expected by Lunr.

Parameters 1
pages list[dict[str, Any]]

List of page dictionaries from index.json

Returns

list[dict[str, Any]]

List of documents ready for Lunr indexing

_get_author
Extract author string from page data.
1 str
def _get_author(self, page: dict[str, Any]) -> str

Extract author string from page data.

Parameters 1
page dict[str, Any]
Returns

str

_join_list
Join list items into space-separated string.
1 str
def _join_list(self, items: list[str] | None) -> str

Join list items into space-separated string.

Parameters 1
items list[str] | None
Returns

str

_get_index_json_path
Get the path to index.json, handling i18n prefixes.
0 Path
def _get_index_json_path(self) -> Path

Get the path to index.json, handling i18n prefixes.

Returns

Path

_get_output_path
Get the output path for search-index.json, handling i18n prefixes.
0 Path
def _get_output_path(self) -> Path

Get the output path for search-index.json, handling i18n prefixes.

Returns

Path