Community编程与开发github.com

Zoha/agent-skills

用于在 NestJS 后端中添加或维护 Elasticsearch 全文搜索——包括客户端和模块连接、MongoDB 到 Elasticsearch 的同步与重建索引、索引映射和搜索投影、仅返回 ID 的查询与 MongoDB 数据填充、游标分页、跨实体派生字段、通过可配置后端抽象实现可选的搜索功能,以及针对模拟后端或真实临时集群的测试。

agent-skills 是什么?

该技能(即 SKILL)为 NestJS GraphQL + MongoDB 应用提供了一整套 Elasticsearch 集成指南。其核心架构思想是“Elasticsearch 负责查找,MongoDB 负责提供数据”:ES 仅作为纯搜索索引返回文档 ID,而实际实体始终从 MongoDB 中填充,确保 MongoDB 作为唯一数据源。技能涵盖了从索引映射定义、搜索投影设计、批量同步/重建索引、跨实体派生字段计算到游标分页、基于 ID 的查询与数据填充等全生命周期。它特别强调了关键设计模式,例如:每个特性模块拥有自己的索引、通过后端抽象(seam)使搜索可配置(可选)、提供 MongoDB 回退方案、以及支持基于 mock 或真实 ES 集群的测试。该技能适用于 Cursor、Claude Code 等主流 AI 编码代理,并按优先级分类了架构、同步、映射、查询等 8 个类别的规则,便于用户快速查找和遵循最佳实践。

兼容平台~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

为 NestJS GraphQL + MongoDB 应用添加 Elasticsearch 支持的集成指南。它首先确立了架构边界(通用基础设施、共享搜索层、每个特性模块拥有自己的索引),然后覆盖了完整生命周期:搜索投影、索引与同步、基于 ID 查询与 MongoDB 数据填充、游标分页、跨实体派生字段、通过可配置后端抽象使搜索可选、无需实际集群即可测试,以及相关的运维和开发者体验。

指导原则是 Elasticsearch 负责查找,MongoDB 负责提供数据:Elasticsearch 仅作为纯搜索索引返回标识符,请求的实体始终从作为唯一数据源的 MongoDB 中填充。

何时应用

在以下情况参考这些指南:

  • 为已有的 NestJS GraphQL + MongoDB 应用添加搜索功能
  • 连接 @nestjs/elasticsearch 客户端和共享搜索层
  • 定义或更改索引映射或搜索投影
  • 实现 MongoDB 到 Elasticsearch 的同步、重建索引或索引迁移
  • 编写搜索查询、游标分页或结果数据填充
  • 使派生值(计数、评分)跨实体可搜索
  • 通过配置使搜索可选,或在没有集群的情况下切换后端
  • 使用模拟后端抽象或针对真实临时集群测试搜索
  • 调试过时的搜索结果或索引风暴

按优先级分类的规则类别

优先级类别影响程度前缀
1架构关键arch-
2索引与同步关键sync-
3模式与映射高map-
4查询高query-
5跨实体中高rel-
6运维与 DX中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. 运维与 DX(中)

  • 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).

相关技能