Home > Enterprise >  Spring Boot search is not working properly
Spring Boot search is not working properly

Time:09-16

Here I want to retrieve Hotels by hotel name, but it is only returning the Hotels which have first letter which I have included.

I have search Hotel name as "Sigiriya", but when I type "s" it's resulting Sigiriya. But when I start typing more ("igiriya"), the results were gone.

/**
     *
     * @param searchkey
     * @return - List of hotels
     */
    public List<Hotel> searchHotelByName( String searchkey){
        searchkey = searchkey.toLowerCase();
        searchkey = "%"   searchkey   "%";

        return hotelRepository.findAllByHotelNameContentLike( searchkey );
    }

CodePudding user response:

I think the reason is even though you are converting the search key to lower case, your database is containing upper case letters. Therefore use ignore case search.

/**
     *
     * @param searchkey
     * @return - List of hotels
     */
    public List<Hotel> searchHotelByName( String searchkey){
        searchkey = searchkey.toLowerCase();

        return hotelRepository.findAllByHotelNameIgnoreCaseContaining( searchkey );
    }

CodePudding user response:

Use the contains JPA keyword

public List<Hotel> searchHotelByName( String searchkey){
    return hotelRepository.findAllByHotelName IgnoreCaseContaining( searchkey );
}

contains takes care of % etc.

  • Related