In MongoDB, full-text search capabilities enable applications to locate and retrieve documents based on specific keywords or phrases, making it a highly useful feature for handling large volumes of text-based data.
Full-text search is a technique used to search for specific terms within a database. It allows applications to retrieve records containing a particular word or phrase, making it useful for finding documents based on their textual content.
In MongoDB, full-text search:
To enable full-text search in MongoDB, we must create text indexes on the fields containing text data. Text indexes allow MongoDB to search for and quickly retrieve documents with specific keywords.
To create a text index on a single field, such as title
, in a books
collection:
db.books.createIndex({ title: "text" });
Here we specify the field name title
and set its index type to "text"
, enabling full-text search on this field.
You can create a text index on multiple fields to search across different text-based fields in the same query.
Suppose we want to index both title
and description
fields:
db.books.createIndex({ title: "text", description: "text" });
This allows searches across both title
and description
fields in a single query.
To search for documents containing a specific term, use the $text
operator. For instance, to search for books with the word “database”:
db.books.find({ $text: { $search: "database" } });
This query retrieves all documents in the books
collection where the term “database” appears in either the title
or description
field (if both are indexed).
MongoDB offers advanced search capabilities with the $text
operator, including phrase search, logical operators, and exclusion of terms.
Phrase search enables us to search for exact phrases. Enclose the phrase in double quotes:
db.books.find({ $text: { $search: "\"NoSQL databases\"" } });
This query will only match documents where the phrase “NoSQL databases” appears together as a complete phrase.
When you provide multiple terms, MongoDB uses OR logic by default.
db.books.find({ $text: { $search: "database NoSQL" } });
This retrieves documents containing either “database” or “NoSQL” in the indexed fields.
To require that all terms appear in the results, enclose each term in double quotes:
db.books.find({ $text: { $search: "\"database\" \"NoSQL\"" } });
This query will only retrieve documents where both “database” and “NoSQL” are present.
To exclude specific terms from the results, prefix them with a minus sign (-
):
db.books.find({ $text: { $search: "database -NoSQL" } });
This retrieves documents containing “database” but not “NoSQL.”
MongoDB assigns a relevance score to each document when performing a text search. Documents with higher scores are considered more relevant to the query terms.
To include the score in query results, use the $meta
operator:
db.books.find(
{ $text: { $search: "database" } },
{ score: { $meta: "textScore" } }
);
You can also sort results by relevance score by using $meta
with sort()
:
db.books.find(
{ $text: { $search: "database" } },
{ score: { $meta: "textScore" } }
).sort({ score: { $meta: "textScore" } });
The query returns documents with an additional score
field indicating their relevance. The sort()
method orders documents from the highest to the lowest score, with the most relevant documents at the top.
MongoDB’s text search supports multiple languages. By default, it’s set to English but can be customized.
To create a text index for a specific language, use the default_language
option:
db.books.createIndex(
{ title: "text", description: "text" },
{ default_language: "spanish" }
);
You can also set the language for individual search queries:
db.books.find({ $text: { $search: "base de datos", $language: "spanish" } });
This query searches for the Spanish term “base de datos” in the title
and description
fields.
Let’s build a basic web application search using MongoDB’s full-text search.
1. Front-End (JavaScript): Capture the search input from the user.
2. JavaScript Function: Define a function to send the search request to the backend.
function searchBooks() {
const query = document.getElementById("searchQuery").value;
fetch(`/search?query=${query}`)
.then(response => response.json())
.then(data => displayResults(data));
}
function displayResults(data) {
let resultsDiv = document.getElementById("results");
resultsDiv.innerHTML = "";
data.forEach(book => {
resultsDiv.innerHTML += `${book.title}: ${book.description}
`;
});
}
3. Back-End (Node.js with Express): Implement a search endpoint that uses MongoDB’s full-text search.
const express = require("express");
const { MongoClient } = require("mongodb");
const app = express();
const client = new MongoClient("mongodb://localhost:27017");
const dbName = "library";
app.get("/search", async (req, res) => {
try {
await client.connect();
const db = client.db(dbName);
const query = req.query.query;
const books = await db.collection("books").find(
{ $text: { $search: query } },
{ score: { $meta: "textScore" } }
).sort({ score: { $meta: "textScore" } }).toArray();
res.json(books);
} finally {
await client.close();
}
});
app.listen(3000, () => console.log("Server started on port 3000"));
/search
endpoint accepts a query, searches the books
collection using $text
, and sorts results by relevance.displayResults()
function outputs the search results on the front end.While MongoDB’s built-in text search is powerful, it has limitations:
If your application requires advanced full-text search features, consider using MongoDB Atlas Search, which integrates directly with MongoDB and provides a more robust search engine.
Full-text search capabilities in MongoDB provide a powerful way to integrate search functionality directly into your applications. By using text indexes, relevance scoring, and advanced search operators, you can create efficient and user-friendly search solutions. MongoDB’s built-in text search is suitable for basic to moderately complex search requirements. Happy Coding!❤️