Text Index
MongoDB 🟡 MidDefinition
Index special pour la recherche full-text sur des champs textuels. Supporte le stemming, les stop words et les poids par champ. Une seule text index par collection.
Analogie
Un moteur de recherche interne qui comprend les variantes des mots comme un humain.
Exemple de code
db.articles.createIndex(
{ title: 'text', body: 'text' },
{ weights: { title: 10, body: 1 } }
);
db.articles.find({ $text: { $search: 'mongo' } });
Cas d'usage
Implementer une barre de recherche sur un blog ou un catalogue de produits.
Anti-pattern
Utiliser $regex pour la recherche textuelle au lieu du text index, bien plus lent.