<p>In this article of the <strong><a href="https://dineshonjava.com/spring-data-solr-tutorial/">Spring Data Solr</a></strong> Pagination, we will discuss how to paginate Solr query results in the spring application. Spring Data provides <em>PageRequest</em> class which extends <em>AbstractPageRequest</em> and this abstract class implements the <em>Pageable</em> interface. We can query to fetch Solr results in pagination by using <em>PageRequest</em> class from the service layer.</p>
<h2>Using Spring Data Solr Pagination</h2>
<p>Spring Data provides a generic way of pagination implementation in the spring application for all Spring Data implementation such as Spring Data Solr, Spring Data Cassandra, Spring Data JPA etc. But in this article, we will look into pagination implementation by using the Spring Data Solr.</p>
<p><img class="wp-image-4171 size-medium alignright" src="https://dineshonjava.com/wp-content/uploads/2018/06/Spring-Boot-Solr-Example-300x93.jpg" alt="Spring Data Solr Pagination Example" width="300" height="93" /></p>
<h3>Creating PageRequest object for pagination</h3>
<p>Since Spring Data 2.0, creating a new <em>PageRequest</em> object using constructors is deprecated. Instead of the <em>PageRequest</em> constructor, we can use <em>of()</em> static method as a factory method to create a <em>PageRequest</em> object. In a <em>PageRequest</em> object, pages are zero indexed, thus providing 0 for a page will return the first page.</p>
<p>We can create the <em>PageRequest</em> objects by using several provided overloaded version of the<em> of()</em> method. This object fulfils the pagination requirements as following:</p>
<h3><strong>Requirement 1</strong></h3>
<p>If you want to create a <em>PageRequest</em> object to specify a query results belonging to a single page. Then we have to create the <em>PageRequest</em> object as the following code snippet:</p>
<pre class="highlight">public static PageRequest of(int page, int size) { 
 //implementation 
} 
 
//In-service class get the query results belonging to the first page when page size is 10. 
PageRequest.of(0, 10) 
</pre>
<h3><strong>Requirement 2</strong></h3>
<p>Another way, if you want to create a <em>PageRequest</em> object to specify the results belonging to a single page but the query results are sorted by using the value of a single field, we have discussed about <strong><a href="https://dineshonjava.com/spring-data-solr-sorting-with-example/">Spring Data Solr Sorting in the previous article</a></strong>. Let&#8217;s see the following code snippet to create <em>PageRequest</em> object:</p>
<pre class="highlight">public static PageRequest of(int page, int size, Sort sort) { 
 //implementation 
} 
 
//In-service class get the query results belonging to the first page when page size is 10. 
//Query results are sorted in descending order by using a price field. 
PageRequest.of(0, 10 Sort.Direction.DESC, "price") 
</pre>
<h3><strong>Requirement 3</strong></h3>
<p>In this requirement, if you want to create a <em>PageRequest</em> object to specify the query results belonging to a single page but the query results are sorted by using multiple fields and sort order of different fields is same. Let&#8217;s see the following code snippet to create <em>PageRequest</em> object:</p>
<pre class="highlight">public static PageRequest of(int page, int size, Direction direction, String... properties) { 
 //implementation 
} 
 
//In-service class get the query results belonging to the first page when page size is 10. 
//Query results are sorted in descending order by using oid and odesc fields. 
PageRequest.of(0, 10 Sort.Direction.DESC, "oid", "odesc") 
</pre>
<h3><strong>Requirement 4</strong></h3>
<p>This requirement is very suitable when we want to create a <em>PageRequest</em> object to specify the query results belonging to a single page when the query results are sorted by using multiple fields and the sort order of different fields is not same. Let&#8217;s see the following code snippet to create <em>PageRequest</em> object:</p>
<pre class="highlight">public static PageRequest of(int page, int size, Direction direction, String... properties) { 
 //implementation 
} 
 
//In-service class get the query results belonging to the first page when page size is 10. 
//Query results are sorted in descending order by using oid field and ascending order by using odesc field. 
Sort sort = Sort.by(Sort.Direction.DESC, "oid").and(Sort.by(Sort.Direction.ASC, "odesc")) 
PageRequest.of(0, 10, sort) 
</pre>
<p>We have discussed how to create a new <em>PageRequest</em> object using several flavours of the <em>of()</em> method of <em>PageRequest</em> class. The Solr query with the <em>Pageable</em> object return the results with following return types.</p>
<h2>A return type of a Paginated Solr Query</h2>
<p>In the Spring Data Solr. if you use a query method with pagination, it can have query result into two types. Let&#8217;s see these return types of the paginated query method.</p>
<ul>
<li><strong>Page<;Order>;</strong>: If you want pagination metadata along with query result then you have to use this return type. Here the Page is an interface of the Spring Data module and it declares the methods used to fetch pagination metadata in the application.</li>
<li><strong>List<;Order>;</strong>: If you want the only content of results of the paginated Solr query then you have to use this return type</li>
</ul>
<p>Let&#8217;s see an example of a Spring Data repository with the pagination implementation.</p>
<h2>Spring Data Solr Repository Example</h2>
<p>In the following repository interface, we have created a repository interface with query generation from the query method.</p>
<h3><strong>Query Generation From Method Name</strong></h3>
<p>In this step, we will explain how to create a query from the method name strategy. Let’s see the following code:</p>
<pre class="highlight">package com.doj.app.repository; 
 
import org.springframework.data.domain.Page; 
import org.springframework.data.domain.Pageable; 
import org.springframework.data.solr.repository.SolrCrudRepository; 
 
import com.doj.app.pojo.Order; 
 
/** 
 * @author Dinesh.Rajput 
 * 
 */ 
public interface SolrOrderRepository extends SolrCrudRepository<;Order, Long>; { 
	 
 Page<;Order>; findByOrderDescription(String searchTerm, Pageable pageable); 
 
} 
</pre>
<p>In the above code, we have created an interface <em>SolrOrderRepository</em>, and we have to add a <em>Pageable</em> parameter to the <em>findByOrderDescription()</em> method of this interface. Let’s see sorting using <em>@Query</em> annotation as the following code:</p>
<p><strong>Using @Query Annotation</strong></p>
<p>Here we create a method annotated with the <em>@Query</em> annotation. Let’s see the following code:</p>
<pre class="highlight">package com.doj.app.repository; 
 
import org.springframework.data.domain.Page; 
import org.springframework.data.domain.Pageable; 
import org.springframework.data.solr.repository.Query; 
import org.springframework.data.solr.repository.SolrCrudRepository; 
 
import com.doj.app.pojo.Order; 
 
/** 
 * @author Dinesh.Rajput 
 * 
 */ 
public interface SolrOrderRepository extends SolrCrudRepository<;Order, Long>; { 
 
 @Query("odesc:*?0*") 	 
 Page<;Order>; findByOrderDescription(String searchTerm, Pageable pageable); 
 
} 
</pre>
<p>In the above code, we have created a method annotated with the <em>@Query</em> annotation. And we have also added a <em>Pageable</em> parameter to the <em>findByOrderDescription()</em> method of the <em>SolrOrderRepository</em> interface.</p>
<p><strong>Named Queries</strong></p>
<p>Let’s see the named queries method.</p>
<pre class="highlight">package com.doj.app.repository; 
 
import org.springframework.data.domain.Page; 
import org.springframework.data.domain.Pageable; 
import org.springframework.data.solr.repository.Query; 
import org.springframework.data.solr.repository.SolrCrudRepository; 
 
import com.doj.app.pojo.Order; 
 
/** 
 * @author Dinesh.Rajput 
 * 
 */ 
public interface SolrOrderRepository extends SolrCrudRepository<;Order, Long>; { 
 
 @Query(name = "Product.findByNamedQuery") 
 Page<;Product>; findByNamedQuery(String searchTerm, Pageable pageable); 
 
} 
</pre>
<p>In the above code, we have used the named queries and we have added a Pageable parameter to the <em>findByNamedQuery()</em> method. In the next section, we will discuss how to call this repository using either controller layer or service layer as the following code:</p>
<pre class="highlight">package com.doj.app.controller; 
 
import java.util.List; 
 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.data.domain.PageRequest; 
import org.springframework.web.bind.annotation.GetMapping; 
import org.springframework.web.bind.annotation.PathVariable; 
import org.springframework.web.bind.annotation.RestController; 
 
import com.doj.app.pojo.Order; 
import com.doj.app.repository.SolrOrderRepository; 
 
/** 
 * @author Dinesh.Rajput 
 * 
 */ 
@RestController 
public class OrderController { 
	 
	@Autowired 
	SolrOrderRepository solrOrderRepository; 
	 
	... 
	 
	@GetMapping("/order/desc/{orderDesc}/{page}") 
	public List<;Order>; findOrder(@PathVariable String orderDesc, @PathVariable int page){ 
		return solrOrderRepository.findByOrderDescription(orderDesc, PageRequest.of(page, 2)).getContent(); 
	} 
	 
	@GetMapping("/order/search/{searchTerm}/{page}") 
	public List<;Order>; findOrderBySearchTerm(@PathVariable String searchTerm, @PathVariable int page){ 
		return solrOrderRepository.findByCustomerQuery(searchTerm, PageRequest.of(page, 2)).getContent(); 
	} 
	 
	... 
} 
 
</pre>
<p>As you can see in the above code, how we have used the paginated request from the controller layer by passing the Pageable object to the repository method.</p>
<h2>Summary</h2>
<p>In this article, we have learned about how to create a paginated query in the Spring Data Solr example by using the <em>PageRequest</em> class and <em>Pageable</em> interface.</p>
<div align="center"><iframe style="width: 120px; height: 240px;" src="//ws-in.amazon-adsystem.com/widgets/q?ServiceVersion=20070822&;OneJS=1&;Operation=GetAdHtml&;MarketPlace=IN&;source=ac&;ref=qf_sp_asin_til&;ad_type=product_link&;tracking_id=dineshonjav06-21&;marketplace=amazon&;region=IN&;placement=1788299450&;asins=1788299450&;linkId=05b0146b0a85f4472697901e353dc276&;show_border=true&;link_opens_in_new_window=true&;price_color=333333&;title_color=0066c0&;bg_color=ffffff" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"><br />
</iframe></div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/spring-data-solr-sorting-with-example/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/spring-data-solr-crud-application-spring-boot/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '4213'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…