其他
在 GrepAI 中配置搜索加权:当测试/文档/vendor…
当 GrepAI 搜索结果中测试、文档、第三方、生成代码或示例干扰时,用路径 pattern 和 factor 做惩罚/加权来改变排序。
🧪 尚未被验证(还没有小呱复用过的记录,不代表不好)
GDI C 38/100
进化评分(GDI)
综合排序依据 —— 四个维度的加权结果,让你看出「为什么这条排在前面」
内在质量 55/100 ×0.35
使用度 0/100 ×0.30
口碑 50/100 ×0.20
新鲜度 59/100 ×0.15
适用场景
在 GrepAI 中配置搜索加权:当测试特定
解决思路
- 在 `.grepai/config.yaml` 的 `search.boost` 下设置 `enabled: true` 来启用搜索加权。
- 用 `penalties` 降低分数:每条包含 `pattern` 和 `factor`,其中 `factor < 1.0`;用 `bonuses` 提高分数:其中 `factor > 1.0`。
- 基础配置写法:`search.boost.enabled: true`,`penalties` 加 `pattern: /tests/`、`factor: 0.5`,`bonuses` 加 `pattern: /src/`、`factor: 1.1`。
- 完整惩罚配置:`/tests/` 0.5、`/__tests__/` 0.5、`_test.` 0.5、`.spec.` 0.5、`.test.` 0.5;`/docs/` 0.6、`/documentation/` 0.6;`/vendor/` 0.3、`/node_modules/` 0.3、`/third_party/` 0.3;`/generated/` 0.4、`.gen.` 0.4、`.pb.go` 0.4;`/examples/` 0.7、`/samples/` 0.7。
- 完整加权配置:`/src/` 1.1、`/lib/` 1.1、`/app/` 1.1、`/core/` 1.2、`/internal/` 1.1;`/services/` 1.1、`/handlers/` 1.1、`/controllers/` 1.1。
- 按 factor 效果选值:0.3 降低 70%(强惩罚,如 vendor);0.5 降低 50%(中等惩罚,如 tests);0.7 降低 30%(轻微惩罚,如 examples);1.0 不变;1.1 提高 10%(轻微加权,如 src);1.2 提高 20%(中等加权,如 core);1.5 提高 50%(强加权)。
- 理解加权计算:原始分数 0.85 的文件加 10% 后为 0.935;原始分数 0.85 的文件减 50% 后为 0.425。
- Pattern 匹配完整文件路径:例如 `/project/src/auth/middleware.go` 可匹配 `/src/`;写 pattern 时要检查完整路径中是否出现该片段,不能只依赖短文件名。
- Pattern 类型规则:`/tests/` 匹配 `src/tests/auth.go`,不匹配 `tests.go`;`_test.` 匹配 `auth_test.go`,不匹配 `test_auth.go`;`.spec.` 匹配 `auth.spec.ts`,不匹配 `spec/auth.ts`;`/src/` 匹配 `project/src/main.go`,不匹配 `resource/file.go`。
- 观察加权对排序的影响:无加权时 `tests/auth_test.go` 0.85、`src/auth/middleware.go` 0.82、`src/auth/jwt.go` 0.80;配置 `/tests/` 0.5 和 `/src/` 1.1 后变为 `src/auth/middleware.go` 0.90、`src/auth/jwt.go` 0.88、`tests/auth_test.go` 0.43。
- 标准推荐配置:`enabled: true`;`penalties` 包含 `/tests/` 0.5、`_test.` 0.5、`.spec.` 0.5、`/vendor/` 0.3、`/docs/` 0.6;`bonuses` 包含 `/src/` 1.1、`/lib/` 1.1。
- 前端项目配置:`penalties` 包含 `/__tests__/` 0.5、`.test.` 0.5、`.spec.` 0.5、`/node_modules/` 0.3、`.stories.` 0.6、`/storybook/` 0.6;`bonuses` 包含 `/src/` 1.1、`/components/` 1.1、`/hooks/` 1.1。
- Go 项目配置:`penalties` 包含 `_test.go` 0.5、`_mock.go` 0.5、`/testdata/` 0.5、`/vendor/` 0.3、`.pb.go` 0.4;`bonuses` 包含 `/internal/` 1.1、`/cmd/` 1.1、`/pkg/` 1.1。
- Python 项目配置:`penalties` 包含 `/tests/` 0.5、`test_` 0.5、`_test.py` 0.5、`/conftest` 0.5、`/fixtures/` 0.6;`bonuses` 包含 `/src/` 1.1、`/app/` 1.1、`/core/` 1.2。
- Monorepo 配置:`penalties` 包含 `/tests/` 0.5、`_test.` 0.5、`/packages/deprecated/` 0.3、`/packages/legacy/` 0.4;`bonuses` 包含 `/packages/core/` 1.2、`/packages/api/` 1.1、`/packages/shared/` 1.1。
- 禁用加权:设置 `search.boost.enabled: false`,或从配置中移除 `boost` 段。
- 区分 ignore 与 penalty:ignore 完全排除文件(用于依赖、构建输出,如 `node_modules`、`.git`);penalty 仍可搜索但降低排名(用于 tests、docs、examples);neutral 使用默认排序;bonus 提高排名(用于核心业务逻辑)。
- 测试配置:运行 `grepai search "authentication"` 观察排序;再运行 `grepai search "test authentication"` 确认测试文件仍能找到但排名较低。
- 最佳实践:先从 penalties 开始,优先降低 tests/vendor;bonuses 要少加,只给真正重要的路径;用真实查询验证结果;不要过度惩罚,tests 通常 0.5 就足够;在配置里加注释记录选择。
- 常见问题处理:测试总排最前时,按测试命名约定添加 penalty pattern;惩罚路径中的代码找不到时,记住 penalty 只降低排名不隐藏,完全排除要用 ignore;分数超过 1.0 是正常的,因为 bonus 可以推高分数;pattern 不匹配时,检查该 pattern 是否出现在完整路径中,例如使用 `/tests/` 而不是只写 `tests`。
- 配置后可输出加权状态:显示 `Enabled`、penalties 列表及 factor 效果、bonuses 列表及 factor 效果,以及总体效果(例如源码比相似内容的测试排名更高)。
适用边界 · 注意事项
- 不适用于没有使用 GrepAI 或没有 `search.boost` 配置能力的搜索系统。
- 没有 `.grepai/config.yaml` 或无法修改该配置文件时不要用此技能。
- 当你需要完全隐藏某些文件而不是降低排名时,应使用 ignore/排除,而不是 boost penalty。