# lucaong/minisearch architecture

> Architecture diagram of the GitHub repository lucaong/minisearch, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-01-11). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/lucaong/minisearch
- Repository: https://github.com/lucaong/minisearch

## Overview

##### Main Components

a. Core Search Engine Components:
- MiniSearch Class (Main Engine)
- SearchableMap Class (Index Structure)
- Document Processor
- Query Processor
- Search Results Handler

b. Supporting Components:
- Tokenization System
- Term Processing Pipeline
- Auto-suggestion Engine
- Index Storage
- Search Options Handler

##### Component Relationships and Data Flow

**Document Indexing Flow:**
**Document → Document Processor → Tokenization → Term Processing → SearchableMap (Index)**

**Search Query Flow:**
**Query → Query Processor → Tokenization → Term Processing → Search Engine → Results Handler → Ranked Results**

**Auto-suggestion Flow:**
**Partial Query → Query Processor → Auto-suggestion Engine → Ranked Suggestions**

##### Specific Implementation Details

- Blue rectangles for core classes (MiniSearch, SearchableMap)
- Green rectangles for processing components (Tokenizer, Term Processor)
- Yellow diamonds for decision points (Filter, Boost operations)
- Orange ovals for data stores (Index)
- Purple rectangles for utility functions (Ranking, Fuzzy matching)

##### Key Features to Highlight

- Prefix Search capability
- Fuzzy Search matching
- Field Boosting
- Filtering mechanism
- Auto-suggestion system

##### Technical Implementation Notes

Annotations showing:
- TypeScript implementation
- Zero dependencies
- Memory efficiency features
- Browser/Node.js compatibility

##### Architectural Patterns

Emphasize:
- Pipeline pattern for document processing
- Observer pattern for index updates
- Strategy pattern for search options
- Factory pattern for default implementations

##### Component Details

MiniSearch (Core):
**┌─────────────────────────┐**
- MiniSearch        │
- ┤
- Configuration         │
- Search Options        │
- Document Management   │
- Search Implementation │
- ┘

SearchableMap (Index):
**┌─────────────────────────┐**
- SearchableMap       │
- ┤
- Tree Structure        │
- Term Storage          │
- Prefix Lookup         │
- Fuzzy Match Support   │
- ┘

##### Integration Points

Interfaces for:
- Document input
- Query input
- Search results output
- Auto-suggestions output

## Diagram (Mermaid)

```mermaid
graph TB
    %% Main Components
    subgraph Core["Core Search Engine"]
        MiniSearch["MiniSearch Class"]:::core
        SearchableMap["SearchableMap"]:::core
        TreeIterator["Tree Iterator"]:::core
        FuzzySearch["Fuzzy Search"]:::core
        TypeDefs["Type Definitions"]:::core
    end

    subgraph Features["Search Features"]
        ExactMatch["Exact Match"]:::feature
        PrefixSearch["Prefix Search"]:::feature
        AutoSuggest["Auto-suggestion"]:::feature
        ResultsRank["Results Ranking"]:::feature
        SearchFilter["Search Filtering"]:::feature
    end

    subgraph Processing["Document Processing Pipeline"]
        FieldExtract["Field Extraction"]:::process
        TokenSystem["Tokenization System"]:::process
        TermProcess["Term Processing"]:::process
        IndexBuild["Index Building"]:::process
    end

    subgraph SearchPipe["Search Processing Pipeline"]
        QueryToken["Query Tokenization"]:::process
        SearchStrat["Search Strategy"]:::process
        ResultFilter["Results Filtering"]:::process
        ResultRank["Results Ranking"]:::process
        ResultAssembly["Results Assembly"]:::process
    end

    subgraph Storage["Storage & Management"]
        IndexStorage["Index Storage"]:::storage
        MemoryMgmt["Memory Management"]:::storage
        AutoVacuum["Auto-vacuum System"]:::storage
    end

    subgraph Documentation["Documentation & Interfaces"]
        DesignDoc["Design Documentation"]:::doc
        APIDoc["API Documentation"]:::doc
        SearchOpts["Search Options Interface"]:::doc
        QueryInterface["Query Interface"]:::doc
        ResultsInterface["Search Results Interface"]:::doc
        VacuumOpts["Auto-vacuum Options"]:::doc
    end

    %% Relationships
    MiniSearch --> SearchableMap
    SearchableMap --> TreeIterator
    SearchableMap --> FuzzySearch
    SearchableMap --> TypeDefs

    FieldExtract --> TokenSystem
    TokenSystem --> TermProcess
    TermProcess --> IndexBuild
    IndexBuild --> IndexStorage

    QueryToken --> SearchStrat
    SearchStrat --> ResultFilter
    ResultFilter --> ResultRank
    ResultRank --> ResultAssembly

    ExactMatch --> SearchStrat
    PrefixSearch --> SearchStrat
    FuzzySearch --> SearchStrat
    AutoSuggest --> ResultAssembly
    ResultsRank --> ResultRank
    SearchFilter --> ResultFilter

    IndexStorage --> MemoryMgmt
    MemoryMgmt --> AutoVacuum

    %% Click Events
    click MiniSearch "https://github.com/lucaong/minisearch/blob/master/src/MiniSearch.ts"
    click SearchableMap "https://github.com/lucaong/minisearch/blob/master/src/SearchableMap/SearchableMap.ts"
    click TreeIterator "https://github.com/lucaong/minisearch/blob/master/src/SearchableMap/TreeIterator.ts"
    click FuzzySearch "https://github.com/lucaong/minisearch/blob/master/src/SearchableMap/fuzzySearch.ts"
    click TypeDefs "https://github.com/lucaong/minisearch/blob/master/src/SearchableMap/types.ts"
    click AutoSuggest "https://github.com/lucaong/minisearch/blob/master/benchmarks/autoSuggestion.js"
    click ExactMatch "https://github.com/lucaong/minisearch/blob/master/benchmarks/exactSearch.js"
    click PrefixSearch "https://github.com/lucaong/minisearch/blob/master/benchmarks/prefixSearch.js"
    click ResultsRank "https://github.com/lucaong/minisearch/blob/master/benchmarks/ranking.js"
    click SearchFilter "https://github.com/lucaong/minisearch/blob/master/benchmarks/searchFiltering.js"
    click IndexBuild "https://github.com/lucaong/minisearch/blob/master/benchmarks/indexing.js"
    click MemoryMgmt "https://github.com/lucaong/minisearch/blob/master/benchmarks/memory.js"
    click DesignDoc "https://github.com/lucaong/minisearch/blob/master/DESIGN_DOCUMENT.md"
    click APIDoc "https://github.com/lucaong/minisearch/blob/master/docs/classes/MiniSearch.MiniSearch.html"
    click SearchOpts "https://github.com/lucaong/minisearch/blob/master/docs/types/MiniSearch.SearchOptions.html"
    click QueryInterface "https://github.com/lucaong/minisearch/blob/master/docs/types/MiniSearch.Query.html"
    click ResultsInterface "https://github.com/lucaong/minisearch/blob/master/docs/types/MiniSearch.SearchResult.html"
    click VacuumOpts "https://github.com/lucaong/minisearch/blob/master/docs/types/MiniSearch.AutoVacuumOptions.html"

    %% Styles
    classDef core fill:#2196F3,stroke:#1565C0,color:white
    classDef feature fill:#4CAF50,stroke:#2E7D32,color:white
    classDef process fill:#FFC107,stroke:#FFA000,color:black
    classDef storage fill:#9C27B0,stroke:#7B1FA2,color:white
    classDef doc fill:#FF5722,stroke:#E64A19,color:white

    %% Legend
    subgraph Legend
        CoreComp["Core Component"]:::core
        FeatureComp["Search Feature"]:::feature
        ProcessComp["Processing Component"]:::process
        StorageComp["Storage Component"]:::storage
        DocComp["Documentation"]:::doc
    end
```
