You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/tools/mongodb/read/aggregate.ts
+94-1Lines changed: 94 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -28,7 +28,100 @@ import { bsonToJson } from "../../../helpers/bsonToJson.js";
28
28
exportconstpipelineDescriptionWithVectorSearch=`\
29
29
An array of aggregation stages to execute.
30
30
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.
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.
\`$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.
0 commit comments