Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Phrase search matches query terms that appear together in a specific order, with an optional slop tolerance for intervening tokens. It's the right tool when word order is meaningful (multi-word product names, quoted user input, error strings) and when a plain $search + text query returns too much noise because the tokens are common individually but rare together.
Note
Full-text search is in preview. Before using these examples, enable full-text search on your cluster.
What is phrase search?
A $search + text query matches every document that contains the requested tokens, in any order, anywhere in the field. A $search + phrase query enforces that the tokens appear in the same order the user supplied, with at most slop other tokens between them. Phrase precision matters when:
- The user enters a quoted string such as
"bracket controller". - Title, entity, or error-string matching depends on word order.
- The component words are common individually (
bracket,controller) but their combination is the actual signal.
The slop parameter
slop controls the permitted distance between phrase terms and accepts integer values from 0 through 10. For a two-term phrase, it allows intervening tokens as shown in the following examples.
slop |
Behavior | Example match for query "bracket controller" |
|---|---|---|
0 |
Strict adjacency. | Matches "bracket controller". Does not match "bracket for controller". |
1 |
One intervening token allowed. | Matches "bracket for controller". |
3 |
Broader proximity. | Matches "bracket and the new controller"; useful for titles with adjectives or articles. |
Higher slop values increase recall but reduce precision and ranking quality.
How to run a phrase query
// ❌ Plain text query for "bracket controller" matches tokens in any order.
// Returns "controller for bracket", "bracket without controller", etc.
db.products.aggregate([
{ $search: {
index: "idx_title_standard",
text: { query: "bracket controller", path: "title" }
}},
{ $limit: 20 }
]);
// ❌ Regex hack: loses BM25 ranking and forces a COLLSCAN.
db.products.find({ title: { $regex: "bracket.*controller" } });
// ✅ Phrase search with slop: 3 against the BM25 index.
db.products.aggregate([
{
$search: {
index: "idx_title_standard",
phrase: {
query: "bracket controller",
path: "title",
slop: 3
}
}
},
{ $limit: 20 },
{
$project: {
_id: 0,
title: 1,
score: { $meta: "searchScore" }
}
}
]);
The same rules from BM25 keyword search apply: $search is the first stage, index: "<name>" is set explicitly, and $limit is downstream.
Phrase search with downstream filters
To combine phrase matching with structured conditions inside $search, map the structured fields in the same index and use compound. See Structured and text search.
You can also apply a downstream $match. Place it before the final $limit so the limit applies to eligible results.
db.products.aggregate([
{ $search: {
index: "idx_title_standard",
phrase: { query: "bracket controller", path: "title", slop: 1 }
}},
{ $match: { inStock: true } },
{ $limit: 20 },
{ $project: { _id: 0, title: 1, price: 1, score: { $meta: "searchScore" } } }
]);
Known constraint
Important
The phrase operator doesn't accept fuzzy. You can combine separate phrase and fuzzy-text conditions with compound clauses, but this approach doesn't provide typo-tolerant phrase matching. Alternatively, fuse separate result lists with Reciprocal Rank Fusion (RRF) for broader recall. Fusion can include results that match only one query, so it doesn't guarantee both phrase order and typo tolerance.