Refactor category related code in search