0%

Group and count: faceting query

In Solr, faceting is a feature that allows you to categorise search results into different groups based on specified criteria. It can be useful, for example, if you need to know all field values and their counts to filter out unnecessary ones and make the query more efficient.

To perform a facet request, use the solr_request function with the required Solr parameters: facet and facet.field.

  • facet — enables facet counts in the query response.
  • facet.field — specifies the field to run faceting on.
  • facet.limit — controls how many constraints should be returned for each facet. By default, it shows 100; set it to 15 if you need a smaller set. Make sure to set the correct value if you need the full set of different values.
  • facet.mincount — specifies the minimum counts required for a facet field to be included in the response. We recommend setting it to 1 to exclude zero values.

More faceting query parameters are in the official Solr documentation.

num_found, df = solr_request(
    core="statistical-result",
    params={
         "q": "*:*",
         "rows": 0,
         "facet": "on",
         "facet.field": "zygosity",
         "facet.limit": 15,
         "facet.mincount": 1,
    }
)

In this example, we execute a faceted search on a statistical-result core, specifically targeting the zygosity field to get a count of how many documents fall into different categories of zygosity.