13. Text Indexes and Text Search
A text index supports MongoDB's built-in text-search query features for indexed string content. A text index can include one or more string fields and can assign weights to influence the relative importance of fields.
Text search is not identical to substring matching or a full search-engine platform. It has language, tokenization, stemming, and query behavior that should be tested against the application's requirements. Text indexes also have constraints, including the number and configuration of text indexes permitted for a collection.
For simple prefix lookups or exact normalized values, a normal index may be more suitable. For advanced relevance tuning, autocomplete, typo tolerance, or complex search experiences, consider whether a dedicated search solution is required.
db.articles.createIndex({ title: "text", body: "text" });
db.articles.find(
{ $text: { $search: "mongodb aggregation" } },
{ score: { $meta: "textScore" }, title: 1 }
).sort({ score: { $meta: "textScore" } });- Text indexes support MongoDB's built-in text search.
- Text search behavior differs from substring matching.
- Weights can influence field importance.
- Validate whether built-in text search meets the product requirements.
What is a MongoDB text index primarily designed to support?