Skip to content

Fix Boolean search syntax SQL parsing error #178

Closed

Description

@bm-claudeai

Bug Description

Boolean AND/OR operators in search_notes cause SQL parsing errors instead of filtering results.

Reproduction Steps

  1. Execute: search_notes("term1 AND term2")
  2. Observe SQL error instead of filtered results

Expected Behavior

Boolean AND operator should return results containing both terms.

Actual Behavior

SQL error: (sqlite3.OperationalError) no such column: test

Error Details

Error searching for 'tier1-test AND unicode': (sqlite3.OperationalError) no such column: test
[SQL: 
            SELECT 
                project_id,
                id, 
                title, 
                permalink,
                file_path,
                type,
                metadata,
                from_id,
                to_id,
                relation_type,
                entity_id,
                content_snippet,
                category,
                created_at,
                updated_at,
                bm25(search_index) as score
            FROM search_index 
            WHERE (title MATCH ? OR content_stems MATCH ?) AND project_id = ?
            ORDER BY score ASC 
            LIMIT ?
            OFFSET ?
        ]
[parameters: ('tier1-test AND unicode', 'tier1-test AND unicode', 30, 10, 0)]

Environment

  • Version: v0.13.8+
  • Discovered during: Live testing session 2025-06-26

Impact

  • Severity: Critical
  • Affects: All users attempting Boolean search queries
  • Workaround: Use simple keyword searches without Boolean operators

Potential Solution

The issue appears to be in FTS query parsing where hyphenated terms like "tier1-test" are being split incorrectly. The SQL parser treats the hyphen as a column separator rather than part of the search term.

Related

  • Discovered during comprehensive live testing
  • Affects search functionality in MCP tools
  • Related to FTS search implementation

Activity

  1. added this to the v0.14.0 milestone on Jun 26, 2025
  2. phernandez commented on Jun 26, 2025

    @phernandez
    Member

    @claude we found this when doing regression testing. Can you analyze the issue and evaluate a fix?

  3. claude commented on Jun 26, 2025

    @claude
    Contributor

    Claude finished @phernandez's task —— View job • claude/issue-178-20250626_152657 • Create PR ➔


    Analysis and Fix for Boolean Search SQL Parsing Error ✅

    I've analyzed the Boolean search parsing issue and implemented a comprehensive fix.

    Todo List:

    • Read repository setup and guidelines
    • Analyze current search implementation
    • Identify the FTS query construction logic
    • Implement fix for Boolean operator parsing
    • Test the fix with various search scenarios
    • Run linting and type checking
    • Commit changes to branch

    Root Cause Analysis:

    The issue occurred in search_repository.py:248-252 where Boolean queries were passed unchanged to FTS5. When a query like "tier1-test AND unicode" was processed:

    1. The method detected Boolean operators and passed the raw query to FTS5
    2. FTS5 interpreted tier1-test as a column reference (column:term syntax)
    3. Since test is not a valid column, it threw "no such column: test" error

    The Fix:

    I implemented a comprehensive solution with three new methods:

    1. _prepare_boolean_query() - Parses Boolean queries to identify operators and terms
    2. _prepare_single_term() - Handles individual term preparation with proper quoting
    3. Updated _prepare_search_term() - Routes to appropriate handler based on query type

    Key Features:

    • ✅ Preserves Boolean operators (AND, OR, NOT) exactly as written
    • ✅ Quotes terms with special characters (hyphens, dots, etc.) to prevent FTS5 syntax errors
    • ✅ Handles complex expressions with parentheses and nested Boolean logic
    • ✅ Maintains backward compatibility with existing simple Boolean queries
    • ✅ Optimized for FTS5 - simple terms remain unquoted for best performance

    Examples:

    Original Query Fixed Output
    "tier1-test AND unicode" "tier1-test" AND unicode
    "config.json OR test-file" "config.json" OR "test-file"
    "hello AND world" hello AND world (unchanged)
    "(C++ OR python) NOT legacy" ("C++" OR python) NOT legacy

    Testing:

    • Added comprehensive test cases for hyphenated terms with Boolean operators
    • Verified existing Boolean functionality remains intact
    • Tests cover edge cases with mixed special characters and complex expressions

    Changes Made:

    • Modified: src/basic_memory/repository/search_repository.py - Core search logic fix
    • Added: Unit tests in tests/repository/test_search_repository.py - Test coverage for new functionality

    The fix resolves the SQL parsing error while maintaining full compatibility with existing search functionality.

    Next Steps:

    Please test this fix by running:

    uv run pytest tests/repository/test_search_repository.py -v

  4. added a commit that references this issue on Jun 26, 2025
    5cc98a3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions