Full-text search is a powerful feature that allows you to search for text within documents stored in your database. In this section, we will explore how to implement full-text search in MongoDB using the text index type. This feature is particularly useful when dealing with large volumes of textual data and requires efficient querying capabilities.
MongoDB provides a robust full-text search feature that supports searching across multiple fields within documents. The full-text search functionality is built on top of text indexes, which are designed to handle complex queries involving multiple words and phrases.
To enable full-text search on a collection, you need to create a text index. Here's how you can do it:
db.collection.createIndex(
{ "fieldName": "text" },
{
weights: { "fieldName": 10 }, // Optional: Assign weight to the field
default_language: "english", // Optional: Specify the language
name: "textIndexName" // Optional: Name for the index
}
)
Let's assume we have a collection named products with documents containing fields like name, description, and category. We want to create a text index on these fields:
db.products.createIndex(
{
name: "text",
description: "text",
category: "text"
},
{
weights: { name: 10, description: 5, category: 2 }, // Higher weight for 'name'
default_language: "english"
}
)
Once the text index is created, you can perform full-text searches using the $text operator. Here are some examples of how to use it:
To search for documents containing a specific word or phrase:
db.products.find(
{ $text: { $search: "laptop" } }
)
By default, MongoDB's full-text search is case-insensitive and diacritic-sensitive. You can modify this behavior using the $language option:
db.products.find(
{ $text: { $search: "café", $language: "fr" } }
)
MongoDB assigns a relevance score to each document based on how well it matches the search query. You can sort the results by this score:
db.products.find(
{ $text: { $search: "laptop" } },
{ score: { $meta: "textScore" } }
).sort({ score: { $meta: "textScore" } })
You can combine full-text search with other query conditions to filter results:
db.products.find(
{
$text: { $search: "laptop" },
category: "Electronics"
}
)
To perform a phrase search, enclose the phrase in double quotes:
db.products.find(
{ $text: { $search: "\"ultra high definition\"" } }
)
You can exclude certain words from your search results using the - operator:
db.products.find(
{ $text: { $search: "laptop -notebook" } }
)
MongoDB supports wildcard searches using the * character. However, this feature is limited to suffixes:
db.products.find(
{ $text: { $search: "notebo*" } }
)
Full-text search in MongoDB is a powerful tool for handling complex text queries efficiently. By leveraging text indexes and the $text operator, you can build robust search capabilities into your applications. Remember to follow best practices for index management and query optimization to ensure optimal performance.
In the next section, we will explore other advanced topics in MongoDB, including aggregation pipelines and sharding. Stay tuned!