AI Search Indexing
This page describes how wallet-ai-service's indexer builds the search index, which files are parsed, how content is cleaned, and a separate mechanism specific to the AI Assistant: how the OpenAPI spec used for example request generation gets into Azure Search at all.
Indexing logic lives entirely in wallet-ai-service's src/app/shared/indexer/ β not in this repo. There is no uploadToAzureSearch.js or equivalent script here anymore.
The index the UI queriesβ
- Index:
wallet-docs-vector-index-v1β a hybrid (keyword + vector-embedding) index. It is the only index the UI queries:VECTOR_INDEX_NAMEinsrc/components/ChatBot/walletAIService.jsis sent asindex_nameon every chat request. - Feeding endpoint:
POST /api/index/vector(see Re-indexing). Use this endpoint, not/api/index, to refresh what the Assistant retrieves.
Overviewβ
Indexing runs on the wallet-ai-service backend (see Re-indexing) β admin-key gated, and expected to be IP-allowlisted at the ingress layer (the endpoint itself does not enforce caller identity beyond the admin key). It accepts three source types plus a fourth catch-all:
| Source | Parser | Handles |
|---|---|---|
docs | markdown.py | .md / .mdx files |
yaml | yaml.py | .yaml / .yml files β generic YAML docs and OpenAPI/Swagger specs |
json | json.py | .json files |
documents (inline) | pipeline.py::index_inline_documents | Pre-formatted document dicts, uploaded as-is β no parsing, no truncation |
Markdown parsing (markdown.py)β
Per file, walked recursively under the configured docs root:
- Strip front matter, extract the body.
- Extract title β first
# H1heading; if none, fall back to front-mattertitle:; if neither, fall back to the filename stem. (Files without an H1 used to be silently dropped entirely β fixed; the fallback chain now matches what the code always claimed to do.) - Strip remaining markdown syntax (code fences, links, headings, HTML tags, MDX imports) to produce plain searchable text, truncated to 32,000 chars (Azure Search field limit).
- Derive the URL β front-matter
slug:if present (prefixed/docs), otherwise the file's path relative to the docs root with numeric prefixes (01-,03-) stripped and segments lowercased. - Skip changelog pages β matched by title or URL pattern, deliberately excluded as noise.
- Deduplicate by URL β last-write-wins, useful when versioned docs overlap.
Noise exclusionβ
Discovery (safe_discover_files, shared by all three parsers) skips, before any of the above ever runs:
- Any path segment starting with
_β covers_category_.json,_drafts/. - Any path segment starting with
.β covers.docusaurus. node_modules,.git,build,static.
YAML parsing (yaml.py)β
Handles two distinct shapes:
Generic YAML docs β a single mapping or a sequence of mappings, each needing at least a content field (same fallback/derivation rules as JSON, below).
OpenAPI/Swagger specs β detected by a top-level openapi/swagger key. This repo's specs split every request/response schema across many small files under schemas/, parameters/, servers/, linked by $ref. Two things matter here:
A file is only a root spec if it has pathsβ
Component/schema fragment files (e.g. schemas/sessions/session-resource.yaml) also declare a top-level openapi: key β for standalone linting β but have no paths. Without checking for that, every such fragment file was previously indexed as its own near-empty, noisy document (literally just the string "v2" or "3.1.1"). Fragment files are now skipped β their content is already pulled into the root spec's own indexed text via $ref resolution (below), so indexing them separately would only add noise.
$ref resolution β bounded, cycle-safe, contained to the docs rootβ
Without resolving $refs, an endpoint's indexed text was just "POST /v2/sessions β Create Session β <top-level description>" β the actual field-level descriptions living in referenced schema files were invisible to search. Resolution:
- Bounded depth (4 levels) β a broken or deeply nested
$refchain degrades gracefully rather than recursing forever. - Cycle-safe without over-blocking siblings β the "already resolved" tracking set is branched per recursion path (
seen | {ref}), not mutated in place. Two sibling properties that happen to reference the same component (reuse, not a cycle) both resolve correctly instead of the second one being incorrectly treated as a cycle. - Contained to the docs root β a
$reflike../../../../etc/passwdis rejected (the sameos.path.normpath(os.path.realpath(...))+startswithcontainment check used elsewhere for path safety). Verified directly: a spec with a$refpointing outside the configured docs root gets that reference silently dropped, not read.
JSON parsing (json.py)β
A single object or array of objects, each needing a content field (or a best-effort fallback: join whatever string values exist if content is absent). id/title/file_path/category all have the same fallback behavior as YAML β derived from the file path/name if not explicitly present.
Index schemaβ
| Field | Type | Notes |
|---|---|---|
id | Edm.String (key) | SHA-1 of the URL/file path β stable across re-runs, enables upsert |
title | Edm.String | Searchable |
content | Edm.String | Searchable β truncated to 32,000 chars |
file_path | Edm.String | Filterable β used as the citation link |
category | Edm.String | Filterable, facetable β drives the two-pass search filter |
rawContent | Edm.String | Retrievable only β no searchable/filterable/sortable/facetable. See below. |
Why rawContent existsβ
filterable/sortable/facetable force Azure Search to index a field's entire value as a single term, capped at 32KB. The full bundled OpenAPI spec (~300KB) is far past that β uploading it into content fails outright with "Field 'content' contains a term that is too large to process." rawContent has none of those attributes, so it has no such limit. It exists for exactly one purpose today: storing that spec blob.
The OpenAPI spec documentβ
Example request generation needs the actual, structured OpenAPI schema β not the human-readable text the indexer above produces for search. This is a separate mechanism from everything above:
- The full bundled v2 spec (
build/redocusaurus/plugin-redoc-1.yaml) is uploaded as one document, under a fixed key, with the raw YAML text inrawContent:{"@search.action": "upload","id": "ccg-openapi-v2-spec","title": "CCG v2 OpenAPI Spec (bundled)","category": "openapi-spec-raw","rawContent": "<the full plugin-redoc-1.yaml text>"} - At startup, the backend fetches it by direct key lookup (
SearchClient.get_documentβGET /indexes/{index}/docs/{key}, not a ranked search) usingCCG_OPENAPI_V2_SPEC_DOC_ID. - The generator (
OpenAPIExampleGenerator.from_yaml_text()) parses it once and keeps it in memory for the process lifetime.
This must be re-run whenever the v2 spec changes β there's no automatic sync between yarn build's bundled output and this Azure Search document. If example generation starts producing stale field names/enums after an API change, this is the document to re-upload.
A local file path (CCG_OPENAPI_V2_SPEC_PATH) is also supported as a fallback for local dev, when the backend happens to have this repo's build/redocusaurus/ output on disk β not viable in a real deployment, where the two services don't share a filesystem.
Retrieval at query timeβ
Indexing builds the corpus; retrieval (AzureAISearchLangChainRetriever, used by the Assistant's chain) queries it with a two-pass search for API-shaped questions β spec-filtered pass + full-corpus pass, merged and deduplicated. See Overview β Retrieval.
Re-indexingβ
curl -X POST https://<ai-service-host>/api/index/vector \
--max-time 600 \
-H "Content-Type: application/json" \
-H "X-Admin-Key: <admin key>" \
-d '{
"index_name": "wallet-docs-vector-index-v1",
"create_index": true,
"index_data": {
"docs": [{ "directory": "docs", "category": "docs" }],
"yaml": [{ "directory": "openapi/v2/apispec.yaml", "category": "open-api-spec-v2" }],
"json": [{ "directory": "path/to/error-codes", "category": "error-codes" }]
}
}'
Requires the backend's admin key (AZURE_SEARCH_ADMIN_KEY) β returns 503 if unset. overwrite: true clears the target index first; create_index: true creates it if missing.
How this endpoint differs from a plain per-file index:
- One row per chunk β large docs are split (heading-aware for markdown, per-operation for OpenAPI, per-object for JSON).
- Embeddings β every chunk's
contentis embedded (UAIS embeddings gateway) before upload, so it is slower and costs money per call.
--max-time 600 (or your client's equivalent) matters: a full-corpus request can legitimately take several minutes, and most HTTP clients' default timeout (often 120s) will cut the connection before the server finishes β a client-side timeout, not a sign anything is stuck. Splitting one big request into one call per directory/category avoids depending on a big timeout.
There is currently no automatic re-index on merge β no CI step calls the endpoint after docs change on main, so re-indexing is a manual step after meaningful documentation changes.
To target an index for a single chat request, index_name is a top-level field on the /api/ccg/chat(/stream) body, not nested under anything. Unrecognized fields are silently ignored, so a misplaced index_name falls back to the server default with no error.