Skip to content

Instantly share code, notes, and snippets.

@cesarmiquel
Created May 25, 2014 04:41
Show Gist options
  • Save cesarmiquel/8d026ca79789ad931785 to your computer and use it in GitHub Desktop.
Save cesarmiquel/8d026ca79789ad931785 to your computer and use it in GitHub Desktop.
Call Apache Solr usings the apachesolr Drupal module and parse the response.
<?php
// -------------------------------------------------------------------------
//
// Taken from:
// http://worldofdrupal.blogspot.com.ar/2014/01/drupal-7-apache-solr-custom-query.html
//
// -------------------------------------------------------------------------
// Search parameters
$keyword = 'como';
$offset = 0;
$limit = 100;
// Get instance
$solr = apachesolr_get_solr();
// Create basic query
$query = apachesolr_drupal_query("custom", array('q' => $keyword));
// Add offset and limits
$query->addParam('start', $offset);
$query->addParam('rows', $limit);
// Turn on highlighting (http://wiki.apache.org/solr/HighlightingParameters)
$query->addParam('hl', TRUE);
// Filter by content-type in this case either nota, nota_galeria or nota_video.
$query->addParam('fq', "bundle:(nota OR nota_galeria OR nota_video)");
// Perform query.
$resp = $query->search();
// Parse response
if ($resp->code == 200) {
$result = $resp->response;
$total_rows = $result->numFound;
$docs = $result->docs;
print("Found $total_rows nodes.\n\n");
foreach($docs as $doc) {
$nid = $doc->entity_id;
$type = $doc->entity_type;
$node = node_load($nid);
$title = $node->title;
print("$nid $title - $type\n");
}
}
// snippets
print_r( $resp->highlighting );
// all response
print_r( $resp );
exit(0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment