{"id":13272,"date":"2013-05-31T13:00:35","date_gmt":"2013-05-31T10:00:35","guid":{"rendered":"http:\/\/www.javacodegeeks.com\/?p=13272"},"modified":"2013-06-02T22:40:18","modified_gmt":"2013-06-02T19:40:18","slug":"spring-data-solr-tutorial-sorting","status":"publish","type":"post","link":"https:\/\/www.javacodegeeks.com\/2013\/05\/spring-data-solr-tutorial-sorting.html","title":{"rendered":"Spring Data Solr Tutorial: Sorting"},"content":{"rendered":"<p>When we are implementing a word search function, we typically want to sort the search results in descending order by using the <a href=\"http:\/\/wiki.apache.org\/solr\/SolrRelevancyFAQ\" target=\"_blank\">relevancy of each search result<\/a>. This is also the default behaviour of Solr.<\/p>\n<p>However, there are situations when it makes to sense to specify the sort order manually. One such situation is an implementation of a \u201cregular\u201d search function which was discussed in the <a href=\"http:\/\/www.javacodegeeks.com\/2013\/05\/spring-data-solr-tutorial-dynamic-queries.html\">previous part of my Spring Data Solr tutorial<\/a>.<\/p>\n<p>This blog post describes how we can sort our query results with Spring Data Solr. To be more specific, we have to modify the search function of our example application to sort the search results in descending order by using the value of the <em>id<\/em> field.<\/p>\n<p>This blog post is divided into three sections:<\/p>\n<ul>\n<li>The first section describes how we can specify the sorting options used in our queries.<\/li>\n<li>The second section describes how we can sort our query results when we are building our queries by using query methods.<\/li>\n<li>The third section teaches us to sort the query results of dynamic queries.<\/li>\n<\/ul>\n<p>Let\u2019s get started.<\/p>\n<p><strong>Note:<\/strong> These blog posts provide additional information which helps us to understand the concepts described in this blog post:<\/p>\n<ul>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2013\/05\/running-solr-with-maven.html\">Running Solr with Maven<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2013\/05\/spring-data-solr-tutorial-introduction-to-solr.html\">Spring Data Solr Tutorial: Introduction to Solr<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2013\/05\/spring-data-solr-tutorial-configuration.html\">Spring Data Solr Tutorial: Configuration<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2013\/05\/spring-data-solr-tutorial-crud-almost.html\">Spring Data Solr Tutorial CRUD (Almost)<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2013\/05\/spring-data-solr-tutorial-adding-custom-methods-to-a-single-repository.html\">Spring Data Solr Tutorial: Adding Custom Methods to a Single Repository<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2013\/05\/spring-data-solr-tutorial-dynamic-queries.html\">Spring Data Solr Tutorial: Dynamic Queries<\/a><\/li>\n<\/ul>\n<h2>Specifying the Sort Options of a Query<\/h2>\n<p>The sort options of a query are specified by using the <a href=\"http:\/\/static.springsource.org\/spring-data\/data-commons\/docs\/current\/api\/org\/springframework\/data\/domain\/Sort.html\" target=\"_blank\"><em>Sort<\/em><\/a> class. The typical requirements for sorting query results are given in the following:<\/p>\n<ul>\n<li>Sort the query results by using the value of a single field.<\/li>\n<li>Sort the query results by using the values of multiple fields when the sort order of different fields is the same.<\/li>\n<li>Sort the query results by using the values of multiple fields when sort order of different fields is not the same.<\/li>\n<\/ul>\n<p>Let\u2019s take a look how we can create a <em>Sort<\/em> object which fulfills the given requirements.<\/p>\n<p>First, we must create a <em>Sort<\/em> object which specifies that query results are sorted by using a single field. Lets assume that we want to sort the query results in ascending order by using the <em>id<\/em> field. We can create the <em>Sort<\/em> object by using the following code:<\/p>\n<pre class=\"brush:java\">new Sort(Sort.Direction.ASC, \"id\")<\/pre>\n<p>Second, we must create a <em>Sort<\/em> object which states that query results are sorted by using the values of multiple fields when the sort order of different fields is the same. Lets assume that we have to sort the query results in descending order by using the <em>id<\/em> and <em>description<\/em> fields. We can create the <em>Sort<\/em> object by using the following code:<\/p>\n<pre class=\"brush:java\">new Sort(Sort.Direction.DESC, \"id\", \"description\")<\/pre>\n<p>Third, we want to sort the query results by using the values of multiple fields when the sort order of different fields is not the same. Lets assume that we want to sort the query results in descending order by using the <em>description<\/em> field and in ascending order by using the <em>id<\/em> field. We can create the <em>Sort<\/em> object by using the following code:<\/p>\n<pre class=\"brush:java\">new Sort(Sort.Direction.DESC, \"description\").and(new Sort(Sort.Direction.ASC, \"id\"))<\/pre>\n<p>We now know how we can create new <em>Sort<\/em> objects. Let\u2019s move on and put this theory into practice.<\/p>\n<h2>Sorting the Query Results of Query Methods<\/h2>\n<p>When we are building our queries by using query methods, we can sort the query results by following these steps:<\/p>\n<ol>\n<li>Add a new <em>Sort<\/em> parameter to the query method. This method parameter specifies the used sort options.<\/li>\n<li>Create a new <em>Sort<\/em> object in the service layer and pass it as a method parameter when the query method is called.<\/li>\n<\/ol>\n<p>Let\u2019s move on and find out how this is done.<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<h4>Modifying the Repository Interface<\/h4>\n<p>We can sort the query results of our query by adding a new <em>Sort<\/em> parameter to the our query method. This method parameter specifies the sort options of the executed query. Let\u2019s move on and take a look at the declarations of our query methods.<\/p>\n<h2>Query Generation From Method Name<\/h2>\n<p>When the executed query is created by using the query generation from method name strategy, we have to add a <em>Sort<\/em> parameter to the <em>findByTitleContainsOrDescriptionContains()<\/em> method of the <em>TodoDocumentRepository<\/em> interface. The source code of our repository interface looks as follows:<\/p>\n<pre class=\"brush:java\">import org.springframework.data.domain.Sort;\r\nimport org.springframework.data.solr.repository.Query;\r\nimport org.springframework.data.solr.repository.SolrCrudRepository;\r\n\r\nimport java.util.List;\r\n\r\npublic interface TodoDocumentRepository extends PartialUpdateRepository, SolrCrudRepository&lt;TodoDocument, String&gt; {\r\n\r\n    public List&lt;TodoDocument&gt; findByTitleContainsOrDescriptionContains(String title, String description, Sort sort);\r\n}<\/pre>\n<h2>Named Queries<\/h2>\n<p>When the executed query is created by using named queries, we have to add a <em>Sort<\/em> parameter to the <em>findByNamedQuery()<\/em> method of the <em>TodoDocumentRepository<\/em> interface. The source code of our repository interface looks as follows:<\/p>\n<pre class=\"brush:java\">import org.springframework.data.domain.Sort;\r\nimport org.springframework.data.solr.repository.Query;\r\nimport org.springframework.data.solr.repository.SolrCrudRepository;\r\n\r\nimport java.util.List;\r\n\r\npublic interface TodoDocumentRepository extends PartialUpdateRepository, SolrCrudRepository&lt;TodoDocument, String&gt; {\r\n\r\n    @Query(name = \"TodoDocument.findByNamedQuery\")\r\n    public List&lt;TodoDocument&gt; findByNamedQuery(String searchTerm, Sort sort);\r\n}<\/pre>\n<p><strong>Note:<\/strong> This approach does not work if we are using Spring Data Solr RC1 because of a <a href=\"https:\/\/jira.springsource.org\/browse\/DATASOLR-63\" target=\"_blank\">known bug<\/a>. We have to either use the build snapshot dependency or wait for the release of RC2.<\/p>\n<h2>The @Query Annotation<\/h2>\n<p>When the executed query is created by using the <em>@Query<\/em> annotation, we have to add a <em>Sort<\/em> parameter to the <em>findByQueryAnnotation()<\/em> method of the <em>TodoDocumentRepository<\/em> interface. The source code of our repository interface looks as follows:<\/p>\n<pre class=\"brush:java\">import org.springframework.data.domain.Sort;\r\nimport org.springframework.data.solr.repository.Query;\r\nimport org.springframework.data.solr.repository.SolrCrudRepository;\r\n\r\nimport java.util.List;\r\n\r\npublic interface TodoDocumentRepository extends PartialUpdateRepository, SolrCrudRepository&lt;TodoDocument, String&gt; {\r\n\r\n    @Query(\"title:*?0* OR description:*?0*\")\r\n    public List&lt;TodoDocument&gt; findByQueryAnnotation(String searchTerm, Sort sort);\r\n}<\/pre>\n<p><strong>Note:<\/strong> This approach does not work if we are using Spring Data Solr RC1 because of a <a href=\"https:\/\/jira.springsource.org\/browse\/DATASOLR-62\" target=\"_blank\">known bug<\/a>. We have to either use the build snapshot dependency or wait for the release of RC2.<\/p>\n<h4>Using the Query Method<\/h4>\n<p>We can use the modified query method by making the following changes to the <em>search()<\/em> method of the <em>RepositoryIndexService<\/em> class:<\/p>\n<ol>\n<li>Create a private <em>sortByIdDesc()<\/em> method which specifies that the query results are sorted in descending order by using the <em>id<\/em> of the document.<\/li>\n<li>Get the sorted query results by calling the query method declared in the <em>TodoDocumentRepository<\/em> interface.<\/li>\n<li>Return the query results.<\/li>\n<\/ol>\n<p>Let\u2019s move on and take a look at the different implementations of the <em>search()<\/em> method.<\/p>\n<h2>Query Generation From Method Name<\/h2>\n<p>When we are building our queries by using the query generation from method name strategy, we can get the query results by using the <em>findByTitleContainsOrDescriptionContains()<\/em> method of the <em>TodoDocumentRepository<\/em> interface.<\/p>\n<p>The source code of the relevant part of <em>RepositoryTodoIndexService<\/em> class looks as follows:<\/p>\n<pre class=\"brush:java\">import org.springframework.data.domain.Sort;\r\nimport org.springframework.stereotype.Service;\r\nimport javax.annotation.Resource;\r\nimport java.util.List;\r\n\r\n@Service\r\npublic class RepositoryTodoIndexService implements TodoIndexService {\r\n\r\n    @Resource\r\n    private TodoDocumentRepository repository;\r\n\r\n    @Override\r\n    public List&lt;TodoDocument&gt; search(String searchTerm) {\r\n        return repository.findByTitleContainsOrDescriptionContains(searchTerm, searchTerm, sortByIdDesc());\r\n    }\r\n\r\n    private Sort sortByIdDesc() {\r\n        return new Sort(Sort.Direction.DESC, \"id\");\r\n    }\r\n   \r\n    \/\/Other methods are omitted\r\n}<\/pre>\n<h2>Named Queries<\/h2>\n<p>When we are building our queries by using named queries, we can get the query results by using the <em>findByNamedQuery()<\/em> method of the <em>TodoDocumentRepository<\/em> interface.<\/p>\n<p>The relevant part of the <em>RepositoryTodoIndexService<\/em> looks as follows:<\/p>\n<pre class=\"brush:java\">import org.springframework.data.domain.Sort;\r\nimport org.springframework.stereotype.Service;\r\nimport javax.annotation.Resource;\r\nimport java.util.List;\r\n\r\n@Service\r\npublic class RepositoryTodoIndexService implements TodoIndexService {\r\n\r\n    @Resource\r\n    private TodoDocumentRepository repository;\r\n\r\n    @Override\r\n    public List&lt;TodoDocument&gt; search(String searchTerm) {\r\n        return repository.findByNamedQuery(searchTerm, sortByIdDesc());\r\n    }\r\n\r\n    private Sort sortByIdDesc() {\r\n        return new Sort(Sort.Direction.DESC, \"id\");\r\n    }\r\n   \r\n    \/\/Other methods are omitted\r\n}<\/pre>\n<h2>The @Query annotation<\/h2>\n<p>When we are building our queries by using the <em>@Query<\/em> annotation, we can get the query results by using the <em>findByQueryAnnotation()<\/em> method of the <em>TodoDocumentRepository<\/em> interface.<\/p>\n<p>The relevant part of the <em>RepositoryTodoIndexService<\/em> class looks as follows:<\/p>\n<pre class=\"brush:java\">import org.springframework.data.domain.Sort;\r\nimport org.springframework.stereotype.Service;\r\nimport javax.annotation.Resource;\r\nimport java.util.List;\r\n\r\n@Service\r\npublic class RepositoryTodoIndexService implements TodoIndexService {\r\n\r\n    @Resource\r\n    private TodoDocumentRepository repository;\r\n\r\n    @Override\r\n    public List&lt;TodoDocument&gt; search(String searchTerm) {\r\n        return repository.findByQueryAnnotation(searchTerm, sortByIdDesc());\r\n    }\r\n\r\n    private Sort sortByIdDesc() {\r\n        return new Sort(Sort.Direction.DESC, \"id\");\r\n    }\r\n   \r\n    \/\/Other methods are omitted\r\n}<\/pre>\n<h2>Sorting the Query Results of Dynamic Queries<\/h2>\n<p>Because <a href=\"http:\/\/www.javacodegeeks.com\/2013\/05\/spring-data-solr-tutorial-dynamic-queries.html\">dynamic queries<\/a> are created by <a href=\"http:\/\/www.javacodegeeks.com\/2013\/05\/spring-data-solr-tutorial-adding-custom-methods-to-a-single-repository.html\">adding a custom method to a repository interface<\/a>, the steps required to sort the query results of a dynamic query have no effect to the service layer of our example application.<\/p>\n<p>We can sort the query results of dynamic queries by making the following changes to the implementation of our custom repository interface:<\/p>\n<ol>\n<li>Add a private <em>sortByIdDesc()<\/em> method to the <em>TodoDocumentRepositoryImpl<\/em> class. This method returns a <em>Sort<\/em> object which specifies that the query results are sorted in descending order by using the <em>id<\/em> of the document.<\/li>\n<li>Modify the <em>search()<\/em> method of the <em>TodoDocumentRepositoryImpl<\/em> class. Set the sort options to the executed query by using the <em>addSort()<\/em> method of the <a href=\"http:\/\/static.springsource.org\/spring-data\/solr\/docs\/1.0.0.RC1\/api\/org\/springframework\/data\/solr\/core\/query\/Query.html\" target=\"_blank\"><em>Query<\/em><\/a> interface and pass the created <em>Sort<\/em> object as a method parameter.<\/li>\n<\/ol>\n<p>The relevant part of the <em>TodoDocumentRepositoryImpl<\/em> class looks as follows:<\/p>\n<pre class=\"brush:java\">import org.springframework.data.domain.Page;\r\nimport org.springframework.data.domain.Sort;\r\nimport org.springframework.data.solr.core.SolrTemplate;\r\nimport org.springframework.data.solr.core.query.Criteria;\r\nimport org.springframework.data.solr.core.query.SimpleQuery;\r\nimport org.springframework.stereotype.Repository;\r\n\r\nimport javax.annotation.Resource;\r\nimport java.util.List;\r\n\r\n@Repository\r\npublic class TodoDocumentRepositoryImpl implements CustomTodoDocumentRepository {\r\n\r\n    @Resource\r\n    private SolrTemplate solrTemplate;\r\n\r\n    @Override\r\n    public List&lt;TodoDocument&gt; search(String searchTerm) {\r\n        String[] words = searchTerm.split(\" \");\r\n\r\n        Criteria conditions = createSearchConditions(words);\r\n        SimpleQuery search = new SimpleQuery(conditions);\r\n       \r\n        \/\/SET SORT OPTIONS\r\n        search.addSort(sortByIdDesc());\r\n\r\n        Page results = solrTemplate.queryForPage(search, TodoDocument.class);\r\n        return results.getContent();\r\n    }\r\n\r\n    private Criteria createSearchConditions(String[] words) {\r\n        Criteria conditions = null;\r\n\r\n        for (String word: words) {\r\n            if (conditions == null) {\r\n                conditions = new Criteria(\"id\").contains(word)\r\n                        .or(new Criteria(\"description\").contains(word));\r\n            }\r\n            else {\r\n                conditions = conditions.or(new Criteria(\"id\").contains(word))\r\n                        .or(new Criteria(\"description\").contains(word));\r\n            }\r\n        }\r\n\r\n        return conditions;\r\n    }\r\n\r\n    private Sort sortByIdDesc() {\r\n        return new Sort(Sort.Direction.DESC, \"id\");\r\n    }\r\n\r\n    \/\/Other methods are omitted\r\n}<\/pre>\n<h2>Summary<\/h2>\n<p>We have now learned how we can sort query results with Spring Data Solr. This tutorial has taught us three things:<\/p>\n<ul>\n<li>We know that we can specify the used sort options by using the <em>Sort<\/em> class.<\/li>\n<li>We learned that we can sort the query result of query methods by adding a new method parameter to the query method.<\/li>\n<li>We learned that we can set the sort options to a dynamic query by using the <em>addSort()<\/em> method of the <em>Query<\/em> interface.<\/li>\n<\/ul>\n<p>The next part of my Spring Data Solr tutorial describes <a href=\"http:\/\/www.javacodegeeks.com\/2013\/05\/spring-data-solr-tutorial-pagination.html\">how we can paginate the query results of our queries<\/a>.<\/p>\n<p>P.S. The example applications of this blog posts are available at Github (<a href=\"https:\/\/github.com\/pkainulainen\/spring-data-solr-examples\/tree\/master\/query-methods\" target=\"_blank\">query methods<\/a> and <a href=\"https:\/\/github.com\/pkainulainen\/spring-data-solr-examples\/tree\/master\/criteria\" target=\"_blank\">dynamic queries<\/a>).<br \/>\n&nbsp;<\/p>\n<div style=\"border: 1px solid #D8D8D8; background: #FAFAFA; width: 100%; padding-left: 5px;\"><b><i>Reference: <\/i><\/b><a href=\"http:\/\/www.petrikainulainen.net\/programming\/solr\/spring-data-solr-tutorial-sorting\/\">Spring Data Solr Tutorial: Sorting<\/a> from our <a href=\"http:\/\/www.javacodegeeks.com\/jcg\">JCG partner<\/a> Petri Kainulainen at the <a href=\"http:\/\/www.petrikainulainen.net\/\">Petri Kainulainen<\/a> blog.<\/div>\n","protected":false},"excerpt":{"rendered":"<p>When we are implementing a word search function, we typically want to sort the search results in descending order by using the relevancy of each search result. This is also the default behaviour of Solr. However, there are situations when it makes to sense to specify the sort order manually. One such situation is an &hellip;<\/p>\n","protected":false},"author":429,"featured_media":80,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[470,30,321],"class_list":["post-13272","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-enterprise-java","tag-apache-solr","tag-spring","tag-spring-data"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Spring Data Solr Tutorial: Sorting<\/title>\n<meta name=\"description\" content=\"When we are implementing a word search function, we typically want to sort the search results in descending order by using the relevancy of each search\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.javacodegeeks.com\/2013\/05\/spring-data-solr-tutorial-sorting.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Spring Data Solr Tutorial: Sorting\" \/>\n<meta property=\"og:description\" content=\"When we are implementing a word search function, we typically want to sort the search results in descending order by using the relevancy of each search\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.javacodegeeks.com\/2013\/05\/spring-data-solr-tutorial-sorting.html\" \/>\n<meta property=\"og:site_name\" content=\"Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2013-05-31T10:00:35+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2013-06-02T19:40:18+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/apache-solr-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Petri Kainulainen\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/petrikainulaine\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Petri Kainulainen\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/05\\\/spring-data-solr-tutorial-sorting.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/05\\\/spring-data-solr-tutorial-sorting.html\"},\"author\":{\"name\":\"Petri Kainulainen\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/5af4df3fdfeb79e9fa3598d79bff2c9e\"},\"headline\":\"Spring Data Solr Tutorial: Sorting\",\"datePublished\":\"2013-05-31T10:00:35+00:00\",\"dateModified\":\"2013-06-02T19:40:18+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/05\\\/spring-data-solr-tutorial-sorting.html\"},\"wordCount\":1345,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/05\\\/spring-data-solr-tutorial-sorting.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/apache-solr-logo.jpg\",\"keywords\":[\"Apache Solr\",\"Spring\",\"Spring Data\"],\"articleSection\":[\"Enterprise Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/05\\\/spring-data-solr-tutorial-sorting.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/05\\\/spring-data-solr-tutorial-sorting.html\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/05\\\/spring-data-solr-tutorial-sorting.html\",\"name\":\"Spring Data Solr Tutorial: Sorting\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/05\\\/spring-data-solr-tutorial-sorting.html#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/05\\\/spring-data-solr-tutorial-sorting.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/apache-solr-logo.jpg\",\"datePublished\":\"2013-05-31T10:00:35+00:00\",\"dateModified\":\"2013-06-02T19:40:18+00:00\",\"description\":\"When we are implementing a word search function, we typically want to sort the search results in descending order by using the relevancy of each search\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/05\\\/spring-data-solr-tutorial-sorting.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/05\\\/spring-data-solr-tutorial-sorting.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/05\\\/spring-data-solr-tutorial-sorting.html#primaryimage\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/apache-solr-logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/apache-solr-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/05\\\/spring-data-solr-tutorial-sorting.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Enterprise Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\\\/enterprise-java\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Spring Data Solr Tutorial: Sorting\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Developers Resource Center\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.javacodegeeks.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/javacodegeeks\",\"https:\\\/\\\/x.com\\\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/5af4df3fdfeb79e9fa3598d79bff2c9e\",\"name\":\"Petri Kainulainen\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/9e57425180f323fa65bc519a64c8273d3fcb7c6bd272e56b37dd15613f403659?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/9e57425180f323fa65bc519a64c8273d3fcb7c6bd272e56b37dd15613f403659?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/9e57425180f323fa65bc519a64c8273d3fcb7c6bd272e56b37dd15613f403659?s=96&d=mm&r=g\",\"caption\":\"Petri Kainulainen\"},\"description\":\"Petri is passionate about software development and continuous improvement. He is specialized in software development with the Spring Framework and is the author of Spring Data book.\",\"sameAs\":[\"http:\\\/\\\/www.petrikainulainen.net\\\/\",\"http:\\\/\\\/www.linkedin.com\\\/in\\\/petrikainulainen\",\"https:\\\/\\\/x.com\\\/https:\\\/\\\/twitter.com\\\/petrikainulaine\"],\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/author\\\/petri-kainulainen\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Spring Data Solr Tutorial: Sorting","description":"When we are implementing a word search function, we typically want to sort the search results in descending order by using the relevancy of each search","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.javacodegeeks.com\/2013\/05\/spring-data-solr-tutorial-sorting.html","og_locale":"en_US","og_type":"article","og_title":"Spring Data Solr Tutorial: Sorting","og_description":"When we are implementing a word search function, we typically want to sort the search results in descending order by using the relevancy of each search","og_url":"https:\/\/www.javacodegeeks.com\/2013\/05\/spring-data-solr-tutorial-sorting.html","og_site_name":"Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2013-05-31T10:00:35+00:00","article_modified_time":"2013-06-02T19:40:18+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/apache-solr-logo.jpg","type":"image\/jpeg"}],"author":"Petri Kainulainen","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/petrikainulaine","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Petri Kainulainen","Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.javacodegeeks.com\/2013\/05\/spring-data-solr-tutorial-sorting.html#article","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/2013\/05\/spring-data-solr-tutorial-sorting.html"},"author":{"name":"Petri Kainulainen","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/5af4df3fdfeb79e9fa3598d79bff2c9e"},"headline":"Spring Data Solr Tutorial: Sorting","datePublished":"2013-05-31T10:00:35+00:00","dateModified":"2013-06-02T19:40:18+00:00","mainEntityOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2013\/05\/spring-data-solr-tutorial-sorting.html"},"wordCount":1345,"commentCount":0,"publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2013\/05\/spring-data-solr-tutorial-sorting.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/apache-solr-logo.jpg","keywords":["Apache Solr","Spring","Spring Data"],"articleSection":["Enterprise Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.javacodegeeks.com\/2013\/05\/spring-data-solr-tutorial-sorting.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.javacodegeeks.com\/2013\/05\/spring-data-solr-tutorial-sorting.html","url":"https:\/\/www.javacodegeeks.com\/2013\/05\/spring-data-solr-tutorial-sorting.html","name":"Spring Data Solr Tutorial: Sorting","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2013\/05\/spring-data-solr-tutorial-sorting.html#primaryimage"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2013\/05\/spring-data-solr-tutorial-sorting.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/apache-solr-logo.jpg","datePublished":"2013-05-31T10:00:35+00:00","dateModified":"2013-06-02T19:40:18+00:00","description":"When we are implementing a word search function, we typically want to sort the search results in descending order by using the relevancy of each search","breadcrumb":{"@id":"https:\/\/www.javacodegeeks.com\/2013\/05\/spring-data-solr-tutorial-sorting.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.javacodegeeks.com\/2013\/05\/spring-data-solr-tutorial-sorting.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/2013\/05\/spring-data-solr-tutorial-sorting.html#primaryimage","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/apache-solr-logo.jpg","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/apache-solr-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.javacodegeeks.com\/2013\/05\/spring-data-solr-tutorial-sorting.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Java","item":"https:\/\/www.javacodegeeks.com\/category\/java"},{"@type":"ListItem","position":3,"name":"Enterprise Java","item":"https:\/\/www.javacodegeeks.com\/category\/java\/enterprise-java"},{"@type":"ListItem","position":4,"name":"Spring Data Solr Tutorial: Sorting"}]},{"@type":"WebSite","@id":"https:\/\/www.javacodegeeks.com\/#website","url":"https:\/\/www.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Developers Resource Center","publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/5af4df3fdfeb79e9fa3598d79bff2c9e","name":"Petri Kainulainen","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/9e57425180f323fa65bc519a64c8273d3fcb7c6bd272e56b37dd15613f403659?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/9e57425180f323fa65bc519a64c8273d3fcb7c6bd272e56b37dd15613f403659?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/9e57425180f323fa65bc519a64c8273d3fcb7c6bd272e56b37dd15613f403659?s=96&d=mm&r=g","caption":"Petri Kainulainen"},"description":"Petri is passionate about software development and continuous improvement. He is specialized in software development with the Spring Framework and is the author of Spring Data book.","sameAs":["http:\/\/www.petrikainulainen.net\/","http:\/\/www.linkedin.com\/in\/petrikainulainen","https:\/\/x.com\/https:\/\/twitter.com\/petrikainulaine"],"url":"https:\/\/www.javacodegeeks.com\/author\/petri-kainulainen"}]}},"_links":{"self":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/13272","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/users\/429"}],"replies":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=13272"}],"version-history":[{"count":0,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/13272\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media\/80"}],"wp:attachment":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=13272"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=13272"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=13272"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}