0
votes

I'm trying to get a two-level facet count from our solr server. The documents look like this (shortened to only the relevant fields):

{
        "id":"100071F5",
        "datasource":"ABC",
        "mediatype":"ISSBD"
}

With a query like this ...

http://localhost:8983/solr/select?wt=json&fl=id&indent=true&facet=true&facet.field=datasource&facet.field=mediatype

... I get as a result like this:

    "facet_fields":{
      "datasource":[
        "ABC",75286,
        "DEF",47257],
      "mediatype":[
        "POSTCARD",75286,
        "RARE",12033,
        "MANUSCRIPT",9418,
        "BOOK",5849,
        "OTHER",4747,
        "UNKNOWN",2603,
        "MAP",1033,
        "GLOBE",704,
        "MIXED",13,
        "ISSUE",1]} ...

But what I really want is something like this:

"facet_fields":{
  "datasource":[
    "ABC",75286,
      "mediatype":[
        "POSTCARD",1234,
        "RARE",1,
        "BOOK",533,
        "OTHER",47],
  "DEF",47257,
      "mediatype":[
        "POSTCARD",7286,
        "RARE",1203,
        "MANUSCRIPT",918,
        "BOOK",549,
        "OTHER",4747,
        "UNKNOWN",2603,
        "MAP",1033,
        "GLOBE",704,
        "MIXED",13,
        "ISSUE",1]} ...

In words: I like to have a facet over one field and then a subfacet for each subresult over another field. Is this possible in Solr?

1

1 Answers

1
votes

You can use Pivot Faceting:

&facet.pivot=datasource,mediatype

It should give you a similar structure back:

[{
  "field":"datasource",
  "value":"ABC",
  "count":75286,
  "pivot":[{
          "field":"mediatype",
          "value":"POSTCARD",
          "count":34919
    }, { .... 
  }]
}]

You can also use the JSON Facet API to make even more detailed facet aggregations and sub-facets. Example adopted from the reference guide above:

top_datasource:{
  type: terms,
  field: datasource,
  facet:{
    top_mediatype:{
      type: terms, // nested terms facet on author will be calculated for each parent bucket (datasource)
      field: mediatype
    }
  }
}