Information retrieval finds relevant information in stored data in response to a query. In document collections, that can mean ranking text by relevance rather than requiring an exact-word match. In retrieval-augmented generation (RAG), it means finding external information that a language model can use when composing an answer. section 11.0 section 14.0
Choose a retrieval method for the information need:
- Use keyword and Boolean searches to find terms and narrow results with AND, OR, or NOT. section 11.0
- Use fuzzy or wildcard search when spelling or word forms vary; use phrase or proximity search when wording and context matter. section 11.0
- Use range and faceted search to restrict and categorize results by attributes, such as dates or product features. section 11.0
- Use vector search to retrieve relevant data for RAG when generating answers grounded in external information. section 1.0 section 14.0
Implementation affects operations. MongoDB’s basic $text index may be insufficient for complex searches across nested fields. A separate search engine adds data replication, monitoring, maintenance, and possible consistency problems. Atlas Search instead integrates Apache Lucene-based full-text search with MongoDB Atlas; its inverted indexes map terms to document locations, while the mongot process keeps search indexes updated through change streams. section 11.1 section 11.8
Figure 11.1 High-level data synchronization architecture. The application uses separate drivers and query languages to interact with the database and search clusters. Data is replicated from the database cluster to the search cluster to keep the index updated. This setup adds complexity, increases resource usage, and can lead to consistency issues. (Image © MongoDB 2024 CC BY-NC-SA 3.0)
The synchronization diagram shows why a separate database and search cluster require additional drivers, queries, and replication—and where consistency risks arise. section 11.1
Retrieve and rank local inspection records
In a logged-in local Atlas cluster, create a dynamic search index with db.inspections.createSearchIndex("LocalSearchIndex", { mappings: { dynamic: true } }), then check getSearchIndexes() for readiness. Run an aggregation using $search to query the result and business_name fields for “No Violation Issued,” allowing two edits for typos. Apply $match for the desired sector and city, add the searchScore, sort by that score, and limit output to three records. Drop the index with dropSearchIndex() when finished. Dynamic mapping suits this test environment; selective static mappings can avoid indexing unwanted fields. section 13.5.1 section 11.8
Listing 13.2 Executing a full-text search locally
db.inspections.aggregate([
{
$search: { #1
index: 'LocalSearchIndex', #2
text: {
query: 'No Violation Issued', #3
path: ['result', 'business_name'], #4
fuzzy: {
maxEdits: 2 #5
}
}
}
},
{
$match: { #6
sector: 'Cigarette Retail Dealer - 127', #7
'address.city': 'RIDGEWOOD' #8
}
},
{
$addFields: { #9
score: { $meta: "searchScore" }
}
},
{
$sort: { #10
score: -1
}
},
{
$limit: 3 #11
},
{
$project: { #12
_id: 0, #13
business_name: 1,
certificate_number: 1,
date: 1,
result: 1,
'address.street': 1,
'address.number': 1,
'address.zip': 1,
score: 1 #14
}
}
])
The complete aggregation illustrates how fuzzy retrieval, structured filters, relevance ranking, and projection work together. section 13.5.1










