blog-banner

How to Make Apache Solr Search Programmatically from Drupal 7

  • APACHE SOLR
  • Drupal 7
  • Drupal Planet

Recently I came across a situation where I needed to make Apache solr search from drupal programmatically. Basically, we have Apache solr module available in Drupal, which enables us to connect with solr for searching. There are loads of documentation for setting solr search with drupal, so instead of going into those details, let's head straight to the custom code required to make programmatic solr search from drupal:

$solr = apachesolr_get_solr();
$query = apachesolr_drupal_query("apachesolr");
$query->addParam('q', 'keyword'); // keyword to be searched
$query->addParam('hl', true); // enable highlighting
$query->addParam('rows', '200');
$response = $query->search();
$raw_results = apachesolr_search_process_response($response, $query);
if (isset($raw_results) && !empty($raw_results)) {
  foreach ($raw_results as $raw_result) {
    // theme individual results
    $result .= theme('search_result', array('result' => $raw_result, 'module' => 'apachesolr_search'));
  }
}
return $result;

 

Get awesome tech content in your inbox