一句话:
IndexModule 是“为单个索引创建 IndexService 的工厂 + 插件扩展点”,负责把索引级别的配置、插件、分析器、相似度、快照引擎等组件装配成一个可运行的 IndexService 实例。
---
✅ IndexModule 的核心职责(启动阶段)
维度 作用
工厂角色 创建并返回 `IndexService` 实例(见 `IndexModule.newIndexService(...)`)。
插件扩展点 允许插件通过 `IndexModule.add*()` 方法注册:- 自定义 `Similarity`(评分算法)- 自定义 `QueryCache`- 自定义 `Directory`(存储后端)- 自定义 `IndexEventListener`- 自定义 `MergePolicy`、`Codec`、`RecoveryStateFactory` …
配置注入 把索引级设置(settings)、映射(mapping)、分析器(analysis)注入到即将创建的 IndexService。
版本/兼容性 检查索引创建时的版本兼容性,设置正确的 lucene codec。
---
✅ 与 IndexService、IndexShard 的创建关系
1. 节点启动 → `IndicesService.createIndex(...)`
2. 构建一个 IndexModule(此时只是“配方”)
3. 插件通过 `onIndexModule()` 回调往里加料
4. `IndexModule.newIndexService(...)` → 真正生成 IndexService
5. IndexService 再按需创建 IndexShard
---
✅ 一句话总结
> IndexModule 就是“索引的装配车间”:把配置、插件、扩展点打包好,最终生产出 IndexService;它本身不保存运行时状态,只在索引创建/恢复时用到一次。