Using facets in Solr using Solr4J in Java or Grails applications
Posted By : Jasgeet Singh | 08-Oct-2012
I am using faceting, one of the features of Solr (java search server) in my current project and would like to share my experience and learning.
Faceting is Solr's killer-feature. It's a must-have feature for most search implementations, especially those with structured data. Faceting enhances search results with aggregated information over all the documents found in the search, not the entire index.Faceted search is also called faceted browsing, faceted navigation, guided navigation and sometimes parametric search.Now might be a good time for a screenshot,
If we revisit the comparison of search technology to databases, then faceting is more or less analogous to SQL's GROUP BY feature on a column with count(*). However, in Solr, facet processing is performed subsequent to an existing search as part of a single request-response with both the primary search results and the faceting results coming back together. Like as in following screenshot after selecting some facet values we have breadcrumb trail using the following snippet:
def solrQuery = new SolrQuery( “*” ) solrQuery.setFilterQueries (/subcat: Accommodation Provider AND tags: tours/)
Note: *- query parameter which matches all documents
fq - (filter query) parameter, which allows one to filter by a query.
Faceting Field Values
Field value faceting is the most common type of faceting. The following are the request parameters for using it.
• facet.field: You must set this parameter to a field's name in order to facet on that field. Repeat this parameter for each field to be faceted on.
solrQuery.addFacetField(“subcat”) solrQuery.addFacetField(“tags”) solrQuery.addFacetField(“languages”)
Note: parameter – field name from the IndexSchema.
• facet.sort : It is set to either count to sort the facet values by descending totals or to index to sort lexicographically, as if you sorted on the field. If facet.limitis greater than zero (it's 100 by default), then Solr picks count as the default, otherwise index is chosen.
solrQuery.setFacetSort(FacetParams.FACET_SORT_INDEX) solrQuery.setFacetSort(FacetParams.FACET_SORT_COUNT)
•facet.limit: It defaults to 100. It limits the number of facet values returned in the searchresults of a field. As these are usually going to be displayed to the user, it doesn't make sense to have a large number of these in the response. If you need all of them then disable the limit with a value of -1.
solrQuery.setFacetLimit(10)
• facet.mincount: This defaults to 0. It filters out facet values that have facet counts less than this. This is applied before limit and offset so that paging works as expected. It is common to set this to 1 since 0 is almost useless.
solrQuery.setFacetMinCount(1)
• facet.missing: It defaults to blank and is set to true or on for the facet value listing to include an unnamed count at the end, which is the number of searched documents that have no indexed terms.
solrQuery.setFacetMissing(FacetParams.FACET_MISSING)
Resources
• Read more about Solr on the Solr Home Page.
• Read Solr’s Documentation
• Read the Solr tutorial
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Jasgeet Singh
Jasgeet is a Sr. Lead developer .He is an experienced Groovy and Grails and has worked on designing & developing B2B and B2C portals using Grails technologies.