Prerequisites
- Install the Morphik SDK:
pip install morphik- Provide credentials via Morphik URI
- Basic understanding of document ingestion
1. Ingest Documents with Rich Typed Metadata
Morphik supports various metadata types for sophisticated filtering:2. Build Complex Filters
Combine multiple operators to create sophisticated queries:Filtering by Folder Name
Documents ingested with afolder_name parameter can be filtered using that value in metadata. This enables cross-folder queries and pattern matching:
3. List Documents with Filters
Find documents matching your criteria:4. Retrieve Chunks with Filters
Get document chunks that match your metadata filters:Supported Filter Operators
| Operator | Description | Example |
|---|---|---|
$eq | Exact match | {"status": {"$eq": "active"}} |
$in | Value in array | {"region": {"$in": ["andes", "altiplano"]}} |
$gte | Greater than or equal | {"date": {"$gte": "2024-01-01"}} |
$lte | Less than or equal | {"score": {"$lte": 45}} |
$gt | Greater than | {"temperature": {"$gt": 0}} |
$lt | Less than | {"count": {"$lt": 100}} |
$contains | Array contains value | {"tags": {"$contains": {"value": "urgent"}}} |
$and | All conditions must match | {"$and": [condition1, condition2]} |
$or | Any condition must match | {"$or": [condition1, condition2]} |
Use Cases
Complex metadata filtering is ideal for:- Document management systems with multi-dimensional categorization
- Compliance and audit systems requiring date-based queries
- Scientific data repositories with measurements and precise numerical filtering
- Multi-tenant applications with scope-based isolation
- Time-series document collections with date range queries
- Hierarchical data with nested metadata structures
Best Practices
1. Use Appropriate Types
Use the correct Python types for metadata:2. Convert Dates for Filtering
Always convert date objects to ISO format when building filters:3. Combine Operators Strategically
- Use
$andfor required conditions that must all match - Use
$inwhen a field can have multiple possible values - Use range operators (
$gte,$lte) for numerical and date filtering - Use
$containsfor array membership checks
4. Index Important Fields
Frequently filtered fields benefit from proper indexing. Consider performance when adding many metadata fields.Running the Example
Related Cookbooks
- Generating Completions with Retrieved Chunks - Send filtered chunks to OpenAI
- Python SDK Basic Operations - Core Morphik operations

