Resource collections are often enormous, and when some data has to be retrieved from them, it would be only not very efficient to always get the full list and browse it for specific items. Therefore we should design an optimized Search API.
-
Filtering:
- Narrow down query results by parameters (e.g., country, creation date).
GET /api/cars?country=Japan GET /api/cars?createDate=2019-11-11
- Narrow down query results by parameters (e.g., country, creation date).
-
Sorting:
- Sort results ascending or descending by chosen parameters (e.g., by date).
GET /api/cars?sort=createDate,asc GET /api/cars?sort=createDate,desc
- Sort results ascending or descending by chosen parameters (e.g., by date).
-
Paging:
- Use "limit" to restrict results and "offset" to specify which part of the result range to show.
GET /api/cars?limit=100 GET /api/cars?offset=2
- Use "limit" to restrict results and "offset" to specify which part of the result range to show.
Combine features:
GET /api/cars?country=Japan&sort=createDate,desc&limit=100&offset=2
Results in the list of 100 cars from Japan, sorted by creation date in descending order, starting from the second page (records 101-200).
$ git clone https://github.com/Raouf25/Spring-Boot-efficient-search-API.git
$ docker build -t="spring-boot-efficient-search-api" --force-rm=true .
This command runs Maven build to create a JAR package and builds the Docker image.
Note: Initial command may take time to download the base image from DockerHub
$ docker run -p 8080:8080 -it --rm spring-boot-efficient-search-api
$ curl localhost:8080/api/cars/1
Response:
{
"id":1,
"manufacturer":"Acura",
"model":"Integra",
"type":"Small",
"country":"Japan",
"createDate":"1931-02-01"
}
docker stop $(docker container ls | grep "spring-boot-efficient-search-api:*" | awk '{ print $1 }')
This project is deployed on https://spring-boot-efficient-search-api.fly.dev/api/cars
Try: Demo Link
Docker Image: raouf25/spring-boot-efficient-search-api
Refer to the Docker Commands section for pulling, running, and querying the API using cURL and jq.
For more details please see this medium post .
Spring Boot Efficient Search Api Copyright © 2020 by Abderraouf Makhlouf makhlouf.raouf@gmail.com