Skip to content

Add structured query filters to search command#7

Merged
patrick91 merged 2 commits intomainfrom
feature/structured-query-filters
Jan 18, 2026
Merged

Add structured query filters to search command#7
patrick91 merged 2 commits intomainfrom
feature/structured-query-filters

Conversation

@patrick91
Copy link
Copy Markdown
Owner

Summary

Implements structured query filters for the search command, enabling precise searches beyond FTS5 full-text queries. This feature is essential for agent-friendly usage and programmatic querying of the Gmail database.

Features

New Filter Options

All filters are optional and combine with AND logic:

  • --from <email> - Filter by sender (partial match)
  • --to <email> - Filter by recipient (partial match)
  • --after <date> - Messages after date (supports "today", "yesterday", ISO dates)
  • --before <date> - Messages before date
  • --has - Messages with attachments
  • --label <label> - Filter by Gmail label
  • --subject <text> - Filter by subject (partial match)

Usage Examples

# Filter by sender
gmail search --from alice@example.com

# Filter by date range
gmail search --after 2024-01-01 --before 2024-12-31

# Messages with attachments from specific sender
gmail search --from finance@ --has

# Combine FTS5 query with filters
gmail search "invoice" --from finance@ --after today

# Filter by label and date
gmail search --label INBOX --after yesterday

Implementation Details

  • New module: src/gmail_cli/db/filters.py - Contains SearchFilters dataclass, date parsing, and SQL building
  • Enhanced queries: Updated search_messages() to handle three scenarios:
    1. FTS5 query only (original behavior)
    2. Filters only (new)
    3. FTS5 + filters combined (new)
  • Security: Uses parameterized queries to prevent SQL injection
  • JSON support: Handles JSON fields (labels) with SQLite's json_each() function
  • Performance: Applies indexed filters first (from_addr, date_utc) for efficiency
  • Backward compatible: Existing search commands work unchanged

Test Coverage

Added comprehensive test suite with 30 tests:

  • ✅ Date parsing (today, yesterday, ISO formats, invalid input)
  • ✅ SQL filter building (individual and combined filters)
  • ✅ End-to-end search functionality (all filter combinations)
  • ✅ Edge cases (no results, partial matches, date ranges)

All tests passing with 100% coverage of filter functionality.

Dependencies

  • Added python-dateutil>=2.8.0 for flexible date parsing
  • Added pytest>=7.0.0 as dev dependency for testing

Why This Matters

This feature enables:

  • Agent integration: LLMs can now perform precise queries programmatically
  • Advanced filtering: Users can narrow down results beyond text matching
  • Date-based searches: Essential for finding recent or historical messages
  • Attachment filtering: Quickly find messages with files
  • Label-based queries: Leverage Gmail's organizational structure

Closes part of the roadmap for making gmail-cli agent-friendly.

Implement structured filters for the search command to enable precise
searches beyond FTS5 full-text queries. Agents and users can now filter
by sender, recipient, date range, attachments, labels, and subject.

New filter options:
- --from: Filter by sender email (partial match)
- --to: Filter by recipient email (partial match)
- --after/--before: Date range filtering (supports "today", "yesterday", ISO dates)
- --has: Filter messages with attachments
- --label: Filter by Gmail label (INBOX, IMPORTANT, etc.)
- --subject: Filter by subject text (partial match)

Implementation details:
- Created filters module with SearchFilters dataclass and SQL builder
- Enhanced search_messages() to support filters alongside FTS5 queries
- Uses parameterized queries for SQL injection protection
- Handles JSON fields (labels) and indexed fields (from_addr, date_utc)
- Backward compatible - existing search commands work unchanged

Examples:
  gmail search --from alice@example.com
  gmail search "invoice" --from finance@ --after 2024-01-01
  gmail search --has --label INBOX

Tests:
- 30 comprehensive tests covering date parsing, filter building, and search
- All tests passing with 100% coverage of filter functionality
Add CI workflow that runs tests on every push and pull request to main
branch. The workflow:
- Runs on Ubuntu with Python 3.12
- Installs project dependencies including dev dependencies
- Executes pytest test suite
- Generates coverage report

This ensures all tests pass before merging changes.
@patrick91 patrick91 merged commit 7c0b74c into main Jan 18, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant