Restaurant Search Service

In our last lesson, we created repository methods to search for restaurants in various ways.

Now, we'll build the service layer that will use these repository methods to provide an API for searching restaurants.

The Restaurant Service Interface

Let's start by adding the search method to our service interface. This defines the contract that our service implementation will follow.

public interface RestaurantService { // ... Page<Restaurant> searchRestaurants( String query, Float minRating, Float latitude, Float longitude, Float radius, Pageable pageable ); }

Implementing the Search Service

Now we'll implement the search functionality in our service layer, handling different search scenarios based on the provided parameters.

@Service @RequiredArgsConstructor public class RestaurantServiceImpl implements RestaurantService { private final RestaurantRepository restaurantRepository; // ... @Override public Page<Restaurant> searchRestaurants( String query, Float minRating, Float latitude, Float longitude, Float radius, Pageable pageable) { // If just filtering my min rating if(null != minRating && (null == query || query.isEmpty())) { return restaurantRepository.findByAverageRatingGreaterThanEqual(minRating, pageable); } // Normalize min rating to be used in other queries Float searchMinRating = minRating == null ? 0f : minRating; // If there's a text, search query if (query != null && !query.trim().isEmpty()) { return restaurantRepository.findByQueryAndMinRating(query, searchMinRating, pageable); } // If there's a location search if (latitude != null && longitude != null && radius != null) { return restaurantRepository.findByLocationNear(latitude, longitude, radius, pageable); } // Otherwise we'll perform a non-location search return restaurantRepository.findAll(pageable); } }

Summary

  • Defined the searchRestaurants method in the service interface
  • Integrated fuzzy text search for flexible matching
  • Added support for rating-specific searches
© 2026 Devtiro Ltd. All rights reserved