Skip to main content
Apache Solr Search

How to make Apache solr search programmatically from Drupal 7

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 to connect with solr for searching. There are loads of documentation for setting solr search with drupal, so instead going into those details, lets 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;