Tuesday, June 11, 2013

SOLR facet component customizations

Here is the use case:   Assume, you are developing e-commerce  search system, one fine day, someone from marketing department comes and says he wants to able to sort the brad facet differently ( neither increasing count order or lexicographical order  supported by the SOLR facets module. For example bring Samsung after Apple, for a tablet category brand facet) In this case, development team will end up customizing the sort at application layer for each facet field. I did this as  custom SOLR component. Following recipe will show.  I am not including all the component logic, but following logic which does parsing of facet component outputs & setting the order differently.

public void process(ResponseBuilder rb) throws IOException
      
  NamedList facet_counts = (NamedList)rb.rsp.getValues().get("facet_counts" );

   if (facet_counts == null) return;
   NamedList> facet_fields = (NamedList>) facet_counts.get( "facet_fields" );

           if (facet_fields == null) return;
           if( facet_fields != null ) {
           TreeMap sortedMap = new  TreeMap();
              int ctr=0;
            for( Map.Entry> facet : facet_fields ) {
                 String key =  facet.getKey() ;
                 if ( customFacetCache.containsKey(key )){
                    FacetObj facetDetails = customFacetCache.get(key);
                    if (facetDetails != null ){
                          String sortOrder = facetDetails.sortOrder;
                           sortedMap.clear();
                           for( Map.Entry entry : facet.getValue() ) {
                             sortedMap.put(entry.getKey(), entry.getValue().longValue() );               
                           }

                  if (sortOrder.equalsIgnoreCase("custom")){
                          ///now introduce your own custom order.
                  }

No comments: