I think we need some sort of customization of Apache Solr  search result in most of our drupal projects.  For simple customization, we can do theming but if we need more complex customization where we need more custom code and need to implement complex busines logic, we can use Views to customizaiton the search result. It is very simple, just call you views from hook_apachesolr_process_results and reset the apache solr search generated output item. Please see an expamle below.

function apachesolr_custom_results_apachesolr_process_results(&$arg)
{
    // loop through each result, performing desired actions
    for($i = 0, $l = count($arg); $i < $l; $i++)
    {    
        $nid = $arg[$i]['fields']['nid'];
        if($arg[$i]['type'] == 'Resource')
        {          
           $arg[$i]['snippet'] = views_embed_view($name = 'search_result_template_resource', $display_id = 'default', $nid);
        }
       else if($arg[$i]['type'] == 'Page')
        {
           $arg[$i]['snippet'] = views_embed_view($name = 'search_result_template_page', $display_id = 'default', $nid);
        }
        else if($arg[$i]['type'] == 'CaT News/Event')
        {
           $arg[$i]['snippet'] = views_embed_view($name = 'search_result_template_newsevent', $display_id = 'default', $nid);
        }
        // remove Apache Solr generated output items
        $arg[$i]['title'] =  "";
        $arg[$i]['extra'][0] = "";
        $arg[$i]['date'] = "";
        $arg[$i]['type'] =  "";
        $arg[$i]['user'] = "";
        
    }
}

 

Here,  I have to call different views to format the search result for different content type. I have implemented all business to views and even i could reuse the function that i have already developed.

"Views in your hand, You can use it as your wish"

Overheads and Fix:
There is some sort of overhead to call views for each result item. it may affect for large database only. In that case, we can cache the views or indexing.
 
 
 

Comments

ron's picture

Calling views for each result means calling a query (usually with joins) for each item. For making the application scalable, this is not good approach at all. Have to give a second thought before going for this approach.

There are alternatives for sure using apachesolr way.

Add new comment