Community程式設計與開發github.com

Zoha/agent-skills NestJS Elasticsearch 整合指南

在 NestJS GraphQL + MongoDB 應用程式中加入或維護 Elasticsearch 全文搜尋功能,包括客戶端與模組配線、資料同步與重建索引、欄位映射與搜尋投影、ID 查詢搭配 MongoDB 還原、游標分頁、跨實體衍伸欄位、透過可設定後端接縫讓搜尋成為選項,以及使用模擬接縫或真實測試叢集進行測試。

agent-skills NestJS Elasticsearch 整合指南 是什麼?

本技能提供一套完整的架構指引,幫助開發者在 NestJS GraphQL + MongoDB 應用程式中整合 Elasticsearch 全文搜尋。其核心架構遵循「Elasticsearch 負責查找,MongoDB 負責提供資料」的合約,Elasticsearch 僅作為純搜尋索引傳回 ID,而實際實體資料則從 MongoDB 還原。技能涵蓋從基礎架構配置、索引定義與同步、查詢與游標分頁、跨實體衍生欄位,到透過設定切換搜尋後端(可替換為 MongoDB 回退或真實叢集)以及使用模擬或真實測試叢集進行測試的完整生命週期。支援平台包括 NestJS、Elasticsearch、MongoDB、Redis 等。技能透過優先級分類的規則來指導開發者,從架構(關鍵)、索引與同步(關鍵)、綱要與映射(高)、查詢(高)、跨實體(中高)、操作與開發體驗(中)、設定與環境(高)到測試(中高),每個規則都附有詳細說明與程式碼範例,確保搜尋功能的高效性、可維護性與可測試性。

相容平台~Claude Code~Codex CLI✓Cursor
npx skills add https://github.com/Zoha/agent-skills/tree/HEAD/skills/nestjs-elasticsearch

Installed? Explore more 程式設計與開發 skills: steipete/bluebubbles, steipete/eightctl, steipete/blucli · View all 6 →

在你喜歡的 AI 中提問

開啟一個已預先載入此 Agent Skill 的新對話。

說明文件

NestJS Elasticsearch 整合指南

將 Elasticsearch 支援的搜尋功能加入 NestJS GraphQL + MongoDB 應用程式的整合指南。它明確定義架構邊界(通用基礎設施、共用搜尋層、每個功能擁有自己的索引),然後涵蓋完整的生命週期:搜尋投影、索引與同步、僅 ID 查詢搭配 MongoDB 還原、游標分頁、跨實體衍生欄位、透過可設定後端接縫讓搜尋成為選項、無需即時叢集的測試,以及相關的操作與開發體驗。

主要的合約是 Elasticsearch 負責查找,MongoDB 負責提供資料:Elasticsearch 是純粹的搜尋索引,僅回傳識別碼,而要求的實體永遠從 MongoDB (資料來源) 還原。

適用時機

當下列情況時參考這些準則:

  • 為現有的 NestJS GraphQL + MongoDB 應用程式加入搜尋功能
  • 配線 @nestjs/elasticsearch 客戶端與共用搜尋層
  • 定義或更改索引映射或搜尋投影
  • 實作 MongoDB 到 Elasticsearch 的同步、重建索引或索引遷移
  • 編寫搜尋查詢、游標分頁或結果還原
  • 讓衍伸值(計數、評分)可在不同實體間搜尋
  • 從設定中讓搜尋成為選項,或無需叢集即可切換後端
  • 使用模擬後端接縫或真實的短暫測試叢集進行測試
  • 偵錯過時的搜尋結果或索引風暴

依優先順序分類的規則

優先級類別影響前綴
1架構關鍵arch-
2索引與同步關鍵sync-
3綱要與映射高map-
4查詢高query-
5跨實體中高rel-
6操作與開發體驗中ops-
7設定與環境高cfg-
8測試中高test-

快速參考

1. 架構 (關鍵)

  • arch-base-search-repository - 將共用索引與查詢邏輯放在基礎儲存庫中
  • arch-entity-vs-search-document - 保持 Elasticsearch 欄位不出現在實體中
  • arch-feature-owned-index - 讓每個功能擁有並註冊自己的索引
  • arch-infra-module - 將 Elasticsearch 客戶端隔離在通用基礎設施模組中

2. 索引與同步 (關鍵)

  • sync-bulk-index - 使用批次 API 分批索引
  • sync-debounce-same-id - 對相同 ID 的叢發請求進行去抖
  • sync-delete-immediately - 立即從索引中刪除
  • sync-enqueue-index - 將索引操作排入佇列;切勿內嵌寫入 Elasticsearch
  • sync-full-reindex-alias-swap - 透過版本化索引並交換別名來重建索引
  • sync-idempotent-index-creation - 使用確定性名稱以冪等方式建立索引

3. 綱要與映射 (高)

  • map-analyzers - 審慎選擇文字分析器
  • map-derived-fields - 在批次映射器中計算衍生欄位與關聯欄位
  • map-exclude-non-search-fields - 僅索引具有搜尋目的的欄位
  • map-mapping-object - 將映射宣告為型別化物件
  • map-typed-search-document - 根據實體型別化搜尋文件

4. 查詢 (高)

  • query-cursor-pagination - 使用 search_after 進行分頁
  • query-hydrate-restore-order - 從 MongoDB 還原並恢復相關性順序
  • query-ids-only-search - 搜尋僅回傳 ID
  • query-sort-tiebreaker - 永遠包含唯一的排序平手打破器

5. 跨實體 (中高)

  • rel-derived-counter-cache - 在擁有文件上快取衍生計數器
  • rel-owning-module-subscribes - 擁有模組訂閱事件;其他模組發射自己的事件

6. 操作與開發體驗 (中)

  • ops-cli-scripts - 提供遷移與重建索引的 CLI 指令
  • ops-explain-debug - 提供解釋鉤子
  • ops-health-check - 將 Elasticsearch 納入健康檢查
  • ops-log-es-requests - 記錄 Elasticsearch 請求和回應
  • ops-no-cron-for-indexing - 切勿使用進程內計時器排程索引

7. 設定與環境 (高)

  • cfg-mongo-fallback-backend - 回退到確定性的 MongoDB 搜尋器
  • cfg-search-backend-seam - 將搜尋放在後端接縫之後

8. 測試 (中高)

  • test-mock-elasticsearch-seam - 在單元測試中模擬搜尋後端接縫
  • test-real-es-for-query-semantics - 針對真實 Elasticsearch 測試查詢語意

使用方法

閱讀個別規則檔案以取得詳細說明與程式碼範例:

rules/arch-entity-vs-search-document.md
rules/sync-full-reindex-alias-swap.md
rules/query-ids-only-search.md

每個規則檔案包含:

  • 簡要說明其重要性及適用層級
  • 不正確的程式碼範例與說明
  • 正確的程式碼範例與說明
  • 額外上下文與參考資料

與其他技能的關係

  • nestjs-graphql-structure — 檔案放置。本技能在功能中新增 search/ 資料夾,以及 common/search/ + infra/elasticsearch/ 層;它遵循該技能的功能模組、儲存庫和事件規則。
  • nestjs-best-practices — 本技能範例所依據的服務、事件、佇列、設定和健康檢查的行為規則。
  • 一個刻意例外 — nestjs-graphql-structure 的 service-background-jobs 規則將處理器保留在擁有功能中。搜尋管線在 common/search/ 中執行一次,並透過索引註冊表解析每個功能的儲存庫,因為所有功能的管線完全相同;請參閱 arch-base-search-repository。

完整彙編文件

如需包含所有展開規則的完整指南,請參閱 AGENTS.md。

Individual skills in this repo

This repo contains 2 individual skills — each has its own dedicated page.

Zoha/agent-skills

Application structure and file-placement guide for NestJS GraphQL (code-first) applications. Use when deciding where a file belongs, designing or reviewing a feature-first directory layout, scaffolding a new feature module, or working out how modules connect to resolve GraphQL fields and relations. Covers the base structure map plus per-file rules for modules, services, resolvers (query, mutation, subscription, field), inputs and args, pagination, entities (Mongoose schema best practices for shared types), repository-only model injection, models/mappers, repositories, Mongoose discriminators, derived fields, providers, loaders, events, background jobs, auth, exceptions, custom scalars, config, common building blocks, infra setup, and the naming/testing/lint conventions — and module-resolution concerns such as circular dependencies. For behavioral rules (DI depth, security, performance, error handling) use the nestjs-best-practices skill instead.

Zoha/agent-skills

Use when caching GraphQL reads in a NestJS (code-first) app — wiring a stale-while-revalidate (SWR) cache module once, deciding what to cache and where to mount it (service, field, operation, or CDN), and keeping it leak-safe. Triggers when you need SWR, single-flight dedupe, tag invalidation, per-user vs public scoping, cache observability, a stampede or blocking-miss at TTL expiry, a shared-key data leak, a dev/ops switch that disables caching entirely, or a one-time setup reference (module, file placement, storage, referencesTTL/GC, caller identity, decorator runtime, fail-open).

相關技能