Refactor esearch-related codes