Make delicious recipes!

Grouping in Solr



Grouping in Solr can be used to group results together based on fields.
This is very similar to group-by clause in SQL with one important difference.

SQL group-by clauses (OR Solr facets) only return the counts per group where as Solr Group option returns all the documents per group too.

http://localhost:8983/solr/collection1/
select?q=*:*&
wt=json&
fl=id,cat,manu,price&
indent=true&
omitHeader=true&
group=true&
group.field=price&
group.limit=2&
rows=5
Note that the group options are very similar to the facet options like
Facet Group
facet=true/false group=true/false
facet.field group.field
facet.limit group.limit
facet.query group.query


{
  "grouped":{
    "price":{
      "matches":1035,
      "groups":[{
          "groupValue":0.0,
          "doclist":{"numFound":103,"start":0,"docs":[
              {
                "id":"GB18030TEST",
                "price":0.0},
              {
                "id":"SOLR1000",
                "manu":"Apache Software Foundation",
                "cat":["software", "search"],
                "price":0.0}]
          }},
        {
          "groupValue":92.0,
          "doclist":{"numFound":1,"start":0,"docs":[
              {
                "id":"SP2514N",
                "manu":"Samsung Electronics Co. Ltd.",
                "cat":["electronics", "hard drive"],
                "price":92.0}]
          }},
        {
          "groupValue":350.0,
          "doclist":{"numFound":1,"start":0,"docs":[
              {
                "id":"6H500F0",
                "manu":"Maxtor Corp.",
                "cat":["electronics", "hard drive"],
                "price":350.0}]
          }},
        {
          "groupValue":19.95,
          "doclist":{"numFound":1,"start":0,"docs":[
              {
                "id":"F8V7067-APL-KIT",
                "manu":"Belkin",
                "cat":["electronics", "connector"],
                "price":19.95}]
          }},
        {
          "groupValue":11.5,
          "doclist":{"numFound":1,"start":0,"docs":[
              {
                "id":"IW-02",
                "manu":"Belkin",
                "cat":["electronics", "connector"],
                "price":11.5}]
          }
        }
     ]
   }
 }
}


Using arbitrary queries for grouping


http://localhost:8983/solr/collection1/
select?q=*:*&
wt=json&
fl=id,cat,manu,price&
indent=true&
omitHeader=true&
group=true&
group.query=price:[* TO 500]&
group.query=price:[501 TO *]&
group.limit=2&
rows=5

{
  "grouped":{
    "price:[* TO 500]":{
      "matches":1035,
      "doclist":{
        "numFound":1017,"start":0,
        "docs":[
          {
            "id":"GB18030TEST",
            "price":0.0},
          {
            "id":"SP2514N",
            "manu":"Samsung Electronics Co. Ltd.",
            "cat":["electronics", "hard drive"],
            "price":92.0}
         ]
      }},
    "price:[501 TO *]":{
      "matches":1035,
      "doclist":{
        "numFound":2,"start":0,
        "docs":[
          {
            "id":"3007WFP",
            "manu":"Dell, Inc.",
            "cat":["electronics",
              "monitor"],
            "price":2199.0},
          {
            "id":"100-435805",
            "manu":"ATI Technologies",
            "cat":["electronics", "graphics card"],
            "price":649.99}
        ]
      }
    }
  }
}

The official Field Collapsing page provides a good description of all the options which can be used with facets.





Like us on Facebook to remain in touch
with the latest in technology and tutorials!


Got a thought to share or found a
bug in the code?
We'd love to hear from you:

Name:
Email: (Your email is not shared with anybody)
Comment:

Facebook comments:

Site Owner: Sachin Goyal