Filtering and Searching In API
Filtering and searching are essential features in REST APIs, especially when dealing with large datasets. They allow clients to retrieve only the relevant data, improving performance and usability.
We can do effective filtering, searching and sorting in the following ways -
1. Using Query Parameters
Query parameters in a REST API are key-value pairs appended to the URL after a ?
. For filtering and searching:
Standardized Syntax: Use clear and intuitive names for query parameters:
field=value
: Simple equality filter (e.g.,/users?role=admin
).field[operator]=value
: For advanced filtering using operators ([gt]
,[lt]
,[gte]
,[lte]
, etc.):Combine filters with logical operators like
AND
orOR
.
Consistent Format: Ensure naming conventions are consistent:
Use camelCase (
userName
,minPrice
) or snake_case (user_name
,min_price
) based on your API style guide.
Reserved Characters: Encode special characters properly to avoid parsing issues.
The simplest way to implement filtering is by using query parameters in the URL. Spring Boot makes it easy to handle query parameters with @RequestParam
2: Using Specifications (Dynamic Queries)
For more complex filtering, you can use Specifications with JPA Criteria API. This approach is flexible and allows you to build dynamic queries.
For more details info see Specification
3. Combining Filtering and Searching
We can combine filtering and searching in a single endpoint to provide a powerful querying mechanism.
Ex. /products?category=electronics&price[gt]=100&brand=Samsung
Using spring data specification we can combine multiple specifications -
Specification spec = Specification.where(spec1).and(spec2).or(spec3);
4. Pagination and Sorting
We can utilize effective pagination and sorting based on our need to display properly ordered data in chunks.
Query Parameter for Sorting: Use a
sort
query parameter:field
: Field to sort by (e.g.,price
).direction
: Sorting direction (asc
ordesc
).
Multiple Sort Fields: Support sorting by multiple fields:
Spring Data PagingAndSortingRepository: Use the
Pageable
interface:Example
Pageable
request:
Example -
Simple -
Filter record based on exact match
GET /api/users?role=admin
Rangle Filters
Support range queries using operators like
gt
,lt
,gte
,lte
.GET /api/products?price[gt]=100&price[lte]=500
Multiple Filters
GET /api/orders?status=delivered&date[gte]=2023-01-01&date[lte]=2023-12-31
Full Text Search
GET /api/books?search=Spring Boot
Logical Operators
GET /api/movies?filter=(genre=action AND rating[gte]=8) OR (director=Nolan)
Sorting
GET /api/products?sort=price,asc
GET /api/products?sort=price,desc&sort=name,asc
Pagination
GET /api/users?page=1&size=10
Filtering on Nested or Related Fields
GET /api/orders?customer.name=John&product.category=electronics
Retrieves orders where the
customer
’s name isJohn
and theproduct
belongs to theelectronics
category.Case-Insensitive Search
GET /api/users?email=example@example.com
Matches
example@example.com
,Example@example.com
, etc.Include/Exclude Fields
GET /api/users?fields=id,name,email
Includes only the
id
,name
, andemail
fields in the response.Reserved Characters Handling
Ensure special characters in query parameters are encoded.
/products?name=Spring%20Boot&description=advanced%2Fbeginner
Retrieves products where the
name
isSpring Boot
and thedescription
containsadvanced/beginner
.
Summary
Filtering: Use query parameters or Specifications to narrow down results.
Searching: Implement simple or advanced search using query parameters or Specifications.
Combining Filtering and Searching: Provide a unified endpoint for flexible querying.
Pagination and Sorting: Improve performance and usability by paginating and sorting results.
Last updated