Skip to content

Commit cf1f20f

Browse files
authored
fix: remove get-search-stage-rules tool CLOUDP-424119 (#1381)
1 parent cb16627 commit cf1f20f

7 files changed

Lines changed: 95 additions & 323 deletions

File tree

api-extractor/reports/tools.public.api.md

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1698,24 +1698,6 @@ export class SearchKnowledgeTool extends AssistantToolBase {
16981698
static toolName: string;
16991699
}
17001700

1701-
// @public (undocumented)
1702-
export class SearchStageRulesTool extends ToolBase {
1703-
// (undocumented)
1704-
argsShape: {};
1705-
// (undocumented)
1706-
static category: ToolCategory;
1707-
// (undocumented)
1708-
description: string;
1709-
// (undocumented)
1710-
protected execute(): Promise<CallToolResult>;
1711-
// (undocumented)
1712-
static operationType: OperationType;
1713-
// (undocumented)
1714-
protected resolveTelemetryMetadata(): TelemetryToolMetadata;
1715-
// (undocumented)
1716-
static toolName: string;
1717-
}
1718-
17191701
// @public (undocumented)
17201702
export class StreamsBuildTool extends StreamsToolBase {
17211703
// (undocumented)

src/tools/mongodb/read/aggregate.ts

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,100 @@ import { bsonToJson } from "../../../helpers/bsonToJson.js";
2828
export const pipelineDescriptionWithVectorSearch = `\
2929
An array of aggregation stages to execute.
3030
31-
If the user's request involves \`$search\`, \`$vectorSearch\`, \`$rankFusion\`, \`$scoreFusion\`, or \`$rerank\`, call the \`get-search-stage-rules\` tool first to learn the construction rules for these stages before building the pipeline.
31+
If the user has asked for a vector search, \`$vectorSearch\` **MUST** be the first stage
32+
of the pipeline (or the first stage of a \`$unionWith\` sub-pipeline only when explicitly
33+
combining unrelated result sets — for hybrid full-text + vector search, use \`$rankFusion\`
34+
or \`$scoreFusion\` instead, see below).
35+
36+
If the user has asked for lexical/Atlas search, use \`$search\` instead of \`$text\`.
37+
### Usage Rules for \`$vectorSearch\`
38+
- **Index Type Detection:**
39+
Use the collection-indexes tool to determine if the target field has a classic vector index (type: 'vector') or an auto-embed index (type: 'autoEmbed').
40+
- **Classic Vector Search (type: 'vector'):**
41+
Use 'queryVector' with embeddings as an array of numbers.
42+
- **Auto-Embed Vector Search (type: 'autoEmbed'):**
43+
Use 'query' - MongoDB automatically generates embeddings at query time. Do NOT use 'queryVector' or 'embeddingParameters' for auto-embed indexes.
44+
- **Unset embeddings:**
45+
Unless the user explicitly requests the embeddings, add an \`$unset\` stage **at the end of the pipeline** to remove the embedding field and avoid context limits. **The $unset stage in this situation is mandatory**.
46+
- **Pre-filtering:**
47+
If the user requests additional filtering, include filters in \`$vectorSearch.filter\` only for pre-filter fields in the vector index.
48+
NEVER include fields in $vectorSearch.filter that are not part of the vector index.
49+
- **Post-filtering:**
50+
For all remaining filters, add a $match stage after $vectorSearch.
51+
- If unsure which fields are filterable, use the collection-indexes tool to determine valid prefilter fields.
52+
- If no requested filters are valid prefilters, omit the filter key from $vectorSearch.
53+
54+
### Usage Rules for \`$search\`
55+
- Include the index name, unless you know for a fact there's a default index. If unsure, use the collection-indexes tool to determine the index name.
56+
- The \`$search\` stage supports multiple operators, such as 'autocomplete', 'text', 'geoWithin', and others. Choose the appropriate operator based on the user's query. If unsure of the exact syntax, consult the MongoDB Atlas Search documentation, which can be found here: https://www.mongodb.com/docs/atlas/atlas-search/operators-and-collectors/
57+
58+
### Usage Rules for \`$rankFusion\` and \`$scoreFusion\` (Hybrid Search)
59+
Use these stages when the user wants to combine full-text (\`$search\`) and vector
60+
(\`$vectorSearch\`) retrieval into a single fused result set. **Prefer native
61+
fusion over a \`$unionWith\` + \`$group\` workaround** — the workaround averages
62+
incompatible score scales and produces wrong rankings.
63+
64+
**Which stage to use:**
65+
- \`$rankFusion\` (MongoDB 8.0+) — Reciprocal Rank Fusion. The recommended default.
66+
Normalizes scores across incompatible scales automatically. No score tuning needed.
67+
- \`$scoreFusion\` (MongoDB 8.2+) — Score-based fusion. Use when the user needs explicit
68+
per-pipeline weights, score normalisation (sigmoid / minMaxScaler), or a custom
69+
combination expression.
70+
71+
**Construction rules:**
72+
- \`$rankFusion\` / \`$scoreFusion\` MUST be the first stage of the top-level pipeline.
73+
- Sub-pipelines go inside \`input.pipelines\` as a named map (not an array). Each name
74+
must be non-empty, must not start with \`$\`, and must not contain \`.\` or null bytes.
75+
- Allowed stages inside sub-pipelines: \`$search\`, \`$vectorSearch\`, \`$match\`, \`$sort\`,
76+
\`$geoNear\`, \`$skip\`, \`$limit\`. \`$project\` and \`$unset\` are NOT allowed inside sub-pipelines.
77+
- Do field shaping (\`$project\` / \`$unset\`) only AFTER the fusion stage, at the root.
78+
- Both a vectorSearch (or autoEmbed) index AND a search (lexical) index must exist on
79+
the collection. Use the collection-indexes tool to confirm both before running a hybrid query.
80+
- Add a \`$limit\` stage after the fusion stage to cap the final result set.
81+
- Add \`$unset\` at the end to remove embedding fields and avoid context bloat.
82+
83+
### Usage Rules for \`$rerank\` (Native Reranking)
84+
Use this stage when the user wants to reorder a set of candidate documents using a cross-encoder reranker model.
85+
86+
**Construction rules:**
87+
- \`$rerank\` can be any stage in the pipeline on an Atlas cluster running MongoDB 8.3 or higher.
88+
- It is recommended to use \`$rerank\` after a sorted pipeline, e.g. \`$search\`, \`$vectorSearch\`, \`$rankFusion\`, \`$scoreFusion\`, or [\`$match\`, \`$sort\`].
89+
- $rerank must be enabled via the Native Reranking Project Setting
90+
- Set \`numDocsToRerank\` as the number of documents passed into \`$rerank\`. This will also limit the number of documents returned by \`$rerank\`
91+
- Set \`path\` as a field name or an array of field names that exist in all documents. Use \`$match\` or \`$set\` before \`$rerank\` to validate no fields are missing.
92+
- Add \`$addFields\` after \`$rerank\` to retrieve the reranker score.
93+
94+
**\`$rerank\` example (recommended default):**
95+
\`\`\`javascript
96+
[
97+
{
98+
$match: {
99+
description: { $exists: true },
100+
name: { $exists: true }
101+
}
102+
},
103+
{
104+
$sort: {
105+
lastUpdated: -1
106+
}
107+
},
108+
{
109+
$rerank: {
110+
query: {
111+
text: "query text including instructions"
112+
},
113+
model: "rerank-2.5",
114+
numDocsToRerank: 100,
115+
path: ["description", "name"]
116+
}
117+
},
118+
{
119+
$addFields: {
120+
rerankScore: { $meta: "score" }
121+
}
122+
}
123+
]
124+
\`\`\`
32125
`;
33126

34127
const AggregateOutputSchema = {

src/tools/mongodb/reference/searchStageRules.ts

Lines changed: 0 additions & 126 deletions
This file was deleted.

src/tools/mongodb/tools.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ export { CountTool } from "./read/count.js";
1212
export { DbStatsTool, type DbStatsOutput } from "./metadata/dbStats.js";
1313
export { AggregateTool } from "./read/aggregate.js";
1414
export { AggregateDBTool } from "./read/aggregateDB.js";
15-
export { SearchStageRulesTool } from "./reference/searchStageRules.js";
1615
export { UpdateManyTool, type UpdateManyOutput } from "./update/updateMany.js";
1716
export { RenameCollectionTool, type RenameCollectionOutput } from "./update/renameCollection.js";
1817
export { DropDatabaseTool, type DropDatabaseOutput } from "./delete/dropDatabase.js";

0 commit comments

Comments
 (0)