MongoDB Full-Text Relevance Tuning
MongoDB Full-Text Relevance Tuning
MongoDB Full-Text Relevance Tuning
Tip: Field Weights
$collection->createIndex(
{ title: 'text', content: 'text' },
{ weights: { title: 10, content: 1 } }
);
Title matches score 10x higher than content.
Gotcha: Default Weight is 1
Fields without explicit weight get weight 1.
Tip: Language Override
$collection->find(
{ $text: { $search: 'php', $language: 'russian' } }
);
Gotcha: Stop Words
Common words are excluded from text indexes. Configure per language.
Tip: Phrase Search
{ $text: { $search: '"machine learning"' } }
Quotes search for exact phrases.
Gotcha: Text Score Projection
{ $meta: 'textScore' }
Include relevance score in results for sorting.
Tip: Embed or Reference? The 80/20 Rule
If you always access data together, embed it. If you access it independently, reference it. The 16MB document size limit is the hard boundary — stay under 1MB for most documents.
Tip: Index Your Query Patterns, Not All Fields
Creating indexes on every field wastes RAM. Use explain() to find in-memory sorts and collection scans. Index only what your actual queries filter on.
Gotcha: No Transaction Rollback for Index Builds
Building an index on a large collection can take hours. If it fails midway, the partial index is silently discarded. Plan index builds during maintenance windows.
Senior Insight
MongoDB's text search relevance scoring is based on the TF-IDF algorithm. The $text operator returns a textScore metadata field that ranks documents by relevance. I've used $meta: 'textScore' with a $sort to return search results ordered by relevance. The limitation: you can't customize the relevance formula. If you need field-specific boosting or phrase matching, dedicated search tools are necessary.
Source: MongoDB Developer Center (https://www.mongodb.com/developer/), MongoDB Engineering Blog (https://www.mongodb.com/blog/channel/engineering-blog), Studio 3T Blog (https://studio3t.com/blog/)