Spring Boot + Elasticsearch + Pagination Example
Pagination
Displaying large amounts of data in a single view can be overwhelming and hinder user experience. A common approach to address this issue is pagination, where the data is divided into smaller, manageable batches or pages. By implementing pagination, you can improve both the user interface (UI) and the overall performance of your application.
Suppose we have 100 records that need to be displayed. Instead of rendering all 100 records at once, which could result in a cluttered UI and potentially slow performance, we can divide the records into batches or pages. In our example, we have chosen a batch size of 10 records. Initially, only the first 10 records are displayed on the page. This provides a cleaner and more focused view for the user. Additionally, by only fetching and rendering 10 records at a time, we reduce the amount of data that needs to be transferred over the network and processed by the client, thereby improving the overall performance and responsiveness of your application. To navigate through the remaining records, you can include pagination controls, such as "Next" and "Previous" buttons or page numbers.
Video
This tutorial is explained in the below Youtube Video.Implementation
For our current implementation we have 10 employee records in elasticsearch. If suppose we keep the page size as 3, then we will be having 4 pages as follows-
The spring boot project we will be implementing is as follows -

The EmployeeRepository we had implemented previously, it provided CRUD operations by default. For this EmployeeRepository we will be adding a new method named findAll to which we pass a Pageable instance. When called with a Pageable instance, Spring Data Elasticsearch will automatically construct the appropriate Elasticsearch query, execute it against the index, and return a Page<Employee> object containing the paginated result set.
package com.javainuse.boot_elasticsearch_crud.repository; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; import com.javainuse.boot_elasticsearch_crud.model.Employee; public interface EmployeeRepository extends ElasticsearchRepository<Employee, String> { Page<Employee> findAll(Pageable pageable); }
package com.javainuse.boot_elasticsearch_crud.service; import java.util.List; import org.springframework.data.domain.Page; import com.javainuse.boot_elasticsearch_crud.exception.EmployeeNotFoundException; import com.javainuse.boot_elasticsearch_crud.model.EmployeeDto; public interface EmployeeService { EmployeeDto createEmployee(EmployeeDto employeeDto); EmployeeDto getEmployeeById(String employeeId) throws EmployeeNotFoundException; List<EmployeeDto> getEmployees(); void deleteEmployee(String employeeId) throws EmployeeNotFoundException; EmployeeDto updateEmployee(EmployeeDto employeeDto) throws EmployeeNotFoundException; Page<EmployeeDto> getPaginatedEmployees(int page, int size); }