MongoDB结合Apache Solr:是否需索引全集合?部分索引后能否获完整文档?
1. Do you need to index the entire MongoDB collection into Solr?
Absolutely not. The whole point of using Solr is to index only the fields you need for search, filtering, or sorting.
If your use case is field-based search (e.g., querying by product_id, category) and full-text search (e.g., searching within a description field), there’s no reason to import every field from your MongoDB documents. Indexing unnecessary fields will:
- Waste Solr’s storage space
- Slow down indexing and query performance
- Create unnecessary data redundancy
For example, if your MongoDB docs contain large binary fields (like images) or internal metadata that never needs to be searched, you can safely exclude them from Solr indexing.
2. Can you retrieve the full document via Solr queries if only partial fields are indexed?
Short answer: Not directly from Solr, but there’s a standard, efficient workaround:
- Store a unique identifier in Solr: Include MongoDB’s
_id(or another unique field) in your Solr index. This field doesn’t need to be indexed for search (though you can setindexed="true"if you ever need to query by ID), but it must bestored="true"so it’s returned in search results. - Fetch the full document from MongoDB: When you get matching results from Solr, take the
_idvalues and run a query against MongoDB to retrieve the complete, original documents.
This approach is widely used because it keeps Solr lean and fast (only handling search-related workloads) while giving you access to the full data when needed.
If you absolutely need Solr to return the full document directly, you’d have to index/store all fields in Solr—but this defeats the purpose of using Solr efficiently and isn’t recommended for most use cases.
Quick Best Practices
- Design your Solr schema intentionally: Only define fields that serve a search/filter/sort purpose. For fields you need to display but don’t need to search, consider storing them in Solr (with
indexed="false") or just fetching them from MongoDB post-search. - Use targeted synchronization: Tools like the MongoDB Connector for Solr or custom scripts let you sync only specific fields between MongoDB and Solr, avoiding full-collection imports.
- Maintain data consistency: Use MongoDB Change Streams to trigger real-time updates to your Solr index whenever documents are added/updated/deleted in MongoDB.
内容的提问来源于stack exchange,提问作者chandresh.v

