Text search is a powerful feature that allows you to perform full-text searches on string content within your documents. This guide will walk you through the basics of setting up and using text search in MongoDB, including creating text indexes, performing text queries, and understanding best practices.
In MongoDB, text indexes support searching for strings in a collection. Unlike other types of indexes, text indexes are designed to handle full-text searches efficiently. They tokenize the indexed fields into words, remove stop words (common words like "the" or "and"), and store the tokens in a way that allows for efficient search operations.
To enable text search on a collection, you need to create a text index on one or more fields. Here's how you can create a text index:
db.collection.createIndex({ field1: "text", field2: "text" })
This command creates a text index on field1 and field2. You can include as many fields as needed in the index.
Suppose you have a collection named products with documents like this:
{
"_id": ObjectId("..."),
"name": "Wireless Mouse",
"description": "A high-performance wireless mouse for gaming and office use."
}
To create a text index on the name and description fields, you would run:
db.products.createIndex({ name: "text", description: "text" })
Once you have created a text index, you can perform text searches using the $text operator. This operator allows you to search for words in the indexed fields.
To perform a basic text search, use the following syntax:
db.collection.find({ $text: { $search: "searchTerm" } })
For example, to find all products containing the word "mouse":
db.products.find({ $text: { $search: "mouse" } })
Text searches in MongoDB are case insensitive and diacritic insensitive by default. This means that searching for "Mouse" or "møuse" will return the same results.
To search for an exact phrase, enclose the phrase in double quotes:
db.products.find({ $text: { $search: "\"wireless mouse\"" } })
This query will only return documents where the exact phrase "wireless mouse" appears.
MongoDB automatically assigns a score to each document that matches the text search criteria. The score is based on how well the document matches the search terms, with higher scores indicating better matches. You can sort the results by this score:
db.products.find({ $text: { $search: "mouse" } }).sort({ score: { $meta: "textScore" } })
This query sorts the results in descending order of relevance.
MongoDB supports text search for multiple languages. You can specify the language when creating a text index to enable language-specific tokenization and stop words:
db.products.createIndex({ name: "text", description: "text" }, { default_language: "english" })
This example sets the default language to English, which affects how words are tokenized and which stop words are removed.
Text search is a powerful feature in MongoDB that allows you to perform efficient full-text searches on your data. By creating text indexes and using the $text operator, you can quickly find documents containing specific words or phrases. Understanding how to create and manage text indexes, as well as best practices for optimizing performance, will help you make the most of this feature in your applications.
Remember to regularly review and optimize your text indexes to ensure they meet the needs of your application while maintaining optimal performance.