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;



shivachevva (not verified)

Wed, 09/28/2016 - 02:24

Thank suresh. This information very help me to write apache solr programatically in my project. But how can i add the pagination in the mentioned query.

Add new comment

The content of this field is kept private and will not be shown publicly.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.