TestName
TestName

Reputation: 388

Spring Data Rest search by keyword

I'm trying to find the solution for implementing search by keyword. Final API should look like /persons/search?keyword=test. The search should check several columns of persons table (firstName, lastName, school, ...).

In the result list, I need to have the list of persons which contains keyword at least in one column.

What will be the best solution to implement that by using Spring Data Rest?

Upvotes: 1

Views: 1053

Answers (1)

tryingToLearn
tryingToLearn

Reputation: 11663

Using @Query annotation on a repository, you can do something like this:

public interface PersonRepository extends CrudRepository<Persons, Long> {

    @Query("SELECT p FROM Persons where LOWER(p.firstname) like :key%"
            + " or LOWER(p.lastname) like :key%" )
    public List<Person> searchBy(@Param("word") String key);
}

NOTE: I did a similar thing, but I do not have the exact code right now, so just check out the syntax, but what I was able to achieve with this was the ability to search on multiple columns with a single key.

Upvotes: 1

Related Questions