Optimal rates for spectral algorithms with least-squares regression over Hilbert spaces