{"id":361,"date":"2011-08-01T21:46:10","date_gmt":"2011-08-01T19:46:10","guid":{"rendered":"http:\/\/sematext.solr.pl\/?p=361"},"modified":"2020-11-11T21:46:38","modified_gmt":"2020-11-11T20:46:38","slug":"car-sale-application-result-grouping-two-additional-parameters-description-part-7","status":"publish","type":"post","link":"https:\/\/solr.pl\/en\/2011\/08\/01\/car-sale-application-result-grouping-two-additional-parameters-description-part-7\/","title":{"rendered":"\u201cCar sale application\u201d\u2013 Result Grouping, two additional parameters description (part 7)"},"content":{"rendered":"<p>In the last \u201ccar sale application\u201d related post we have described the result grouping functionality. Today I would like to show you how easily we can determine the groups amount and how to sort documents within every group.<\/p>\n\n\n<!--more-->\n\n\n<h3>Requirements specification<\/h3>\n<p>I would like to be able to create a grouping query, that will show me the number of generated groups and provide only one result within every group \u2013 the one with the lowest price in its year group.<\/p>\n<h3>New functionality request parameters description<\/h3>\n<p>What we need is:<\/p>\n<ul>\n<li><em>group.ngroups<\/em> &#8211; boolean type parameter that allows us to include the number of generated groups<\/li>\n<li><em>group.sort<\/em> &#8211; parameter describing how to sort documents within a group<\/li>\n<\/ul>\n<h3>Let&#8217;s create query<\/h3>\n<p>Using the query example from the previous post, let&#8217;s add two new parameters:\n<\/p>\n<pre class=\"brush:xml\">?q=audi+a4&amp;group=true&amp;group.field=year_group&amp;group.limit=1&amp;fl=id,mileage,make,model,year,price&amp;group.ngroups=true&amp;group.sort=price+asc<\/pre>\n<p>As you see, we&#8217;ve also set the group.limit parameter to 1 (in order to have the only one result within every group) and extended the value of fl parameter by adding the price field. As a result we have the response:\n<\/p>\n<pre class=\"brush:xml\">&lt;lst name=\"grouped\"&gt;\n  &lt;lst name=\"year_group\"&gt;\n    &lt;int name=\"matches\"&gt;5&lt;\/int&gt;\n    &lt;int name=\"ngroups\"&gt;3&lt;\/int&gt;\n    &lt;arr name=\"groups\"&gt;\n      &lt;lst&gt;\n        &lt;str name=\"groupValue\"&gt;2002&lt;\/str&gt;\n        &lt;result name=\"doclist\" numFound=\"2\" start=\"0\"&gt;\n          &lt;doc&gt;\n            &lt;str name=\"id\"&gt;3&lt;\/str&gt;\n            &lt;str name=\"make\"&gt;Audi&lt;\/str&gt;\n            &lt;int name=\"mileage\"&gt;125000&lt;\/int&gt;\n            &lt;str name=\"model\"&gt;A4&lt;\/str&gt;\n            &lt;float name=\"price\"&gt;21300.0&lt;\/float&gt;\n            &lt;int name=\"year\"&gt;2002&lt;\/int&gt;\n          &lt;\/doc&gt;\n        &lt;\/result&gt;\n      &lt;\/lst&gt;\n      &lt;lst&gt;\n        &lt;str name=\"groupValue\"&gt;2003&lt;\/str&gt;\n        &lt;result name=\"doclist\" numFound=\"2\" start=\"0\"&gt;\n          &lt;doc&gt;\n            &lt;str name=\"id\"&gt;2&lt;\/str&gt;\n            &lt;str name=\"make\"&gt;Audi&lt;\/str&gt;\n            &lt;int name=\"mileage\"&gt;220000&lt;\/int&gt;\n            &lt;str name=\"model\"&gt;A4&lt;\/str&gt;\n            &lt;float name=\"price\"&gt;27800.0&lt;\/float&gt;\n            &lt;int name=\"year\"&gt;2003&lt;\/int&gt;\n          &lt;\/doc&gt;\n        &lt;\/result&gt;\n      &lt;\/lst&gt;\n      &lt;lst&gt;\n        &lt;str name=\"groupValue\"&gt;2006&lt;\/str&gt;\n        &lt;result name=\"doclist\" numFound=\"1\" start=\"0\"&gt;\n          &lt;doc&gt;\n            &lt;str name=\"id\"&gt;5&lt;\/str&gt;\n            &lt;str name=\"make\"&gt;Audi&lt;\/str&gt;\n            &lt;int name=\"mileage\"&gt;9900&lt;\/int&gt;\n            &lt;str name=\"model\"&gt;A4&lt;\/str&gt;\n            &lt;float name=\"price\"&gt;32100.0&lt;\/float&gt;\n            &lt;int name=\"year\"&gt;2006&lt;\/int&gt;\n          &lt;\/doc&gt;\n        &lt;\/result&gt;\n      &lt;\/lst&gt;\n    &lt;\/arr&gt;\n  &lt;\/lst&gt;\n&lt;\/lst&gt;<\/pre>\n<p>As we see in the response, we have a new element that shows us how many groups are generated:\n<\/p>\n<pre class=\"brush:xml\">&lt;int name=\"ngroups\"&gt;3&lt;\/int&gt;<\/pre>\n<p>We also have only one document in each year group \u2013 the car with the lowest price in its year group. You don&#8217;t believe me ? Look at the query responses from the previous post and compare the prices \ud83d\ude42<\/p>\n<h3>The end<\/h3>\n<p>It was a fast review of yet two another grouping parameters. Big thanks to David Martin who gave me the subject by asking some questions in the previous post \ud83d\ude42<\/p>","protected":false},"excerpt":{"rendered":"<p>In the last \u201ccar sale application\u201d related post we have described the result grouping functionality. Today I would like to show you how easily we can determine the groups amount and how to sort documents within every group.<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_links_to":"","_links_to_target":""},"categories":[27],"tags":[],"class_list":["post-361","post","type-post","status-publish","format-standard","hentry","category-solr-en"],"_links":{"self":[{"href":"https:\/\/solr.pl\/en\/wp-json\/wp\/v2\/posts\/361","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solr.pl\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solr.pl\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solr.pl\/en\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/solr.pl\/en\/wp-json\/wp\/v2\/comments?post=361"}],"version-history":[{"count":1,"href":"https:\/\/solr.pl\/en\/wp-json\/wp\/v2\/posts\/361\/revisions"}],"predecessor-version":[{"id":362,"href":"https:\/\/solr.pl\/en\/wp-json\/wp\/v2\/posts\/361\/revisions\/362"}],"wp:attachment":[{"href":"https:\/\/solr.pl\/en\/wp-json\/wp\/v2\/media?parent=361"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solr.pl\/en\/wp-json\/wp\/v2\/categories?post=361"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solr.pl\/en\/wp-json\/wp\/v2\/tags?post=361"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}