We've seen previously how Solr provides a facet, by using which we can put indexed data into groups depending on the fields. Let's see how we can improve user experience while selecting the type of music by grouping music into genres.
We can use facet=true
to enable faceting of fields, and then use facet.field=genre
to return the number of songs that are in each category. The search query will return only the facet data and will look like this:
http://localhost:8983/solr/musicStore/select?q=*%3A*&start=0&rows=0&wt=json&indent=true&facet=true&facet.field=genre
As we're interested in the facet data only, we've specified rows=0
. This URL will return us the following data:
{ "responseHeader":{ }, "response":{ }, "facet_counts":{ "facet_queries":{ }, "facet_fields":{ "genre":[ "Pop", 3, "Dance/Electronic", 2 ] }, "facet_dates":{}, "facet_ranges":{}, "facet_intervals":{} } }
As the user can be presented with the number of songs available under each genre, he/she can then click on the genre to further his/her search for the song that he/she is looking for. In the next section, we'll see how we can use the filters available in the search handler to further refine search results.
Another way in which we can group data is by using the group parameter. Grouping provides us with a way to return the top N documents (the default value is 1
) by field. Let's see how we can use this feature:
http://localhost:8983/solr/musicStore/select?q=*%3A*&wt=json&indent=true&fl=id,songName,genre&group=true&group.field=genre&group.limit=10
The URL will return the following response; we have currently used the fl
parameter, which tells Solr to return a set of fields from the index:
{ "responseHeader":{ "status":0, "QTime":1 }, "grouped":{ "genre":{ "matches":5, "groups":[ { "groupValue":"Pop", "doclist":{ "numFound":3, "start":0, "docs":[ { "id":"1001", "songName":"Don't Stop the Party", "genre":"Pop" }, { "id":"1002", "songName":"All I Want For Christmas Is You", "genre":"Pop" }, { "id":"1003", "songName":"A Thousand Years", "genre":"Pop" } ] } }, { "groupValue":"Dance/Electronic", "doclist":{ "numFound":2, "start":0, "docs":[ { "id":"1004", "songName":"Killing the Light", "genre":"Dance/Electronic" }, { "id":"1005", "songName":"Not Giving In (Radio Edit)", "genre":"Dance/Electronic" } ] } } ] } } }
From the result, we can see that the group.field
parameter has grouped the genre into two categories:Pop
and Dance/Electronic
. Also, group.limit=10
tells the search handler to return the top 10 results for each category.
3.143.22.26