8

I'd like to send custom solr query (preferably in hook_init) using Search API Solr to a specific URL.

Something like:

function hook_init() {
  $url = "admin/luke?show=schema";
  $solr = new SearchApiSolrService(); // must have an instance of SearchApiServer
  // $httpTransport->performHeadRequest($url);
  // $response = _sendRawGet($url);
  drupal_set_message(print_r($response, TRUE));
}

How I can do achieve that? Is there any live example?

Thank you.

10

From version RC4 you can use makeServletRequest() to generate custom solr queries. See some examples on GitHub.

Here are some code examples:

  // Ping example
  // class_exists('Apache_Solr_Service') ?: spl_autoload_register('_search_api_solr_autoload'); // make sure that SolrPhpClient class is loaded (use only for <=rc3)
  $server = search_api_server_load('my_solr_server'); // CHANGE THIS
  $solr = new SearchApiSolrService($server);
  $result = $solr->ping();
  drupal_set_message(print_r($result, TRUE));

  // Simple search query (not fully tested)
  // class_exists('Apache_Solr_Service') ?: spl_autoload_register('_search_api_solr_autoload'); // make sure that SolrPhpClient class is loaded (use only for <=rc3)
  $server = search_api_server_load('my_solr_server'); // CHANGE THIS
  $index = search_api_index_load('my_index'); // CHANGE THIS
  $query = new SearchApiQuery($index);
  $query->keys('foo'); 
  $query->fields(array('title')); 
  $solr = new SearchApiSolrService($server);
  $result = $solr->search($query);
  drupal_set_message(print_r($result, TRUE));

  // Custom path example for rc4 and above (not fully tested)
  $server = search_api_server_load('my_solr_server');
  $uri = SearchApiSolrConnection::LUKE_SERVLET . "?show=schema"; // or any other custom URL
  $connection = $server->getSolrConnection(); // or: new SearchApiSolrService($server)
  $response = $connection->makeServletRequest($uri);
  drupal_set_message(print_r($response, TRUE));

  // Example for assembling the right URL
  $server = search_api_server_load('my_solr_server');
  $url = 'http://' . $server->options['host'] . ':' . $server->options['port'] . $server->options['path'];

Above code tested in hook_init with Drupal 7 and Search API Solr [rc3].


Acquia

If you're using Acquia, you can use Acquia-Search-Proxy which allows you to send custom queries against Acquia Search (Apachesolr + Search API Solr). See also my fork of Acquia-Search-Proxy which include some extra improvements.

Example custom queries via CLI:

PATH_INFO="/admin/ping" php search-proxy.php
PATH_INFO="/admin/luke" QUERY_STRING="show=schema" php search-proxy.php
PATH_INFO="/select" QUERY_STRING="q=foo" php search-proxy.php
PATH_INFO="/admin/file" QUERY_STRING="contentType=text/xml;charset=utf-8&file=schema.xml" php search-proxy.php
6

I've seen an example in a presentation at http://nickveenhof.github.io/devdays_solr_wizardry/#/click-recipes-search-api. I don't understand every line of code (I mean - why does he add a 'bundle' filter and remove it again?) but I still hope it helps.

$solr = apachesolr_get_solr();
$query = apachesolr_drupal_query("custom", array('q' => 'mykeys'), 'sort_label asc', 'search/path');
$query->setSolrsort('sort_name', 'desc');
$query->addFilter('bundle', (article OR page));
$query->removeFilter('bundle');
$query->addParam('fq', "bundle:(article OR page)");
$query->addParam('fq', "field_date:[1970-12-31T23:59:59Z TO NOW]");
$resp = $query->search();
  • Thanks for that, it's good, but unfortunately it's for Apachesolr, not by using Search API Solr. – kenorb May 29 '13 at 8:33
  • Sorry, I misread your question. I don't have a code example for Search API, but it looks like the documentation at drupal.org/node/2004270 and the inline documentation in query.inc, especially the SearchApiQuery class, is pretty detailed. – marcvangend May 29 '13 at 11:21
1

SearchApiQuery offers a pretty complete tool to build custom queries.

Some code example :

  global $language;
  $term = 'TERM_TO_SEARCH_IN_THE_TITLE';

  // Select an index.      
  $index = search_api_index_load("default_multilingual_node_index");
  $query = new SearchApiQuery($index);

  // Build a filter.
  $filter = $query->createFilter('OR');
  $filter->condition('type', 'article', '=');
  $filter->condition('type', 'blog_post', '=');
  $query->filter($filter);

  // Conditions.
  $query->condition('title_field', $term, '=');
  $query->condition('language', $language->language, '=');
  $query->sort('timestamp_field');

  // Fetch results.
  $results = $query->execute()['results'];
1

In order to add facets to your query, use setOption

$query = new SearchApiQuery($index);
$query->keys(''); 
$query->range(0,0); 
$query->fields(array());
$query->setOption('search_api_facets',array(array('field'=>'field_country'))); 
$solr = new SearchApiSolrService($server);
$result = $solr->search($query);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.