# netgen/query-translator architecture

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

- Interactive diagram: https://gitdiagram.com/netgen/query-translator
- Repository: https://github.com/netgen/query-translator

## Overview

##### Project type and purpose
- This is an open-source PHP library (“Query Translator”) that takes a user’s search string and translates it into various backend query syntaxes (e.g., Lucene, ExtendedDisMax, native SQL-like, QueryString).
- It provides a pluggable pipeline: tokenization → parsing → abstract syntax tree (AST) → generation to target output.
- Key use cases: normalizing user input, targeting multiple search backends, customizable syntax, error-resilient parsing.

##### File-structure analysis
- Top-level:
- composer.json, autoloaded via PSR-4
- README.md, LICENSE, tests/, phpunit.xml
- lib/Parsing.php, lib/Tokenizing.php, lib/Values (core interfaces, AST definitions)
- lib/Languages/Galach:
- Tokenizer (Tokenizer.php, TokenExtractor/Full, Text)
- Parser (Parser.php)
- AST node classes (Values/Node, Values/Token)
- Generators for each target: Common, Lucene, ExtendedDisMax, QueryString, Native
- Each generator implements Visitor pattern (Visitor.php, Aggregate.php)
- tests/: PHPUnit tests validating each pipeline stage and generator dispatch.

##### Key README insights
- Emphasizes AST-based translator pattern.
- Mentions extension points: change characters, implement own term clauses, customize syntax tree → output.
- Demo repository shows how to wire into a PHP app.

##### Main components
a. Input Layer
- User input string (e.g., “+apple AND banana”)
b. Tokenization
- TokenExtractor (Full/Text) → Tokenizer
c. Parsing
- Parser → produces AST (lib/Values/SyntaxTree.php, Node hierarchy)
d. Generation
- Generator interfaces → concrete Generators (Lucene, ExtendedDisMax, Native, QueryString)
e. Output Layer
- Translated query string sent to search backend (Solr, Elasticsearch, custom DB)
f. Extension points
- Interfaces: TokenExtractorInterface, ParserInterface, GeneratorInterface
- Users can drop in custom implementations

##### Relationships and data flow
- That multiple Generators can run in parallel or be swapped (strategy pattern).

##### Architectural patterns & design principles
- Pipeline architecture: linear stages, clear separation of concerns.
- Visitor pattern: AST traversal for generation.
- Strategy/Factory pattern: generator selection based on target.
- Open/Closed: new syntax rules or generators plugged via interfaces.

##### Technologies and frameworks
- PHP ≥5.6, Composer autoloading (PSR-4).
- PHPUnit for tests.
- No UI or database layers—library only.
- External dependencies: any search backend accepting query string.

##### Step-by-step
– Show that each implements GeneratorInterface.
6. Alongside pipeline, annotate “lib/Parsing.php,” “lib/Tokenizing.php,” “lib/Values” as core.
8. Extension points at TokenExtractor, Parser, Generator layers.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Layers
    subgraph "Input Layer"
        UI["User Input String"] 
    end

    subgraph "Core Engine"
        direction TB
        TI["Tokenizer"]:::core
        PI["Parser"]:::core
        AST["AST (SyntaxTree + Node classes)"]:::core
    end

    subgraph "Galach Language Module"
        direction TB
        TEI["TokenExtractorInterface"]:::interface
        TE["TokenExtractor"]:::module
        FE["Full Extractor"]:::module
        TX["Text Extractor"]:::module
        GT["Tokenizer"]:::module
        GP["Parser"]:::module
    end

    subgraph "Generators"
        direction TB
        GI["GeneratorInterface"]:::interface
        CVS["Common Visitor"]:::gen
        AGG["Common Aggregate"]:::gen
        LUC["Generator: Lucene"]:::gen
        EDX["Generator: ExtendedDisMax"]:::gen
        QSR["Generator: QueryString"]:::gen
        NAT["Generator: Native"]:::gen
    end

    subgraph "External Systems"
        BE["Search Backend (Solr/ES/Custom)"]:::external
    end

    %% Connections
    UI --> TE
    TEI --> TE
    TE --> FE
    TE --> TX
    TE --> GT
    GT --> TI
    TI --> PI
    PI --> AST

    AST --> LUC
    AST --> EDX
    AST --> QSR
    AST --> NAT

    LUC --> BE
    EDX --> BE
    QSR --> BE
    NAT --> BE

    GI --> CVS
    GI --> AGG
    CVS --> LUC
    AGG --> LUC

    %% Click events
    click TE "https://github.com/netgen/query-translator/blob/master/lib/Languages/Galach/TokenExtractor.php"
    click FE "https://github.com/netgen/query-translator/blob/master/lib/Languages/Galach/TokenExtractor/Full.php"
    click TX "https://github.com/netgen/query-translator/blob/master/lib/Languages/Galach/TokenExtractor/Text.php"
    click GT "https://github.com/netgen/query-translator/blob/master/lib/Languages/Galach/Tokenizer.php"
    click GP "https://github.com/netgen/query-translator/blob/master/lib/Languages/Galach/Parser.php"
    click TI "https://github.com/netgen/query-translator/blob/master/lib/Tokenizing.php"
    click PI "https://github.com/netgen/query-translator/blob/master/lib/Parsing.php"
    click AST "https://github.com/netgen/query-translator/blob/master/lib/Values/SyntaxTree.php"
    click CVS "https://github.com/netgen/query-translator/blob/master/lib/Languages/Galach/Generators/Common/Visitor.php"
    click AGG "https://github.com/netgen/query-translator/blob/master/lib/Languages/Galach/Generators/Common/Aggregate.php"
    click LUC "https://github.com/netgen/query-translator/blob/master/lib/Languages/Galach/Generators/Lucene/Common/*.php"
    click EDX "https://github.com/netgen/query-translator/blob/master/lib/Languages/Galach/Generators/ExtendedDisMax.php"
    click QSR "https://github.com/netgen/query-translator/blob/master/lib/Languages/Galach/Generators/QueryString.php"
    click NAT "https://github.com/netgen/query-translator/blob/master/lib/Languages/Galach/Generators/Native.php"

    %% Styles
    classDef core fill:#D0E8FF,stroke:#009,stroke-width:1px
    classDef module fill:#D0F2D8,stroke:#090,stroke-width:1px
    classDef gen fill:#FFE5B4,stroke:#E66,stroke-width:1px
    classDef external fill:#EEEEEE,stroke:#666,stroke-width:1px
    classDef interface stroke-dasharray:5 5,stroke:#000,fill:#FFF
```
