Rafal Andrzejewski is a team leader and software developer. Taking part in many Solr and Lucene projects. Recently joined solr.pl site as blogger where can share his knowledge about search engine oriented topics. Rafał is a DZone MVB and is not an employee of DZone and has posted 8 posts at DZone. You can read more from them at their website. View Full User Profile

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

08.01.2011
| 3755 views |
  • submit to reddit

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 :)




Reference:  http://solr.pl/en/2011/08/01/“car-sale-application”–-result-grouping-two-additional-parameters-description-part-7

Published at DZone with permission of Rafał Andrzejewski, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags:

Comments

David Martin replied on Mon, 2011/08/01 - 8:39am

Nice to see my questions illustrated in your article :)

That's a common use case in the way we use our current search engine (which is unfortunately not Solr).

No more question this time ;-)

David

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.