Question

I'm still very much a newbie in the realms of Solr.

I'm attempting create a query which groups by categories, returning a unique list of sub_categories. My schema looks something similar to the following:

+==============================================+
| id  | category | sub_category | Type         |
+-----+----------+--------------+--------------+
| 1   | Apparel  | Pants        | Suede        |
| 2   | Apparel  | Pants        | Leather      |
| 3   | Apparel  | Pants        | Wind Pants   |
| 4   | Apparel  | Shirts       | Short-Sleeve |
| 5   | Apparel  | Shirts       | Long-Sleeve  |
| 6   | Sports   | Balls        | Soccer Ball  |
| 7   | Sports   | Balls        | Football     |
+-----+----------+--------------+--------------+

I'm interested in getting a return similar to the following, but am unsure on how to accomplish it. I can almost get here, the but problem is I am unable to get the sub_category column to return unique values. The example below does account for distinct sub_categories:

{
  "responseHeader": {
    "status": 0,
    "QTime:" 12,
  },
  "grouped": {
    "category": {
      "matches": 1,
      "groups": [
        {
          "groupValue": "Apparel",
          'docList": {
            "numFound": 2,
            'start': 0,
            'docs': [
              {"sub_category": "Pants"},
              {"sub_category": "Shirts"}
            ]
        },
        {
          "groupValue": "Sports",
          'docList": {
            "numFound": 2,
            'start': 0,
            'docs': [
              {"sub_category": "Balls"}
            ]
        },
      ]
    }
  }
}
Was it helpful?

Solution

Assuming, you are using Solr 4.0+, I believe facet pivoting is a better way to do this.

Try:

http://localhost:8983/solr/select?q=*:*&facet.pivot=category,sub_category&facet=true&facet.field=category&rows=0

Update: Hmm, but that's not going to give you the unique counts though :-? That will give you something like this, if that's OK with you:

+ Apparel [5]
|--- Pants [3]
|--- Shirts [2]
|
+ Sports [2]
|--- Balls [2]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top