Search This Blog

Loading...

Monday, November 28, 2011

Implemented highlighting feature using the SolrPHPClient

Hi janusman
It seems that we are developing on the same issues. Maybe you have see that I have developed a spellchecker to (also supporting phrases)
http://drupal.org/node/303937
I have implemented highlighting text snippets using solr.
Step 1: Replace Solr PHP Client with attached one 
The author of SolrPHPClient has released a new SolrPHPClient using json_decode instead of xml results. This is a performance gain.
https://issues.apache.org/jira/browse/SOLR-341
I have attached a modified version of the SolrPHPClient.
Step 2: Edit apachesolr_search.module
<?php
     
try {
       
$params = apachesolr_search_get_params();

       
//Enable highlighting
       
if(variable_get('apachesolr_solrhighlighting', false)) {
       
           
//Add new parameter to the search request
           
$params['hl'] = 'true';
           
$params['hl.fragsize']= variable_get('apachesolr_textsnippetlength', 100);
           
$params['hl.simple.pre'] = '<strong>';
           
$params['hl.simple.post'] = '</strong>';
           
        }
       
       
$response = $solr->search($query->get_query(), $params['start'], $params['rows'], $params, $search_servlet);?>
<?php
        apachesolr_has_searched
(TRUE);
       
       
//New since json_decode
       
$highlight = $response->highlighting;
       
$response = $response->response;?>
<?php
            $extra
['score'] = $doc->score;
           
            if(
variable_get('apachesolr_solrhighlighting', false)){
                
$snippet = each($highlight);
                
$snippet = $snippet['value']->text[0];           
            }
            else
                
$snippet = search_excerpt($keys, $doc->body);   
           
            if (
trim($snippet) == '...') {
             
$snippet = '';
            }
?>
Step3: adapt apachesolr.module
<?php
  $form
['apachesolr_path'] = array(
   
'#type' => 'textfield',
   
'#title' => t('Solr path'),
   
'#default_value' => variable_get('apachesolr_path', '/solr'),
   
'#description' => t('Path that identifies the Solr request handler to be used. Leave this as /solr for now.'),
    );
 
$form['apachesolr_solrhighlighting'] = array(
   
'#type' => 'checkbox',
   
'#title' => t('Enable highlighting by solr'),
   
'#default_value' => variable_get('apachesolr_solrhighlighting', false),
   
'#description' => t('Highlighting and text excerpt is done using solr.'),
   );
 
 
$options = array();
  foreach (array(
100, 150, 200, 250, 300, 400, 500, 600) as $option) {
   
$options[$option] = $option;
  }
 
$form['apachesolr_textsnippetlength'] = array(
   
'#type' => 'select',
   
'#title' => t('Length of text snippet in the result'),
   
'#default_value' => variable_get('apachesolr_textsnippetlength', 100),
   
'#options' => $options,
   
'#description' => t('The number of characters the text snippet in the search result will be.'),
    );
?>
AttachmentSize
apachesolr.zip150.29 KB
SolrPhpClient.zip99.23 KB

#3

Is your attached version of SolrPhpClient the same as the one in https://issues.apache.org/jira/browse/SOLR-341 ?
Your changes to the apachesolr module should be in diff format. Please see http://drupal.org/patch/create ... This way we can track what exact version is being changed and where =)

#4

No, the attached SolrPHPClient is slightly modified to support different search request handler.
Because I have implemented a spellchecker too, I'm not able to export this modification as "standalone". I have attached a patch in the diff form including the spellchecker modifications. Its for the latest 6.0 version.
AttachmentSize
patch_apachesolr_spellchecker_highlight.txt894.67 KB

http://drupal.org/node/303973

0 comments:

Post a Comment

LinkWithin

Related Posts with Thumbnails