How do you handle duplicate documents during RAG ingestion?
Duplicate documents during RAG ingestion should be detected and controlled before they create repeated chunks in the index.
If the same content is indexed multiple times, retrieval can become biased toward that content, waste storage, and return several nearly identical passages instead of diverse evidence.
1. Assign a stable document identity
Each source should have a stable identifier whenever possible.
Examples include:
- Document ID
- Canonical URL
- Repository path
- Database record ID
- Storage object key
This helps the ingestion pipeline recognize that a document has already been processed.
2. Use content hashes for exact duplicates
A hash can be generated from the file or normalized document content.
If two documents produce the same hash, they are likely exact duplicates.
This works well when the same file is uploaded more than once under different names.
3. Normalize before comparing
Small formatting differences can make otherwise identical documents look different.
Before comparing content, it can be useful to normalize harmless differences such as:
- Extra whitespace
- Line-ending differences
- Repeated headers or footers
- Encoding artifacts
Normalization should not remove meaningful content.
4. Detect near-duplicates separately
Two documents may contain almost the same information but differ slightly because of:
- Minor edits
- Different formatting
- Added disclaimers
- Updated dates
- Export differences
Near-duplicate detection may use similarity techniques rather than exact hashes.
Possible approaches include:
- Text similarity
- Fingerprinting
- MinHash or locality-sensitive hashing
- Embedding similarity
Near-duplicate detection should be used carefully because two similar documents may still represent different valid versions.
5. Distinguish duplicates from newer versions
A changed document should not automatically be discarded as a duplicate.
Compare version information such as:
- Version number
- Last-modified time
- Effective date
- Source revision ID
- Content hash
If the new document is an updated version, the system may replace, deactivate, or supersede the older indexed content instead of storing both as active copies.
6. Avoid duplicate chunks
Even when document-level deduplication is in place, chunk-level duplication can still occur.
For example, repeated legal notices, headers, footers, or template sections may appear across many files.
These repeated chunks can often be removed or marked as boilerplate before indexing.
7. Keep provenance
If identical content legitimately appears in several sources, removing every copy may lose useful source information.
One option is to store the content once while preserving references to all relevant source documents.
8. Define a clear ingestion policy
A production system should decide what happens when a duplicate is detected.
Common policies include:
- Skip exact duplicates
- Replace the existing document
- Create a new version
- Keep both but mark one inactive
- Merge source references
The correct choice depends on how the knowledge base handles history and source authority.
Incoming document
↓
Stable ID / canonical source check
↓
Content hash
↓
Exact duplicate? → Skip or merge
↓
Near duplicate / new version?
↓
Apply version policy
↓
Chunk + indexFor handling document revisions over time, see “How do you handle document updates and versions in a RAG knowledge base?”.
In short, handle duplicates with stable source identifiers, content hashes, careful similarity checks, and explicit versioning rules so repeated content does not unnecessarily pollute the RAG index.
Learning Sequence
Retrieval-Augmented Generation Curriculum
How do you handle document updates and versions in a RAG knowledge base?
Explore Related Questions
Deepen your understanding across related Retrieval-Augmented Generation topics.