Let's assume for the purpose of this recipe that you have an application that can search for companies within a city and state. But the requirements say that you should show not only the search results but also the number of companies in each city and the number of companies in each state (in the Solr way we say that you want to exclude the filter query from the faceting results). Can Solr do that in an efficient way? Sure it can, and this recipe will show you how to do it.
Before you start reading this recipe, please take a look at the Getting the number of documents with the same field value recipe in this chapter.
We start with the following index structure (just add this to your
schema.xml
file in the field definition section; we will use thecity
andstate
fields to do the faceting):<field name="id" type="string" indexed="true" stored="true" required="true" /> <field name="name" type="text" indexed="true" stored...