r/SpringBoot • u/Future_Badger_2576 • Feb 01 '26
Question How do you manage complex dynamic SQL queries and filters in a Spring Boot search API?
I am building a restaurant search API in Spring Boot where users can search by either restaurant name or menu item name, along with optional filters like geolocation, rating, and cuisine.
On the database side, I’m using PostgreSQL(ParadeDB) with PostGIS for geospatial queries and fuzzy search. The search itself is fairly advanced, it calculates distance, ranks results by relevance using fuzzy matching, aggregates cuisines, and supports pagination. Because of this, the SQL query has become quite complex.
Right now, I am using a native query with projections. Since many of the request parameters are optional, I am handling all possible combinations inside a single SQL query using conditional clauses ,but the query is getting harder to read and maintain as more filters are added.
I have looked into JPA Specifications and the Criteria API, but they don’t seem like a good fit here.
I am trying to figure out what the “best practice” approach is in this situation. Is sticking with a native query and projections the right call for a search use case like this?
I’d love to hear how others have handled advanced search with lots of optional filters in JPA-heavy applications, especially when full-text or fuzzy search and geospatial queries are involved.
My sample SQL: gist
5
u/Sheldor5 Feb 01 '26
that's what the Criteria API is for
https://jakarta.ee/learn/docs/jakartaee-tutorial/current/persist/persistence-criteria/persistence-criteria.html
2
u/as5777 Feb 01 '26
The criteria api is the best option for dynamic queries.
You query can be translated easily (ask Claude)
3
u/mgalexray Feb 01 '26 edited Feb 01 '26
By using JooQ. I’ve tried criteria api, querydsl and few others but honestly JPA in general is not so great for those kinds of read paths. It just ends up as unmaintainable mess. Don’t torture yourself.
3
u/maethor Feb 01 '26
Probably not the answer you're looking for, but for a place I used to work at the answer was "stop trying to do this with Hibernate and SQL and load the data into SOLR instead".
2
u/Entropjy Feb 01 '26
https://github.com/perplexhub/rsql-jpa-specification for the client side filter, with a careful allowlist of properties of the data is sensitive. And the user supplied filter with security rules for tenancy or resource ownership of its simple enough, or use opa partial/ Cerbos plan resources where it's complex. The only places I find this approach lets me down is complex aggregations. That's when I pivot to jooq, just for those cases
1
u/revilo-1988 Feb 01 '26
Unfortunately, the criteria API is one of the worst APIs imaginable for larger queries; you'd much rather have something like
query dsl http://querydsl.comorBlaze-Persistence https://persistence.blazebit.com. I hope that JPA will get something likeLINQ queryin the future.1
u/gizmogwai Feb 01 '26
You can have a look at JOOQ. This is likely the best option for your use case while still providing some type safety.
1
u/void0xnull Feb 01 '26
I use spring-filter