如何使用Search API Solr发送自定义Solr查询


8

我想使用Search API Solr将自定义的solr查询(最好在hook_init中)发送到特定的URL。

就像是:

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

我该怎么做呢?有现场例子吗?

谢谢。

Answers:


12

从RC4版本开始,您可以使用makeServletRequest()生成自定义的solr查询。在GitHub上查看一些示例

以下是一些代码示例:

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

上面的代码在Drupal 7Search API Solr [rc3]中在hook_init中进行了测试。


Acquia

如果您使用的是Acquia,则可以使用Acquia-Search-Proxy,该代理使您可以针对Acquia Search(Apachesolr + Search API Solr)发送自定义查询。另请参阅我的Acquia-Search-Proxy分支,其中包括一些其他改进。

通过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

我在http://nickveenhof.github.io/devdays_solr_wizardry/#/click-recipes-search-api的演示文稿中看到了一个示例。我不了解每一行代码(我的意思是-他为什么要添加“捆绑”过滤器并再次将其删除?),但我仍然希望它能对您有所帮助。

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

谢谢,这很好,但不幸的是,它用于Apachesolr,而不是通过使用Search API Solr。
kenorb

抱歉,我看不懂你的问题。我没有Search API的代码示例,但是看起来drupal.org/node/2004270中的文档和query.inc中的内联文档(尤其是SearchApiQuery类)非常详细。
marcvangend

2

SearchApiQuery提供了一个非常完整的工具来构建自定义查询。

一些代码示例:

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

2

为了向您的查询添加构面,请使用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);
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.