extract search logic from the controller