Imagine a situation where besides the search results, you have to return the number of documents with the same field value. For example, imagine that you have an application that allows the user to search for companies in Europe, and your client wants the number of companies in the cities where the companies that were found by the query are located. To do this, you could of course run several queries but Solr provides a mechanism called faceting that can do that for you. This recipe will show you how to do it.
For getting the number of documents with the same field value, follow these steps:
To start, let's assume that we have the following index structure (just add this to your
schema.xml
file in the field definition section; we will use thecity
field to do the faceting):<field name="id" type="string" indexed="true" stored="true" required="true" /> <field name="name" type="text" indexed="true" stored="true...