By kenorb


2013-05-28 21:34:24 8 Comments

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.

4 comments

@Marek Tichy 2015-07-01 16:25:25

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);

@David Piry 2014-07-24 10:35:35

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'];

@kenorb 2013-05-29 13:32:09

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

@marcvangend 2013-05-28 22:02:06

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();

@kenorb 2013-05-29 08:33:25

Thanks for that, it's good, but unfortunately it's for Apachesolr, not by using Search API Solr.

@marcvangend 2013-05-29 11:21:52

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.

Related Questions

Sponsored Content

3 Answered Questions

[SOLVED] How to exclude items from search api solr by negative keywords field

  • 2017-05-26 11:17:56
  • TCLopez
  • 2274 View
  • 0 Score
  • 3 Answer
  • Tags:   8 search

2 Answered Questions

Search API Solr Configured, Content Indexed, No Search Results

  • 2019-04-07 13:19:15
  • Scott Hollenbeck
  • 56 View
  • 1 Score
  • 2 Answer
  • Tags:   search

1 Answered Questions

[SOLVED] Programmatically pass parameters to Apache Solr integrated with Search API

  • 2019-02-22 16:22:37
  • Matt
  • 166 View
  • 1 Score
  • 1 Answer
  • Tags:   8 search

1 Answered Questions

[SOLVED] How to send a custom Solr Distance Query with SearchApi

  • 2018-12-06 11:23:49
  • Rainer Feike
  • 313 View
  • 0 Score
  • 1 Answer
  • Tags:   8 location search

2 Answered Questions

[SOLVED] Search API Solr: How to index fulltext fields with and without stemming?

  • 2013-01-01 20:27:10
  • user56reinstatemonica8
  • 14595 View
  • 5 Score
  • 2 Answer
  • Tags:   search

1 Answered Questions

[SOLVED] How to use Solr Handlers when sending requests from Drupal

  • 2015-09-16 13:49:00
  • Loic O.
  • 112 View
  • 2 Score
  • 1 Answer
  • Tags:   search

1 Answered Questions

[SOLVED] Search API Views only displaying one result

  • 2015-08-14 22:19:46
  • Queenvictoria
  • 172 View
  • 3 Score
  • 1 Answer
  • Tags:   7 views search

2 Answered Questions

[SOLVED] How to use Solr Index to display node in a Google Map

3 Answered Questions

Sponsored Content