Spring Data Solr

Spring Data Solr Pagination Example

<p>In this article of the <strong><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-data-solr-tutorial&sol;">Spring Data Solr<&sol;a><&sol;strong> Pagination&comma; we will discuss how to paginate Solr query results in the spring application&period; Spring Data provides <em>PageRequest<&sol;em> class which extends <em>AbstractPageRequest<&sol;em> and this abstract class implements the <em>Pageable<&sol;em> interface&period; We can query to fetch Solr results in pagination by using <em>PageRequest<&sol;em> class from the service layer&period;<&sol;p>&NewLine;<h2>Using Spring Data Solr Pagination<&sol;h2>&NewLine;<p>Spring Data provides a generic way of pagination implementation in the spring application for all Spring Data implementation such as Spring Data Solr&comma; Spring Data Cassandra&comma; Spring Data JPA etc&period; But in this article&comma; we will look into pagination implementation by using the Spring Data Solr&period;<&sol;p>&NewLine;<p><img class&equals;"wp-image-4171 size-medium alignright" src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2018&sol;06&sol;Spring-Boot-Solr-Example-300x93&period;jpg" alt&equals;"Spring Data Solr Pagination Example" width&equals;"300" height&equals;"93" &sol;><&sol;p>&NewLine;<h3>Creating PageRequest object for pagination<&sol;h3>&NewLine;<p>Since Spring Data 2&period;0&comma; creating a new <em>PageRequest<&sol;em> object using constructors is deprecated&period; Instead of the <em>PageRequest<&sol;em> constructor&comma; we can use <em>of&lpar;&rpar;<&sol;em> static method as a factory method to create a <em>PageRequest<&sol;em> object&period; In a <em>PageRequest<&sol;em> object&comma; pages are zero indexed&comma; thus providing 0 for a page will return the first page&period;<&sol;p>&NewLine;<p>We can create the <em>PageRequest<&sol;em> objects by using several provided overloaded version of the<em> of&lpar;&rpar;<&sol;em> method&period; This object fulfils the pagination requirements as following&colon;<&sol;p>&NewLine;<h3><strong>Requirement 1<&sol;strong><&sol;h3>&NewLine;<p>If you want to create a <em>PageRequest<&sol;em> object to specify a query results belonging to a single page&period; Then we have to create the <em>PageRequest<&sol;em> object as the following code snippet&colon;<&sol;p>&NewLine;<pre class&equals;"highlight">public static PageRequest of&lpar;int page&comma; int size&rpar; &lbrace; &NewLine; &sol;&sol;implementation &NewLine;&rcub; &NewLine; &NewLine;&sol;&sol;In-service class get the query results belonging to the first page when page size is 10&period; &NewLine;PageRequest&period;of&lpar;0&comma; 10&rpar; &NewLine;<&sol;pre>&NewLine;<h3><strong>Requirement 2<&sol;strong><&sol;h3>&NewLine;<p>Another way&comma; if you want to create a <em>PageRequest<&sol;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&comma; we have discussed about <strong><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-data-solr-sorting-with-example&sol;">Spring Data Solr Sorting in the previous article<&sol;a><&sol;strong>&period; Let&&num;8217&semi;s see the following code snippet to create <em>PageRequest<&sol;em> object&colon;<&sol;p>&NewLine;<pre class&equals;"highlight">public static PageRequest of&lpar;int page&comma; int size&comma; Sort sort&rpar; &lbrace; &NewLine; &sol;&sol;implementation &NewLine;&rcub; &NewLine; &NewLine;&sol;&sol;In-service class get the query results belonging to the first page when page size is 10&period; &NewLine;&sol;&sol;Query results are sorted in descending order by using a price field&period; &NewLine;PageRequest&period;of&lpar;0&comma; 10 Sort&period;Direction&period;DESC&comma; "price"&rpar; &NewLine;<&sol;pre>&NewLine;<h3><strong>Requirement 3<&sol;strong><&sol;h3>&NewLine;<p>In this requirement&comma; if you want to create a <em>PageRequest<&sol;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&period; Let&&num;8217&semi;s see the following code snippet to create <em>PageRequest<&sol;em> object&colon;<&sol;p>&NewLine;<pre class&equals;"highlight">public static PageRequest of&lpar;int page&comma; int size&comma; Direction direction&comma; String&period;&period;&period; properties&rpar; &lbrace; &NewLine; &sol;&sol;implementation &NewLine;&rcub; &NewLine; &NewLine;&sol;&sol;In-service class get the query results belonging to the first page when page size is 10&period; &NewLine;&sol;&sol;Query results are sorted in descending order by using oid and odesc fields&period; &NewLine;PageRequest&period;of&lpar;0&comma; 10 Sort&period;Direction&period;DESC&comma; "oid"&comma; "odesc"&rpar; &NewLine;<&sol;pre>&NewLine;<h3><strong>Requirement 4<&sol;strong><&sol;h3>&NewLine;<p>This requirement is very suitable when we want to create a <em>PageRequest<&sol;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&period; Let&&num;8217&semi;s see the following code snippet to create <em>PageRequest<&sol;em> object&colon;<&sol;p>&NewLine;<pre class&equals;"highlight">public static PageRequest of&lpar;int page&comma; int size&comma; Direction direction&comma; String&period;&period;&period; properties&rpar; &lbrace; &NewLine; &sol;&sol;implementation &NewLine;&rcub; &NewLine; &NewLine;&sol;&sol;In-service class get the query results belonging to the first page when page size is 10&period; &NewLine;&sol;&sol;Query results are sorted in descending order by using oid field and ascending order by using odesc field&period; &NewLine;Sort sort &equals; Sort&period;by&lpar;Sort&period;Direction&period;DESC&comma; "oid"&rpar;&period;and&lpar;Sort&period;by&lpar;Sort&period;Direction&period;ASC&comma; "odesc"&rpar;&rpar; &NewLine;PageRequest&period;of&lpar;0&comma; 10&comma; sort&rpar; &NewLine;<&sol;pre>&NewLine;<p>We have discussed how to create a new <em>PageRequest<&sol;em> object using several flavours of the <em>of&lpar;&rpar;<&sol;em> method of <em>PageRequest<&sol;em> class&period; The Solr query with the <em>Pageable<&sol;em> object return the results with following return types&period;<&sol;p>&NewLine;<h2>A return type of a Paginated Solr Query<&sol;h2>&NewLine;<p>In the Spring Data Solr&period; if you use a query method with pagination&comma; it can have query result into two types&period; Let&&num;8217&semi;s see these return types of the paginated query method&period;<&sol;p>&NewLine;<ul>&NewLine;<li><strong>Page&lt&semi;Order&gt&semi;<&sol;strong>&colon; If you want pagination metadata along with query result then you have to use this return type&period; Here the Page is an interface of the Spring Data module and it declares the methods used to fetch pagination metadata in the application&period;<&sol;li>&NewLine;<li><strong>List&lt&semi;Order&gt&semi;<&sol;strong>&colon; If you want the only content of results of the paginated Solr query then you have to use this return type<&sol;li>&NewLine;<&sol;ul>&NewLine;<p>Let&&num;8217&semi;s see an example of a Spring Data repository with the pagination implementation&period;<&sol;p>&NewLine;<h2>Spring Data Solr Repository Example<&sol;h2>&NewLine;<p>In the following repository interface&comma; we have created a repository interface with query generation from the query method&period;<&sol;p>&NewLine;<h3><strong>Query Generation From Method Name<&sol;strong><&sol;h3>&NewLine;<p>In this step&comma; we will explain how to create a query from the method name strategy&period; Let’s see the following code&colon;<&sol;p>&NewLine;<pre class&equals;"highlight">package com&period;doj&period;app&period;repository&semi; &NewLine; &NewLine;import org&period;springframework&period;data&period;domain&period;Page&semi; &NewLine;import org&period;springframework&period;data&period;domain&period;Pageable&semi; &NewLine;import org&period;springframework&period;data&period;solr&period;repository&period;SolrCrudRepository&semi; &NewLine; &NewLine;import com&period;doj&period;app&period;pojo&period;Order&semi; &NewLine; &NewLine;&sol;&ast;&ast; &NewLine; &ast; &commat;author Dinesh&period;Rajput &NewLine; &ast; &NewLine; &ast;&sol; &NewLine;public interface SolrOrderRepository extends SolrCrudRepository&lt&semi;Order&comma; Long&gt&semi; &lbrace; &NewLine;&Tab; &NewLine; Page&lt&semi;Order&gt&semi; findByOrderDescription&lpar;String searchTerm&comma; Pageable pageable&rpar;&semi; &NewLine; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<p>In the above code&comma; we have created an interface <em>SolrOrderRepository<&sol;em>&comma; and we have to add a <em>Pageable<&sol;em> parameter to the <em>findByOrderDescription&lpar;&rpar;<&sol;em> method of this interface&period; Let’s see sorting using <em>&commat;Query<&sol;em> annotation as the following code&colon;<&sol;p>&NewLine;<p><strong>Using &commat;Query Annotation<&sol;strong><&sol;p>&NewLine;<p>Here we create a method annotated with the <em>&commat;Query<&sol;em> annotation&period; Let’s see the following code&colon;<&sol;p>&NewLine;<pre class&equals;"highlight">package com&period;doj&period;app&period;repository&semi; &NewLine; &NewLine;import org&period;springframework&period;data&period;domain&period;Page&semi; &NewLine;import org&period;springframework&period;data&period;domain&period;Pageable&semi; &NewLine;import org&period;springframework&period;data&period;solr&period;repository&period;Query&semi; &NewLine;import org&period;springframework&period;data&period;solr&period;repository&period;SolrCrudRepository&semi; &NewLine; &NewLine;import com&period;doj&period;app&period;pojo&period;Order&semi; &NewLine; &NewLine;&sol;&ast;&ast; &NewLine; &ast; &commat;author Dinesh&period;Rajput &NewLine; &ast; &NewLine; &ast;&sol; &NewLine;public interface SolrOrderRepository extends SolrCrudRepository&lt&semi;Order&comma; Long&gt&semi; &lbrace; &NewLine; &NewLine; &commat;Query&lpar;"odesc&colon;&ast;&quest;0&ast;"&rpar; &Tab; &NewLine; Page&lt&semi;Order&gt&semi; findByOrderDescription&lpar;String searchTerm&comma; Pageable pageable&rpar;&semi; &NewLine; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<p>In the above code&comma; we have created a method annotated with the <em>&commat;Query<&sol;em> annotation&period; And we have also added a <em>Pageable<&sol;em> parameter to the <em>findByOrderDescription&lpar;&rpar;<&sol;em> method of the <em>SolrOrderRepository<&sol;em> interface&period;<&sol;p>&NewLine;<p><strong>Named Queries<&sol;strong><&sol;p>&NewLine;<p>Let’s see the named queries method&period;<&sol;p>&NewLine;<pre class&equals;"highlight">package com&period;doj&period;app&period;repository&semi; &NewLine; &NewLine;import org&period;springframework&period;data&period;domain&period;Page&semi; &NewLine;import org&period;springframework&period;data&period;domain&period;Pageable&semi; &NewLine;import org&period;springframework&period;data&period;solr&period;repository&period;Query&semi; &NewLine;import org&period;springframework&period;data&period;solr&period;repository&period;SolrCrudRepository&semi; &NewLine; &NewLine;import com&period;doj&period;app&period;pojo&period;Order&semi; &NewLine; &NewLine;&sol;&ast;&ast; &NewLine; &ast; &commat;author Dinesh&period;Rajput &NewLine; &ast; &NewLine; &ast;&sol; &NewLine;public interface SolrOrderRepository extends SolrCrudRepository&lt&semi;Order&comma; Long&gt&semi; &lbrace; &NewLine; &NewLine; &commat;Query&lpar;name &equals; "Product&period;findByNamedQuery"&rpar; &NewLine; Page&lt&semi;Product&gt&semi; findByNamedQuery&lpar;String searchTerm&comma; Pageable pageable&rpar;&semi; &NewLine; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<p>In the above code&comma; we have used the named queries and we have added a Pageable parameter to the <em>findByNamedQuery&lpar;&rpar;<&sol;em> method&period; In the next section&comma; we will discuss how to call this repository using either controller layer or service layer as the following code&colon;<&sol;p>&NewLine;<pre class&equals;"highlight">package com&period;doj&period;app&period;controller&semi; &NewLine; &NewLine;import java&period;util&period;List&semi; &NewLine; &NewLine;import org&period;springframework&period;beans&period;factory&period;annotation&period;Autowired&semi; &NewLine;import org&period;springframework&period;data&period;domain&period;PageRequest&semi; &NewLine;import org&period;springframework&period;web&period;bind&period;annotation&period;GetMapping&semi; &NewLine;import org&period;springframework&period;web&period;bind&period;annotation&period;PathVariable&semi; &NewLine;import org&period;springframework&period;web&period;bind&period;annotation&period;RestController&semi; &NewLine; &NewLine;import com&period;doj&period;app&period;pojo&period;Order&semi; &NewLine;import com&period;doj&period;app&period;repository&period;SolrOrderRepository&semi; &NewLine; &NewLine;&sol;&ast;&ast; &NewLine; &ast; &commat;author Dinesh&period;Rajput &NewLine; &ast; &NewLine; &ast;&sol; &NewLine;&commat;RestController &NewLine;public class OrderController &lbrace; &NewLine;&Tab; &NewLine;&Tab;&commat;Autowired &NewLine;&Tab;SolrOrderRepository solrOrderRepository&semi; &NewLine;&Tab; &NewLine;&Tab;&period;&period;&period; &NewLine;&Tab; &NewLine;&Tab;&commat;GetMapping&lpar;"&sol;order&sol;desc&sol;&lbrace;orderDesc&rcub;&sol;&lbrace;page&rcub;"&rpar; &NewLine;&Tab;public List&lt&semi;Order&gt&semi; findOrder&lpar;&commat;PathVariable String orderDesc&comma; &commat;PathVariable int page&rpar;&lbrace; &NewLine;&Tab;&Tab;return solrOrderRepository&period;findByOrderDescription&lpar;orderDesc&comma; PageRequest&period;of&lpar;page&comma; 2&rpar;&rpar;&period;getContent&lpar;&rpar;&semi; &NewLine;&Tab;&rcub; &NewLine;&Tab; &NewLine;&Tab;&commat;GetMapping&lpar;"&sol;order&sol;search&sol;&lbrace;searchTerm&rcub;&sol;&lbrace;page&rcub;"&rpar; &NewLine;&Tab;public List&lt&semi;Order&gt&semi; findOrderBySearchTerm&lpar;&commat;PathVariable String searchTerm&comma; &commat;PathVariable int page&rpar;&lbrace; &NewLine;&Tab;&Tab;return solrOrderRepository&period;findByCustomerQuery&lpar;searchTerm&comma; PageRequest&period;of&lpar;page&comma; 2&rpar;&rpar;&period;getContent&lpar;&rpar;&semi; &NewLine;&Tab;&rcub; &NewLine;&Tab; &NewLine;&Tab;&period;&period;&period; &NewLine;&rcub; &NewLine; &NewLine;<&sol;pre>&NewLine;<p>As you can see in the above code&comma; how we have used the paginated request from the controller layer by passing the Pageable object to the repository method&period;<&sol;p>&NewLine;<h2>Summary<&sol;h2>&NewLine;<p>In this article&comma; we have learned about how to create a paginated query in the Spring Data Solr example by using the <em>PageRequest<&sol;em> class and <em>Pageable<&sol;em> interface&period;<&sol;p>&NewLine;<div align&equals;"center"><iframe style&equals;"width&colon; 120px&semi; height&colon; 240px&semi;" src&equals;"&sol;&sol;ws-in&period;amazon-adsystem&period;com&sol;widgets&sol;q&quest;ServiceVersion&equals;20070822&amp&semi;OneJS&equals;1&amp&semi;Operation&equals;GetAdHtml&amp&semi;MarketPlace&equals;IN&amp&semi;source&equals;ac&amp&semi;ref&equals;qf&lowbar;sp&lowbar;asin&lowbar;til&amp&semi;ad&lowbar;type&equals;product&lowbar;link&amp&semi;tracking&lowbar;id&equals;dineshonjav06-21&amp&semi;marketplace&equals;amazon&amp&semi;region&equals;IN&amp&semi;placement&equals;1788299450&amp&semi;asins&equals;1788299450&amp&semi;linkId&equals;05b0146b0a85f4472697901e353dc276&amp&semi;show&lowbar;border&equals;true&amp&semi;link&lowbar;opens&lowbar;in&lowbar;new&lowbar;window&equals;true&amp&semi;price&lowbar;color&equals;333333&amp&semi;title&lowbar;color&equals;0066c0&amp&semi;bg&lowbar;color&equals;ffffff" frameborder&equals;"0" marginwidth&equals;"0" marginheight&equals;"0" scrolling&equals;"no"><br &sol;>&NewLine;<&sol;iframe><&sol;div>&NewLine;<div class&equals;"wp-post-navigation"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-pre"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-data-solr-sorting-with-example&sol;">Previous<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-next"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-data-solr-crud-application-spring-boot&sol;">Next<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;<&sol;div>&NewLine;<script type&equals;"text&sol;javascript">&NewLine;jQuery&lpar;document&rpar;&period;ready&lpar;function&lpar;&dollar;&rpar; &lbrace;&NewLine; &dollar;&period;post&lpar;'https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-admin&sol;admin-ajax&period;php'&comma; &lbrace;action&colon; 'mts&lowbar;view&lowbar;count'&comma; id&colon; '4213'&rcub;&rpar;&semi;&NewLine;&rcub;&rpar;&semi;&NewLine;<&sol;script>

Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

4 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

4 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

4 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

4 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

4 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

4 years ago