“Car sale application”– Result Grouping, two additional parameters description (part 7)

In the last “car sale application” 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.

Requirements specification

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 – the one with the lowest price in its year group.

New functionality request parameters description

What we need is:

  • group.ngroups – boolean type parameter that allows us to include the number of generated groups
  • group.sort – parameter describing how to sort documents within a group

Let’s create query

Using the query example from the previous post, let’s add two new parameters:

?q=audi+a4&group=true&group.field=year_group&group.limit=1&fl=id,mileage,make,model,year,price&group.ngroups=true&group.sort=price+asc

As you see, we’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:

<lst name="grouped">
  <lst name="year_group">
    <int name="matches">5</int>
    <int name="ngroups">3</int>
    <arr name="groups">
      <lst>
        <str name="groupValue">2002</str>
        <result name="doclist" numFound="2" start="0">
          <doc>
            <str name="id">3</str>
            <str name="make">Audi</str>
            <int name="mileage">125000</int>
            <str name="model">A4</str>
            <float name="price">21300.0</float>
            <int name="year">2002</int>
          </doc>
        </result>
      </lst>
      <lst>
        <str name="groupValue">2003</str>
        <result name="doclist" numFound="2" start="0">
          <doc>
            <str name="id">2</str>
            <str name="make">Audi</str>
            <int name="mileage">220000</int>
            <str name="model">A4</str>
            <float name="price">27800.0</float>
            <int name="year">2003</int>
          </doc>
        </result>
      </lst>
      <lst>
        <str name="groupValue">2006</str>
        <result name="doclist" numFound="1" start="0">
          <doc>
            <str name="id">5</str>
            <str name="make">Audi</str>
            <int name="mileage">9900</int>
            <str name="model">A4</str>
            <float name="price">32100.0</float>
            <int name="year">2006</int>
          </doc>
        </result>
      </lst>
    </arr>
  </lst>
</lst>

As we see in the response, we have a new element that shows us how many groups are generated:

<int name="ngroups">3</int>

We also have only one document in each year group – the car with the lowest price in its year group. You don’t believe me ? Look at the query responses from the previous post and compare the prices 🙂

The end

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 🙂

This entry was posted on Monday, August 1st, 2011 at 08:12 and is filed under Bez kategorii. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

One Response to ““Car sale application”– Result Grouping, two additional parameters description (part 7)”

  1. Danny Says:

    Grouping is a very useful feature, but it has some pitfall too.
    If you are using paging for your results and “5” is your referenz for count, you may have problems to visualize your results. Eg. count is 100 and your result-set are only 20 grouped array items, you have no page 3 (for 10 elements per page).