async def search_collection(
    readable_id: str,
    query: str = Query(..., description="Search query"),
    response_type: ResponseType = Query(
        ResponseType.RAW, description="Type of response: raw search results or AI completion"
    ),
    db: AsyncSession = Depends(deps.get_db),
    current_user: User = Depends(deps.get_user),
) -> schemas.SearchResponse:
    """Search within a collection identified by readable ID.

    Args:
        readable_id: The readable ID of the collection to search
        query: The search query
        response_type: Type of response (raw results or AI completion)
        db: The database session
        current_user: The current user

    Returns:
        dict: Search results or AI completion response
    """
    try:
        return await search_service.search_with_completion(
            db,
            readable_id=readable_id,
            query=query,
            current_user=current_user,
            response_type=response_type,
        )
    except Exception as e:
        # Log the error for debugging
        import logging

        logger = logging.getLogger(__name__)
        logger.error(f"Search error for collection {readable_id}: {str(e)}")

        # Check if it's a connection error
        error_message = str(e).lower()
        if (
            "connection" in error_message
            or "refused" in error_message
            or "timeout" in error_message
        ):
            raise HTTPException(
                status_code=503,
                detail="Vector database service is currently unavailable. Please try again later.",
            ) from e
        elif "not found" in error_message:
            raise HTTPException(
                status_code=404,
                detail=f"Collection '{readable_id}' not found or you don't have access to it.",
            ) from e
        else:
            # For other errors, return a generic message but with 500 status
            raise HTTPException(
                status_code=500, detail=f"An error occurred while searching: {str(e)}"
            ) from e
Search within a collection identified by readable ID.

    Args:
        readable_id: The readable ID of the collection to search
        query: The search query
        response_type: Type of response (raw results or AI completion)
        db: The database session
        current_user: The current user

    Returns:
        dict: Search results or AI completion response
    
backend/airweave/api/v1/endpoints/collections.py