Over a million developers have joined DZone.

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

· Java Zone

Microservices! They are everywhere, or at least, the term is. When should you use a microservice architecture? What factors should be considered when making that decision? Do the benefits outweigh the costs? Why is everyone so excited about them, anyway?  Brought to you in partnership with IBM.

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

Discover how the Watson team is further developing SDKs in Java, Node.js, Python, iOS, and Android to access these services and make programming easy. Brought to you in partnership with IBM.

Topics:

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}