Grouping documents by the field value
Imagine a situation where your dataset is divided into different categories, subcategories, price ranges, and things like that. What if you would like to not only get information about counts in such groups (with the use of faceting), but you would only like to show the most relevant document in each of the groups. In such cases, a Solr grouping mechanism comes in handy. This recipe will show you how to group your documents on the basis of the value of the field.
How to do it...
Let's start with the index structure. Let's assume that we have the following fields in our index (just add the following section to the
schema.xml
file):<field name="id" type="string" indexed="true" stored="true" required="true" /> <field name="name" type="text_general" indexed="true" stored="true" /> <field name="category" type="string" indexed="true" stored="true" /> <field name="price" type="tfloat" indexed="true" stored="true" />
The example data, which...