To implement search in a Spring Boot application, start with Spring Data JPA derived queries or @Query for simple filters, move to JpaSpecificationExecutor for optional criteria, and always return a Page using Pageable. When you need relevance ranking, typo tolerance or large text corpora, add a database full-text index or a search engine such as Elasticsearch or OpenSearch.
The examples use Spring Boot 3.x with the jakarta.persistence package. In older code the same annotations lived in javax.persistence.
A Product entity is enough to show every technique.
Use BigDecimal for money, not Double, to avoid rounding errors.
Spring Data builds a query from the method name, which suits fixed searches.
Use named parameters with concat as shown, and do not build the query by concatenating user input into a string. That keeps the query parameterised and protects against SQL injection. Be aware that a LIKE '%text%' pattern with a leading wildcard cannot use a normal B-tree index, so it scans the table.
Return a Page so the client knows the total count and can request the next page. Spring MVC resolves Pageable from page, size and sort request parameters.
A request such as GET /products/search?keyword=phone&page=0&size=10&sort=price,desc returns the first ten matches ordered by price. Set spring.data.web.pageable.max-page-size to stop clients from asking for huge pages. Expose a DTO instead of the entity in a real API.
When filters are optional, a method per combination becomes unmanageable. Let the repository extend JpaSpecificationExecutor and compose predicates.
Each missing parameter becomes a conjunction() (always true), so the query only contains the filters the client sent. A user-supplied % or _ is still a wildcard in LIKE, so escape them if exact matching matters.
LIKE has no ranking, no stemming and no typo tolerance, and it gets slow on large tables. Choose the next step by need:
| Need | Option |
|---|---|
| Word search with ranking on PostgreSQL | tsvector column with a GIN index, called through a native query |
| Prefix search | An index on the lowercase column, with like 'abc%' |
| Typo tolerance, facets, large data | Elasticsearch or OpenSearch (Spring Data Elasticsearch) |
| Search inside JPA entities | Hibernate Search with a Lucene or Elasticsearch backend |
A PostgreSQL example through a native query:
Create a matching GIN index on the same expression, or the database will compute the vector for every row.
Use IgnoreCase in a derived query name, or lower(...) on both sides in JPQL or Criteria, as shown above. Some databases and collations are already case-insensitive.
A leading wildcard in LIKE '%text%' prevents index use. Add pagination, search only the columns you need, and move to full-text search or a search engine.
Combine them with or in a @Query or in a Specification. Index the columns you filter on most.
Prefer a DTO or a projection. It avoids lazy-loading surprises and exposes only the fields a client needs.
Use derived queries or parameterised @Query methods for fixed searches, Specification for optional filters, and Pageable for every list endpoint. Keep queries parameterised, remember that leading-wildcard LIKE does not scale, and adopt PostgreSQL full-text search or Elasticsearch when you need ranking and speed.