news 2026/9/12 16:22:28

Backstage 如何用 entityTransformer 定制 Catalog 与 TechDocs 索引字段?

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Backstage 如何用 entityTransformer 定制 Catalog 与 TechDocs 索引字段?

Backstage 如何用 entityTransformer 定制 Catalog 与 TechDocs 索引字段?

【免费下载链接】backstageBackstage is an open framework for building developer portals项目地址: https://gitcode.com/GitHub_Trending/ba/backstage

当你发现 Backstage 搜索结果中的标题、描述文本不够准确,或者想让索引里多带上 tags 之类的字段时,需要改的不是搜索引擎配置,而是 collator 把实体转换成索引文档的那一步。官方提供的扩展点就是entityTransformer:向 Catalog 或 TechDocs 的 collator 工厂注入一个回调,控制哪些数据进入搜索索引,既可以在默认输出的基础上修改字段,也可以为某个kind整体重写索引文档(但仍需遵循文档要求的基本结构)。

本文覆盖两个索引的定制方法:@backstage/plugin-search-backend-module-catalogCatalogCollatorEntityTransformer@backstage/plugin-search-backend-module-techdocsTechDocsCollatorEntityTransformer,并给出新、旧两套后端系统的对应写法。

先搞清楚默认索引了哪些字段

定制之前,先看默认转换器生成了什么。Catalog 默认的转换器(defaultCatalogCollatorEntityTransformer.ts)为每个实体生成以下字段:

字段默认取值
titleentity.metadata.title,缺失时回退到entity.metadata.name
textmetadata.description拼接;User/Group实体还会加入profile.displayNameUser实体再追加profile.email,各段以:连接
componentType/typeentity.spec?.type,缺失时为'other'
namespaceentity.metadata.namespace,缺失时为'default'
kindentity.kind
lifecycleentity.spec?.lifecycle,缺失时为空字符串
ownerentity.spec?.owner,缺失时为空字符串

TechDocs 侧的TechDocsCollatorEntityTransformer返回类型是Partial<Omit<TechDocsDocument, 'location' | 'authorization'>>(见 TechDocsCollatorEntityTransformer.ts),即允许只返回增量字段,向索引文档追加属性。

字段修改的硬性限制

在写转换器之前记住两条限制,它们来自官方文档与类型定义:

  • authorizationlocation两个字段不能通过entityTransformer修改(类型定义中已对这两个字段做了Omit);
  • location只能通过locationTemplate修改,而不是 transformer。

另外,两个模块的扩展点都只允许设置一次:TechDocs 侧再次调用会抛出TechDocs collator entity transformer may only be set once(module.ts),Catalog 侧的setEntityTransformer同样有 "can only be called once" 限制。也就是说同一个 collator 上注册多份 transformer 是不允许的。

新后端系统:通过 createBackendModule 注入 transformer

新后端系统下,collator 由模块自动注册,你无法直接调用indexBuilder.addCollator,需要用自己的 backend module 依赖模块导出的扩展点来替换 transformer。

先确认两个模块已安装(在 Backstage 根目录执行):

yarn --cwd packages/backend add @backstage/plugin-search-backend-module-catalog yarn --cwd packages/backend add @backstage/plugin-search-backend-module-techdocs

定制 Catalog 索引

search-backend-module-catalog 的 README给出了完整的写法:从主入口导入CatalogCollatorEntityTransformer类型,从/alpha入口导入catalogCollatorExtensionPoint,然后注册一个 backend module:

// packages/backend/src/index.ts import { createBackend } from '@backstage/backend-defaults'; import { createBackendModule } from '@backstage/backend-plugin-api'; import { CatalogCollatorEntityTransformer } from '@backstage/plugin-search-backend-module-catalog'; import { catalogCollatorExtensionPoint } from '@backstage/plugin-search-backend-module-catalog/alpha'; const customTransformer: CatalogCollatorEntityTransformer = entity => ({ title: entity.metadata.title || entity.metadata.name, text: entity.metadata.description || '', componentType: entity.spec?.type?.toString() || 'other', type: entity.spec?.type?.toString() || 'other', namespace: entity.metadata.namespace || 'default', kind: entity.kind, lifecycle: (entity.spec?.lifecycle as string) || '', owner: (entity.spec?.owner as string) || '', }); const backend = createBackend(); backend.add(import('@backstage/plugin-search-backend')); backend.add(import('@backstage/plugin-search-backend-module-catalog')); backend.add( createBackendModule({ pluginId: 'search', moduleId: 'my-catalog-collator-options', register(reg) { reg.registerInit({ deps: { collator: catalogCollatorExtensionPoint }, async init({ collator }) { collator.setEntityTransformer(customTransformer); }, }); }, })(), ); backend.start();

上面的customTransformer与默认输出字段一一对应,你可以在此基础上增删、改写字段。由于返回类型是Omit<CatalogEntityDocument, 'location' | 'authorization'>,完整重写时仍需包含titletextcomponentTypetypenamespacekindlifecycleowner这些字段。

定制 TechDocs 索引

TechDocs 模块导出的扩展点是 techdocsCollatorEntityTransformerExtensionPoint,它提供两个方法:

  • setTransformer(transformer):替换实体到索引文档的转换逻辑;
  • setDocumentTransformer(transformer):替换索引文档到最终 search doc 的转换逻辑。

写法与 Catalog 侧的 backend module 模式相同:在packages/backend/src/index.ts中用createBackendModule依赖该扩展点,并在init中调用setTransformer。两者各自只能设置一次,重复注册会抛出TechDocs collator entity transformer may only be set onceTechDocs collator document transformer may only be set once

旧后端系统:通过 indexBuilder 直接传入 entityTransformer

如果你的后端仍使用旧的系统(入口形如packages/backend/src/plugins/search.ts,通过env.configindexBuilder.addCollator装配),则不需要 backend module,直接把回调传给 collator 工厂。Search How-To guides中的官方示例:

const catalogEntityTransformer: CatalogCollatorEntityTransformer = ( entity: Entity, ) => { if (entity.kind === 'SomeKind') { return { // customize here output for 'SomeKind' kind }; } return { // and customize default output ...defaultCatalogCollatorEntityTransformer(entity), text: 'my super cool text', }; }; indexBuilder.addCollator({ collator: DefaultCatalogCollatorFactory.fromConfig(env.config, { discovery: env.discovery, tokenManager: env.tokenManager, entityTransformer: catalogEntityTransformer, }), }); const techDocsEntityTransformer: TechDocsCollatorEntityTransformer = ( entity: Entity, ) => { return { // add more fields to the index tags: entity.metadata.tags, }; }; const techDocsDocumentTransformer: TechDocsCollatorDocumentTransformer = ( doc: MkSearchIndexDoc, ) => { return { // add more fields to the index bost: doc.boost, }; }; indexBuilder.addCollator({ collator: DefaultTechDocsCollatorFactory.fromConfig(env.config, { discovery: env.discovery, tokenManager: env.tokenManager, entityTransformer: techDocsEntityTransformer, documentTransformer: techDocsDocumentTransformer, }), });

这段示例展示了三种典型用法:按kind分支整体重写(第一个if分支)、在默认输出基础上追加或覆盖字段(展开defaultCatalogCollatorEntityTransformer(entity)后修改text)、以及 TechDocs 的增量字段(只返回tags,因为返回类型允许 partial)。

注意 TechDocs 侧有两个层面的 transformer:entityTransformer作用在实体转索引文档这一步,documentTransformer作用在索引文档之后的转换,两者可以分别定制。

重启后如何确认改动生效

索引不是实时的:collator 按调度周期性重建索引,调度间隔通过 app-config 控制,参数分别放在search.collators.catalogsearch.collators.techdocs配置键下(各模块 README 都说明了这一点,具体可选项见各自包内的config.d.ts)。

因此完整路径是:修改 transformer 代码 → 重启后端 → 等待当前索引周期重建 → 在搜索界面按你新增或改写的字段查询,确认命中文档的字段内容来自自定义输出。由于locationauthorization不受 transformer 影响,如果你的定制意图涉及这两个字段,应改为配置locationTemplate(旧系统下通过 collator 工厂的locationTemplate选项),而不是继续改 transformer。

小结与限制

  • 定制入口:新后端系统用createBackendModule+ 扩展点(Catalog 用catalogCollatorExtensionPoint,TechDocs 用techdocsCollatorEntityTransformerExtensionPoint);旧后端系统直接给DefaultCatalogCollatorFactory/DefaultTechDocsCollatorFactoryentityTransformer(TechDocs 还可加documentTransformer)。
  • 硬性边界:authorizationlocation不可经 transformer 修改;每个 transformer 扩展点只能设置一次。
  • 文档未给出针对索引内容的专用调试接口或断言命令,验证只能依赖上述「重建周期 + 搜索命中字段」的方式;如需更细的字段级核对,可以对照各模块config.d.ts中调度配置确认重建时机。

参考文件:Search How-To guides、search-backend-module-catalog README、search-backend-module-techdocs README、TechDocs collator 模块实现。

【免费下载链接】backstageBackstage is an open framework for building developer portals项目地址: https://gitcode.com/GitHub_Trending/ba/backstage

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/9/12 16:19:22

CookLikeHOC 蒜蓉娃娃菜复刻指南:12 份标准调味料配方与蒸柜工艺详解

CookLikeHOC 蒜蓉娃娃菜复刻指南&#xff1a;12 份标准调味料配方与蒸柜工艺详解 【免费下载链接】CookLikeHOC &#x1f962;像老乡鸡&#x1f414;那样做饭。已添加2026年发布的《老乡鸡菜品溯源报告 2.0中新出现的菜品。主要部分于2024年完工&#xff0c;非老乡鸡官方仓库。…

作者头像 李华
网站建设 2026/9/12 16:13:31

DINOv2 视觉特征提取实战:跑通推理只要 5 分钟,坑一次讲清

DINOv2 视觉特征提取实战&#xff1a;跑通推理只要 5 分钟&#xff0c;坑一次讲清 【免费下载链接】dinov2 PyTorch code and models for the DINOv2 self-supervised learning method. 项目地址: https://gitcode.com/GitHub_Trending/di/dinov2 不想微调、只想直接拿到…

作者头像 李华
网站建设 2026/9/12 16:13:17

SpringBoot+Vue医院挂号系统:事务一致性与并发控制实战

简介&#xff1a;这是一套基于SpringBoot开发的医院预约挂号系统完整源码&#xff0c;面向计算机、电子信息工程等专业学生&#xff0c;适用于高分毕业设计、课程设计及期末大作业。系统采用B/S架构与MVC模式&#xff0c;整合Java、MySQL、MyBatis、Vue、Ajax等主流技术&#x…

作者头像 李华