# phonepe/mustang architecture

> Architecture diagram of the GitHub repository phonepe/mustang, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-08). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/phonepe/mustang
- Repository: https://github.com/phonepe/mustang

## Overview

1. **Repository shape**
   - This is a multi-module Maven repository targeting **Java 17+**.
   - The runtime split is clean: a pure domain module, a core engine module, and an optional **Dropwizard** HTTP bundle.
   - There are also **benchmarking** and **docs** modules, which support validation and documentation but are not part of the serving path.

2. **`mustang-models`: shared domain layer**
   - This module defines the engine’s vocabulary and is intended to be reused by both core and API layers.
   - It contains the object model for **criteria**, **predicates**, **compositions** (conjunction/disjunction), **details** (caveats such as equality/range/regex/versioning/set relations), **pre-operations**, **search queries**, **similarity stats**, **ratification types**, and shared exceptions/error codes.
   - It also carries the core inverted-index data types (`InvertedIndex`, posting entries, keys, index group) that the engine manipulates.

3. **Criteria as the central input**
   - The system indexes and evaluates Boolean expressions in two normalized forms: **DNF** and **CNF**.
   - A criteria is built from compositions of predicates, where each predicate is either **included** or **excluded**, and each predicate references a `Detail` that defines its matching semantics.
   - Pre-operations sit in front of detail evaluation and transform attribute values before matching; this is important because it changes the evaluation path without changing the criteria structure.

4. **`mustang-core`: engine and orchestration**
   - `MustangEngine` is the main entry point for programmatic use.
   - The core module separates responsibilities into facades for **indexing**, **searching**, **scanning**, and **ratification**, rather than putting everything directly into the engine class.
   - This module also includes JSON utilities and criteria utilities, showing that it bridges domain objects with runtime evaluation against event payloads.

5. **Indexing pipeline**
   - Indexing is form-aware: there are dedicated builders for **CNF** and **DNF** (`CNFIndexer`, `DNFIndexer`) behind a shared `CriteriaIndexBuilder`.
   - Criteria are converted into inverted-index structures via posting-list extractors, with separate extractors for CNF and DNF.
   - The resulting `IndexGroup`/`InvertedIndex` structures are the engine’s internal retrieval layer, optimized for finding candidate matches quickly rather than brute-force evaluation.

6. **Search pipeline**
   - Search starts from an evaluation context built from incoming data, commonly JSON, and flows into `SearchFacade` and `CriteriaSearchHandler`.
   - Matching is implemented by dedicated matchers for each normal form: **CNFMatcher** and **DNFMatcher**, with a `CaveatEnforcer` handling detail-specific checks.
   - The engine supports both raw matching and **ranked/top-N search**, with scoring semantics defined in the README and implemented over predicate overlap.

7. **Scan and direct evaluation**
   - Separate from indexed search, the core exposes **scan** and **evaluate** paths.
   - Scan is the in-memory path for evaluating a list of criteria directly against a context, useful for smaller batches or validation.
   - This gives the repo two important execution modes: **indexed retrieval** for scale, and **direct evaluation** for inspection or smaller workloads.

8. **Background processing and ratification**
   - Ratification is an asynchronous background capability for anomaly detection or consistency checking over an index.
   - The flow is split into a processor (`AsyncProcessor`), task handler interface, and a concrete ratification request handler.
   - This indicates a non-blocking maintenance subsystem distinct from the synchronous search/index operations.

9. **Similarity and export/import utilities**
   - The core also includes **similarity detection** for comparing candidate criteria against an existing index.
   - Export/import support is present for moving index state in and out of the engine, which is useful for debugging, migration, or operational recovery.
   - These are secondary engine capabilities, but they share the same model and index structures as the main search path.

10. **`mustang-dw-bundle`: optional HTTP boundary**
   - This module wraps the core engine in a **Dropwizard** bundle and exposes REST resources for search, scan, debug, and ratification-related operations.
   - The bundle layer uses request/response DTOs and service interfaces/implementations to keep HTTP concerns separate from engine logic.
   - Architecturally, this is the network boundary: it translates incoming API requests into core-engine operations and returns serialized responses/exceptions.

11. **Operational and ecosystem boundaries**
   - The repo is designed to be embedded as a library or mounted as a service.
   - Maven Central publishing, CI, SonarCloud checks, and docs generation are part of the delivery pipeline, not runtime behavior.
   - The docs and benchmarking modules are important for understanding intended usage and performance characteristics, but they do not alter the core execution architecture.

12. **Key diagram takeaway**
   - The main flow is: **request/context in → core model objects → index/search engine → matching/scoring results out**.
   - Around that, there are two major supporting boundaries: **Dropwizard REST** for external access and **background maintenance** for ratification/similarity/index management.
   - The most important architectural split is between **shared domain model** and **engine/runtime implementation**, with DNF/CNF treated as first-class, parallel execution paths.

Main entry points: [`mustang-core/src/main/java/com/phonepe/mustang/search/SearchFacade.java`](https://github.com/phonepe/mustang/blob/main/mustang-core/src/main/java/com/phonepe/mustang/search/SearchFacade.java) (SearchFacade), [`mustang-core/src/main/java/com/phonepe/mustang/MustangEngine.java`](https://github.com/phonepe/mustang/blob/main/mustang-core/src/main/java/com/phonepe/mustang/MustangEngine.java) (MustangEngine), [`mustang-models/src/main/java/com/phonepe/mustang/criteria`](https://github.com/phonepe/mustang/tree/main/mustang-models/src/main/java/com/phonepe/mustang/criteria) (Criteria), [`mustang-models/src/main/java/com/phonepe/mustang/index`](https://github.com/phonepe/mustang/tree/main/mustang-models/src/main/java/com/phonepe/mustang/index) (Index Types)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_g_models["mustang-models"]
  node_n_models_criteria["Criteria"]
  node_n_models_composition["Composition"]
  node_n_models_predicate["Predicate"]
  node_n_models_detail["Detail<br/>matching semantics"]
  node_n_models_preop["PreOperation<br/>value transform"]
  node_n_models_query["Query<br/>search request"]
  node_n_models_index[("Index Types<br/>inverted index model")]
  node_n_models_result["Results<br/>shared outputs"]
end

subgraph group_g_core["mustang-core"]
  node_n_core_engine["MustangEngine<br/>engine entrypoint<br/>[MustangEngine.java]"]
  node_n_core_index["IndexingFacade<br/>indexing orchestration"]
  node_n_core_search["SearchFacade<br/>search orchestration<br/>[SearchFacade.java]"]
  node_n_core_scan["Scanner<br/>direct evaluation<br/>[Scanner.java]"]
  node_n_core_ratify["Ratifier<br/>maintenance<br/>[Ratifier.java]"]
  node_n_core_async["AsyncProcessor<br/>background processing"]
  node_n_core_cnf["CNF path<br/>cnf execution<br/>[CNFIndexer.java]"]
  node_n_core_dnf["DNF path<br/>dnf execution<br/>[DNFIndexer.java]"]
  node_n_core_caveat["CaveatEnforcer<br/>detail enforcement"]
  node_n_core_similarity["SimilarityDetector<br/>analysis"]
  node_n_core_json["JsonUtils<br/>json bridge<br/>[JsonUtils.java]"]
end

subgraph group_g_bundle["mustang-dw-bundle"]
  node_n_bundle_bundle["MustangBundle<br/>dropwizard bundle<br/>[MustangBundle.java]"]
  node_n_bundle_resources["REST resources<br/>http resources"]
  node_n_bundle_services["Services<br/>service layer"]
  node_n_bundle_dtos["Request/Response<br/>api dto"]
end

subgraph group_g_support["Support"]
  node_n_support_docs["Docs<br/>documentation"]
  node_n_support_bench["Benchmarks<br/>validation"]
end

node_n_models_composition -->|"contains"| node_n_models_predicate
node_n_models_predicate -->|"references"| node_n_models_detail
node_n_models_predicate -->|"prepares"| node_n_models_preop
node_n_models_criteria -->|"built from"| node_n_models_composition
node_n_models_criteria -->|"mapped to"| node_n_models_query
node_n_models_index -->|"stores"| node_n_models_criteria
node_n_models_result -->|"supports"| node_n_models_detail
node_n_core_engine -->|"uses"| node_n_core_index
node_n_core_engine -->|"uses"| node_n_core_search
node_n_core_engine -->|"uses"| node_n_core_scan
node_n_core_engine -->|"uses"| node_n_core_ratify
node_n_core_index -->|"routes"| node_n_core_cnf
node_n_core_index -->|"routes"| node_n_core_dnf
node_n_core_cnf -->|"builds"| node_n_models_index
node_n_core_dnf -->|"builds"| node_n_models_index
node_n_core_search -->|"matches"| node_n_core_cnf
node_n_core_search -->|"matches"| node_n_core_dnf
node_n_core_search -->|"enforces"| node_n_core_caveat
node_n_core_search -->|"parses"| node_n_core_json
node_n_core_scan -->|"evaluates"| node_n_models_criteria
node_n_core_ratify -->|"scheduled by"| node_n_core_async
node_n_core_ratify -->|"checks"| node_n_models_index
node_n_core_similarity -->|"compares against"| node_n_models_index
node_n_bundle_bundle -->|"wraps"| node_n_core_engine
node_n_bundle_resources -->|"calls"| node_n_bundle_services
node_n_bundle_services -->|"invokes"| node_n_core_search
node_n_bundle_services -->|"invokes"| node_n_core_scan
node_n_bundle_services -->|"invokes"| node_n_core_engine
node_n_bundle_dtos -->|"serializes"| node_n_models_query
node_n_bundle_dtos -->|"maps"| node_n_models_result
node_n_support_docs -.->|"documents"| node_n_models_criteria
node_n_support_docs -.->|"documents"| node_n_core_search
node_n_support_bench -.->|"measures"| node_n_core_search

click node_n_models_criteria "https://github.com/phonepe/mustang/tree/main/mustang-models/src/main/java/com/phonepe/mustang/criteria"
click node_n_models_composition "https://github.com/phonepe/mustang/tree/main/mustang-models/src/main/java/com/phonepe/mustang/composition"
click node_n_models_predicate "https://github.com/phonepe/mustang/tree/main/mustang-models/src/main/java/com/phonepe/mustang/predicate"
click node_n_models_detail "https://github.com/phonepe/mustang/tree/main/mustang-models/src/main/java/com/phonepe/mustang/detail"
click node_n_models_preop "https://github.com/phonepe/mustang/tree/main/mustang-models/src/main/java/com/phonepe/mustang/preoperation"
click node_n_models_query "https://github.com/phonepe/mustang/tree/main/mustang-models/src/main/java/com/phonepe/mustang/search"
click node_n_models_index "https://github.com/phonepe/mustang/tree/main/mustang-models/src/main/java/com/phonepe/mustang/index"
click node_n_models_result "https://github.com/phonepe/mustang/tree/main/mustang-models/src/main/java/com/phonepe/mustang"
click node_n_core_engine "https://github.com/phonepe/mustang/blob/main/mustang-core/src/main/java/com/phonepe/mustang/MustangEngine.java"
click node_n_core_index "https://github.com/phonepe/mustang/blob/main/mustang-core/src/main/java/com/phonepe/mustang/index/IndexingFacade.java"
click node_n_core_search "https://github.com/phonepe/mustang/blob/main/mustang-core/src/main/java/com/phonepe/mustang/search/SearchFacade.java"
click node_n_core_scan "https://github.com/phonepe/mustang/blob/main/mustang-core/src/main/java/com/phonepe/mustang/scan/Scanner.java"
click node_n_core_ratify "https://github.com/phonepe/mustang/blob/main/mustang-core/src/main/java/com/phonepe/mustang/ratify/Ratifier.java"
click node_n_core_async "https://github.com/phonepe/mustang/blob/main/mustang-core/src/main/java/com/phonepe/mustang/processor/AsyncProcessor.java"
click node_n_core_cnf "https://github.com/phonepe/mustang/blob/main/mustang-core/src/main/java/com/phonepe/mustang/index/builder/CNFIndexer.java"
click node_n_core_dnf "https://github.com/phonepe/mustang/blob/main/mustang-core/src/main/java/com/phonepe/mustang/index/builder/DNFIndexer.java"
click node_n_core_caveat "https://github.com/phonepe/mustang/blob/main/mustang-core/src/main/java/com/phonepe/mustang/search/matcher/CaveatEnforcer.java"
click node_n_core_similarity "https://github.com/phonepe/mustang/blob/main/mustang-core/src/main/java/com/phonepe/mustang/similarity/SimilarityDetector.java"
click node_n_core_json "https://github.com/phonepe/mustang/blob/main/mustang-core/src/main/java/com/phonepe/mustang/json/JsonUtils.java"
click node_n_bundle_bundle "https://github.com/phonepe/mustang/blob/main/mustang-dw-bundle/src/main/java/com/phonepe/mustang/MustangBundle.java"
click node_n_bundle_resources "https://github.com/phonepe/mustang/tree/main/mustang-dw-bundle/src/main/java/com/phonepe/mustang/resources"
click node_n_bundle_services "https://github.com/phonepe/mustang/tree/main/mustang-dw-bundle/src/main/java/com/phonepe/mustang/service"
click node_n_bundle_dtos "https://github.com/phonepe/mustang/tree/main/mustang-dw-bundle/src/main/java/com/phonepe/mustang/request"
click node_n_support_docs "https://github.com/phonepe/mustang/tree/main/docs/docs"
click node_n_support_bench "https://github.com/phonepe/mustang/tree/main/benchmarking/src/main/java/com/phonepe/mustang/benchmark"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_n_models_criteria,node_n_models_composition,node_n_models_predicate,node_n_models_detail,node_n_models_preop,node_n_models_query,node_n_models_index,node_n_models_result toneBlue
class node_n_core_engine,node_n_core_index,node_n_core_search,node_n_core_scan,node_n_core_ratify,node_n_core_async,node_n_core_cnf,node_n_core_dnf,node_n_core_caveat,node_n_core_similarity,node_n_core_json toneAmber
class node_n_bundle_bundle,node_n_bundle_resources,node_n_bundle_services,node_n_bundle_dtos toneMint
class node_n_support_docs,node_n_support_bench toneRose
```

## Components

### mustang-models

Shared domain model and value objects

- [Criteria](https://github.com/phonepe/mustang/tree/main/mustang-models/src/main/java/com/phonepe/mustang/criteria): domain criteria · Normalized boolean criteria forms and visitors · `mustang-models/src/main/java/com/phonepe/mustang/criteria`
- [Composition](https://github.com/phonepe/mustang/tree/main/mustang-models/src/main/java/com/phonepe/mustang/composition): domain composition · Conjunction/disjunction structure for criteria trees · `mustang-models/src/main/java/com/phonepe/mustang/composition`
- [Predicate](https://github.com/phonepe/mustang/tree/main/mustang-models/src/main/java/com/phonepe/mustang/predicate): domain predicate · Included/excluded predicates used inside criteria · `mustang-models/src/main/java/com/phonepe/mustang/predicate`
- [Detail](https://github.com/phonepe/mustang/tree/main/mustang-models/src/main/java/com/phonepe/mustang/detail): matching semantics · Equality, range, regex, set, versioning, and caveat semantics · `mustang-models/src/main/java/com/phonepe/mustang/detail`
- [PreOperation](https://github.com/phonepe/mustang/tree/main/mustang-models/src/main/java/com/phonepe/mustang/preoperation): value transform · Attribute transforms applied before detail evaluation · `mustang-models/src/main/java/com/phonepe/mustang/preoperation`
- [Query](https://github.com/phonepe/mustang/tree/main/mustang-models/src/main/java/com/phonepe/mustang/search): search request · Search query and ranking inputs · `mustang-models/src/main/java/com/phonepe/mustang/search`
- [Index Types](https://github.com/phonepe/mustang/tree/main/mustang-models/src/main/java/com/phonepe/mustang/index): inverted index model · InvertedIndex, posting entries, keys, and index groups · `mustang-models/src/main/java/com/phonepe/mustang/index`
- [Results](https://github.com/phonepe/mustang/tree/main/mustang-models/src/main/java/com/phonepe/mustang): shared outputs · Similarity stats, ratification types, and exceptions · `mustang-models/src/main/java/com/phonepe/mustang`

### mustang-core

Engine runtime and orchestration

- [MustangEngine](https://github.com/phonepe/mustang/blob/main/mustang-core/src/main/java/com/phonepe/mustang/MustangEngine.java): engine entrypoint · Main programmatic entry point · `mustang-core/src/main/java/com/phonepe/mustang/MustangEngine.java`
- [IndexingFacade](https://github.com/phonepe/mustang/blob/main/mustang-core/src/main/java/com/phonepe/mustang/index/IndexingFacade.java): indexing orchestration · Builds inverted indexes from criteria · `mustang-core/src/main/java/com/phonepe/mustang/index/IndexingFacade.java`
- [SearchFacade](https://github.com/phonepe/mustang/blob/main/mustang-core/src/main/java/com/phonepe/mustang/search/SearchFacade.java): search orchestration · Coordinates search and ranked retrieval · `mustang-core/src/main/java/com/phonepe/mustang/search/SearchFacade.java`
- [Scanner](https://github.com/phonepe/mustang/blob/main/mustang-core/src/main/java/com/phonepe/mustang/scan/Scanner.java): direct evaluation · In-memory scan path for evaluating criteria against context · `mustang-core/src/main/java/com/phonepe/mustang/scan/Scanner.java`
- [Ratifier](https://github.com/phonepe/mustang/blob/main/mustang-core/src/main/java/com/phonepe/mustang/ratify/Ratifier.java): maintenance · Ratification and anomaly checking over indexes · `mustang-core/src/main/java/com/phonepe/mustang/ratify/Ratifier.java`
- [AsyncProcessor](https://github.com/phonepe/mustang/blob/main/mustang-core/src/main/java/com/phonepe/mustang/processor/AsyncProcessor.java): background processing · Runs ratification tasks asynchronously · `mustang-core/src/main/java/com/phonepe/mustang/processor/AsyncProcessor.java`
- [CNF path](https://github.com/phonepe/mustang/blob/main/mustang-core/src/main/java/com/phonepe/mustang/index/builder/CNFIndexer.java): cnf execution · CNF indexer and matcher pipeline · `mustang-core/src/main/java/com/phonepe/mustang/index/builder/CNFIndexer.java`
- [DNF path](https://github.com/phonepe/mustang/blob/main/mustang-core/src/main/java/com/phonepe/mustang/index/builder/DNFIndexer.java): dnf execution · DNF indexer and matcher pipeline · `mustang-core/src/main/java/com/phonepe/mustang/index/builder/DNFIndexer.java`
- [CaveatEnforcer](https://github.com/phonepe/mustang/blob/main/mustang-core/src/main/java/com/phonepe/mustang/search/matcher/CaveatEnforcer.java): detail enforcement · Applies detail-specific checks during matching · `mustang-core/src/main/java/com/phonepe/mustang/search/matcher/CaveatEnforcer.java`
- [SimilarityDetector](https://github.com/phonepe/mustang/blob/main/mustang-core/src/main/java/com/phonepe/mustang/similarity/SimilarityDetector.java): analysis · Compares candidate criteria against existing index · `mustang-core/src/main/java/com/phonepe/mustang/similarity/SimilarityDetector.java`
- [JsonUtils](https://github.com/phonepe/mustang/blob/main/mustang-core/src/main/java/com/phonepe/mustang/json/JsonUtils.java): json bridge · JSON helpers for contexts and runtime bridging · `mustang-core/src/main/java/com/phonepe/mustang/json/JsonUtils.java`

### mustang-dw-bundle

Optional Dropwizard HTTP boundary

- [MustangBundle](https://github.com/phonepe/mustang/blob/main/mustang-dw-bundle/src/main/java/com/phonepe/mustang/MustangBundle.java): dropwizard bundle · Wires the HTTP module to the core engine · `mustang-dw-bundle/src/main/java/com/phonepe/mustang/MustangBundle.java`
- [REST resources](https://github.com/phonepe/mustang/tree/main/mustang-dw-bundle/src/main/java/com/phonepe/mustang/resources): http resources · Search, scan, and debug endpoints · `mustang-dw-bundle/src/main/java/com/phonepe/mustang/resources`
- [Services](https://github.com/phonepe/mustang/tree/main/mustang-dw-bundle/src/main/java/com/phonepe/mustang/service): service layer · HTTP-facing service interfaces and implementations · `mustang-dw-bundle/src/main/java/com/phonepe/mustang/service`
- [Request/Response](https://github.com/phonepe/mustang/tree/main/mustang-dw-bundle/src/main/java/com/phonepe/mustang/request): api dto · HTTP request objects, responses, and exception mapping · `mustang-dw-bundle/src/main/java/com/phonepe/mustang/request`

### Support

Docs and benchmarking modules

- [Docs](https://github.com/phonepe/mustang/tree/main/docs/docs): documentation · Algorithm, concepts, and usage documentation · `docs/docs`
- [Benchmarks](https://github.com/phonepe/mustang/tree/main/benchmarking/src/main/java/com/phonepe/mustang/benchmark): validation · JMH-style search benchmarks and utilities · `benchmarking/src/main/java/com/phonepe/mustang/benchmark`

## Connections

- Composition → Predicate: contains · Criteria are composed of predicates
- Predicate → Detail: references · Each predicate delegates matching semantics to a detail
- Predicate → PreOperation: prepares · Pre-operations transform values before detail checks
- Criteria → Composition: built from · Criteria are normalized boolean expression trees
- Criteria → Query: mapped to · Search queries wrap criteria and ranking intent
- Index Types → Criteria: stores · Index structures encode criteria for retrieval
- Results → Detail: supports · Shared outputs include exceptions and stats around matching
- MustangEngine → IndexingFacade: uses · Engine delegates indexing to the indexing facade
- MustangEngine → SearchFacade: uses · Engine delegates search to the search facade
- MustangEngine → Scanner: uses · Engine exposes direct scan/evaluate paths
- MustangEngine → Ratifier: uses · Engine exposes ratification and maintenance operations
- IndexingFacade → CNF path: routes · Indexing splits by criteria normal form
- IndexingFacade → DNF path: routes · Indexing splits by criteria normal form
- CNF path → Index Types: builds · CNF indexing produces inverted-index structures
- DNF path → Index Types: builds · DNF indexing produces inverted-index structures
- SearchFacade → CNF path: matches · CNF search path uses CNF matchers
- SearchFacade → DNF path: matches · DNF search path uses DNF matchers
- SearchFacade → CaveatEnforcer: enforces · Search applies detail-specific caveats during evaluation
- SearchFacade → JsonUtils: parses · Search contexts often arrive as JSON payloads
- Scanner → Criteria: evaluates · Scan directly evaluates criteria against a context
- Ratifier → AsyncProcessor: scheduled by · Ratification can run asynchronously in the background
- Ratifier → Index Types: checks · Ratification operates over index state
- SimilarityDetector → Index Types: compares against · Similarity detection works on existing index state
- MustangBundle → MustangEngine: wraps · Dropwizard bundle wires HTTP access to the engine
- REST resources → Services: calls · Resources delegate to service layer
- Services → SearchFacade: invokes · Search service delegates to core search
- Services → Scanner: invokes · Scan service delegates to core scan
- Services → MustangEngine: invokes · Debug and management services reach into the engine
- Request/Response → Query: serializes · HTTP requests carry query and criteria data
- Request/Response → Results: maps · HTTP responses surface shared result and error types
- Docs → Criteria: documents · Docs explain criteria, predicates, and pre-operations
- Docs → SearchFacade: documents · Docs explain search and scoring behavior
- Benchmarks → SearchFacade: measures · Benchmarks compare search performance
