---
title: "Search Query Regex Compilation"
description: "The execution flow from createFromSource down to buildRegexFromQuery represents the core lifecycle of a search request in Fumadocs, transitioning from document loader outputs to indexed search serv..."
last_updated: "2026-10-12T07:53:59.756147+00:00"
canonical_url: "https://www.doc0.dev/docs/e8872a72-1909-479c-b585-dc74c6e26726/technical/how-it-works/search-query-regex-compilation"
---

<details>
<summary>Relevant source files</summary>

The following files were used as context for generating this wiki page:

- [packages/core/src/search/orama/create-server.ts](https://github.com/blade47/fumadocs/blob/a9f988897e7dc4ef3d62b2865d80da2f5607701e/packages/core/src/search/orama/create-server.ts)
- [packages/core/src/search/orama/search/simple.ts](https://github.com/blade47/fumadocs/blob/a9f988897e7dc4ef3d62b2865d80da2f5607701e/packages/core/src/search/orama/search/simple.ts)
- [packages/core/src/search/index.ts](https://github.com/blade47/fumadocs/blob/a9f988897e7dc4ef3d62b2865d80da2f5607701e/packages/core/src/search/index.ts)
</details>

## Overview

### Introduction

The execution flow from `createFromSource` down to `buildRegexFromQuery` represents the core lifecycle of a search request in Fumadocs, transitioning from document loader outputs to indexed search servers, execution against an Orama database instance, and finally result post-processing with dynamic search term highlighting.

```mermaid
sequenceDiagram
    participant Source as create-server.ts
    participant Simple as simple.ts
    participant Index as index.ts

    Source->>Source: createFromSource()
    Source->>Source: getCurrentServer()
    Source->>Source: initServer()
    Source->>Source: createI18nSearchAPI() / initAdvancedSearch()
    Source->>Source: initSearchServers()
    Source->>Simple: initSimpleSearch() / searchSimple()
    Simple->>Index: createContentHighlighter()
    Index->>Index: buildRegexFromQuery()
    Index-->>Simple: RegExp | null
    Simple-->>Source: SortedResult[]
```

Sources: [packages/core/src/search/orama/create-server.ts:273-324](https://github.com/blade47/fumadocs/blob/a9f988897e7dc4ef3d62b2865d80da2f5607701e/packages/core/src/search/orama/create-server.ts#L273-L324), [packages/core/src/search/orama/search/simple.ts:5-27](https://github.com/blade47/fumadocs/blob/a9f988897e7dc4ef3d62b2865d80da2f5607701e/packages/core/src/search/orama/search/simple.ts#L5-L27), [packages/core/src/search/index.ts:36-43](https://github.com/blade47/fumadocs/blob/a9f988897e7dc4ef3d62b2865d80da2f5607701e/packages/core/src/search/index.ts#L36-L43)

```mermaid
flowchart TD
    A[createFromSource] --> B[getCurrentServer]
    B --> C[initServer]
    C --> D{Has i18n?}
    D -- Yes --> E[createI18nSearchAPI]
    D -- No --> F[initAdvancedSearch]
    E --> G[initSearchServers]
    G --> H[initSimpleSearch]
    H --> I[searchSimple]
    I --> J[createContentHighlighter]
    J --> K[buildRegexFromQuery]
```

Sources: [packages/core/src/search/orama/create-server.ts:187-324](https://github.com/blade47/fumadocs/blob/a9f988897e7dc4ef3d62b2865d80da2f5607701e/packages/core/src/search/orama/create-server.ts#L187-L324), [packages/core/src/search/orama/search/simple.ts:5-19](https://github.com/blade47/fumadocs/blob/a9f988897e7dc4ef3d62b2865d80da2f5607701e/packages/core/src/search/orama/search/simple.ts#L5-L19), [packages/core/src/search/index.ts:36-48](https://github.com/blade47/fumadocs/blob/a9f988897e7dc4ef3d62b2865d80da2f5607701e/packages/core/src/search/index.ts#L36-L48)

### Step 1: createFromSource

`createFromSource` initiates the search server configuration using a document loader or an asynchronous loader function. It sets up a WeakMap-based cache linked to specific loader outputs to optimize server initialization and handle re-indexing when loader instances change.

Sources: [packages/core/src/search/orama/create-server.ts:273-279](https://github.com/blade47/fumadocs/blob/a9f988897e7dc4ef3d62b2865d80da2f5607701e/packages/core/src/search/orama/create-server.ts#L273-L279)

### Step 2: getCurrentServer

`getCurrentServer` resolves the current loader output—evaluating it if passed as a function—and checks the internal cache for an existing server promise. If a cached server instance does not exist for the current loader reference, it triggers server initialization and stores the resulting promise in the cache.

Sources: [packages/core/src/search/orama/create-server.ts:306-314](https://github.com/blade47/fumadocs/blob/a9f988897e7dc4ef3d62b2865d80da2f5607701e/packages/core/src/search/orama/create-server.ts#L306-L314)

### Step 3: initServer

`initServer` processes all pages retrieved from the loader. It maps each page through `buildIndex` to create searchable index records, generates missing breadcrumbs automatically, assigns locale metadata, and inspects whether internationalization (`_i18n`) is configured.

Sources: [packages/core/src/search/orama/create-server.ts:280-304](https://github.com/blade47/fumadocs/blob/a9f988897e7dc4ef3d62b2865d80da2f5607701e/packages/core/src/search/orama/create-server.ts#L280-L304)

### Step 4: createI18nSearchAPI

When internationalization is enabled (`loader._i18n`), `createI18nSearchAPI` wraps search operations across multiple language contexts. It initiates background server setup per language via `initSearchServers` and routes queries to the appropriate language handler based on user options or defaults.

Sources: [packages/core/src/search/orama/create-server.ts:178-259](https://github.com/blade47/fumadocs/blob/a9f988897e7dc4ef3d62b2865d80da2f5607701e/packages/core/src/search/orama/create-server.ts#L178-L259)

### Step 5: initSearchServers

`initSearchServers` iterates through all configured languages in the internationalization settings, filters document indexes matching the current locale, maps locale codes to compatible stemmers or tokenizers, and instantiates language-specific search servers.

Sources: [packages/core/src/search/orama/create-server.ts:190-237](https://github.com/blade47/fumadocs/blob/a9f988897e7dc4ef3d62b2865d80da2f5607701e/packages/core/src/search/orama/create-server.ts#L190-L237)

### Step 6: initSimpleSearch

`initSimpleSearch` constructs a simple search server instance over an Orama database (`createDBSimple`). It exposes export capabilities for static bundle generation and a `search` method that queries the underlying database instance using `searchSimple`.

Sources: [packages/core/src/search/orama/create-server.ts:78-98](https://github.com/blade47/fumadocs/blob/a9f988897e7dc4ef3d62b2865d80da2f5607701e/packages/core/src/search/orama/create-server.ts#L78-L98)

### Step 7: searchSimple

`searchSimple` executes the search query against the Orama database with tolerance and term boosting configured. Upon receiving search hits, it invokes `createContentHighlighter` to generate highlighted content strings for presentation.

Sources: [packages/core/src/search/orama/search/simple.ts:5-27](https://github.com/blade47/fumadocs/blob/a9f988897e7dc4ef3d62b2865d80da2f5607701e/packages/core/src/search/orama/search/simple.ts#L5-L27)

### Step 8: createContentHighlighter

`createContentHighlighter` accepts a query string or precompiled regular expression. If given a string, it hands the query to `buildRegexFromQuery` to generate a case-insensitive matching pattern, returning helper methods for text and Markdown highlighting.

Sources: [packages/core/src/search/index.ts:47-49](https://github.com/blade47/fumadocs/blob/a9f988897e7dc4ef3d62b2865d80da2f5607701e/packages/core/src/search/index.ts#L47-L49)

### Step 9: buildRegexFromQuery

`buildRegexFromQuery` cleans and splits the query string into distinct search terms, escapes special regular expression characters via `escapeRegExp`, deduplicates terms, and builds a combined case-insensitive `RegExp` matching group for highlighting matches. If the query trims down to an empty string, it returns `null`.

Sources: [packages/core/src/search/index.ts:36-43](https://github.com/blade47/fumadocs/blob/a9f988897e7dc4ef3d62b2865d80da2f5607701e/packages/core/src/search/index.ts#L36-L43)

## Key Observations

- **Cross-Module Boundaries:** The flow bridges higher-level content source loading (`@/source/loader`) with Orama indexing infrastructure (`@orama/orama`), routing through server endpoint wrappers and text processing utilities in `@/search`.
- **Caching Strategy:** Server instances are cached using a `WeakMap` mapped to loader output references. This ensures memory efficiency while preventing redundant re-indexing unless a fresh loader instance is produced.
- **Robust Query Normalization:** Query strings undergo trimming, whitespace splitting, deduplication, and regex escaping inside `buildRegexFromQuery` to safely handle arbitrary user inputs without crashing parser or highlighter steps.

## Sitemap

See the full [sitemap](https://www.doc0.dev/docs/e8872a72-1909-479c-b585-dc74c6e26726/llms.txt) for all pages in this wiki.
