service.inc 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166
  1. <?php
  2. /**
  3. * Search service class using Solr server.
  4. */
  5. class SearchApiSolrService extends SearchApiAbstractService {
  6. /**
  7. * The date format that Solr uses, in PHP date() syntax.
  8. */
  9. const SOLR_DATE_FORMAT = 'Y-m-d\TH:i:s\Z';
  10. /**
  11. * The connection class used by this service.
  12. *
  13. * Must implement SearchApiSolrConnectionInterface.
  14. *
  15. * @var string
  16. */
  17. protected $connection_class = 'SearchApiSolrConnection';
  18. /**
  19. * A connection to the Solr server.
  20. *
  21. * @var SearchApiSolrConnectionInterface
  22. */
  23. protected $solr;
  24. /**
  25. * Static cache for getFieldNames().
  26. *
  27. * @var array
  28. */
  29. protected $fieldNames = array();
  30. /**
  31. * Metadata describing fields on the Solr/Lucene index.
  32. *
  33. * @see SearchApiSolrService::getFields().
  34. *
  35. * @var array
  36. */
  37. protected $fields;
  38. /**
  39. * Saves whether a commit operation was already scheduled for this server.
  40. *
  41. * @var bool
  42. */
  43. protected $commitScheduled = FALSE;
  44. /**
  45. * Request handler to use for this search query.
  46. *
  47. * @var string
  48. */
  49. protected $request_handler = NULL;
  50. /**
  51. * Overrides SearchApiAbstractService::configurationForm().
  52. */
  53. public function configurationForm(array $form, array &$form_state) {
  54. if ($this->options) {
  55. // Editing this server
  56. $form['server_description'] = array(
  57. '#type' => 'item',
  58. '#title' => t('Solr server URI'),
  59. '#description' => $this->getServerLink(),
  60. );
  61. }
  62. $options = $this->options + array(
  63. 'scheme' => 'http',
  64. 'host' => 'localhost',
  65. 'port' => '8983',
  66. 'path' => '/solr',
  67. 'http_user' => '',
  68. 'http_pass' => '',
  69. 'excerpt' => FALSE,
  70. 'retrieve_data' => FALSE,
  71. 'highlight_data' => FALSE,
  72. 'http_method' => 'AUTO',
  73. // Default to TRUE for new servers, but to FALSE for existing ones.
  74. 'clean_ids' => $this->options ? FALSE : TRUE,
  75. 'autocorrect_spell' => TRUE,
  76. 'autocorrect_suggest_words' => TRUE,
  77. );
  78. if (!$options['clean_ids']) {
  79. if (module_exists('advanced_help')) {
  80. $variables['@url']= url('help/search_api_solr/README.txt');
  81. }
  82. else {
  83. $variables['@url']= url(drupal_get_path('module', 'search_api_solr') . '/README.txt');
  84. }
  85. $description = t('Change Solr field names to be more compatible with advanced features. Doing this leads to re-indexing of all indexes on this server. See <a href="@url">README.txt</a> for details.', $variables);
  86. $form['clean_ids_form'] = array(
  87. '#type' => 'fieldset',
  88. '#title' => t('Clean field identifiers'),
  89. '#description' => $description,
  90. '#collapsible' => TRUE,
  91. );
  92. $form['clean_ids_form']['submit'] = array(
  93. '#type' => 'submit',
  94. '#value' => t('Switch to clean field identifiers'),
  95. '#submit' => array('_search_api_solr_switch_to_clean_ids'),
  96. );
  97. }
  98. $form['clean_ids'] = array(
  99. '#type' => 'value',
  100. '#value' => $options['clean_ids'],
  101. );
  102. $form['scheme'] = array(
  103. '#type' => 'select',
  104. '#title' => t('HTTP protocol'),
  105. '#description' => t('The HTTP protocol to use for sending queries.'),
  106. '#default_value' => $options['scheme'],
  107. '#options' => array(
  108. 'http' => 'http',
  109. 'https' => 'https',
  110. ),
  111. );
  112. $form['host'] = array(
  113. '#type' => 'textfield',
  114. '#title' => t('Solr host'),
  115. '#description' => t('The host name or IP of your Solr server, e.g. <code>localhost</code> or <code>www.example.com</code>.'),
  116. '#default_value' => $options['host'],
  117. '#required' => TRUE,
  118. );
  119. $form['port'] = array(
  120. '#type' => 'textfield',
  121. '#title' => t('Solr port'),
  122. '#description' => t('The Jetty example server is at port 8983, while Tomcat uses 8080 by default.'),
  123. '#default_value' => $options['port'],
  124. '#required' => TRUE,
  125. );
  126. $form['path'] = array(
  127. '#type' => 'textfield',
  128. '#title' => t('Solr path'),
  129. '#description' => t('The path that identifies the Solr instance to use on the server.'),
  130. '#default_value' => $options['path'],
  131. );
  132. $form['http'] = array(
  133. '#type' => 'fieldset',
  134. '#title' => t('Basic HTTP authentication'),
  135. '#description' => t('If your Solr server is protected by basic HTTP authentication, enter the login data here.'),
  136. '#collapsible' => TRUE,
  137. '#collapsed' => empty($options['http_user']),
  138. );
  139. $form['http']['http_user'] = array(
  140. '#type' => 'textfield',
  141. '#title' => t('Username'),
  142. '#default_value' => $options['http_user'],
  143. );
  144. $form['http']['http_pass'] = array(
  145. '#type' => 'password',
  146. '#title' => t('Password'),
  147. '#description' => t('If this field is left blank and the HTTP username is filled out, the current password will not be changed.'),
  148. );
  149. $form['advanced'] = array(
  150. '#type' => 'fieldset',
  151. '#title' => t('Advanced'),
  152. '#collapsible' => TRUE,
  153. '#collapsed' => TRUE,
  154. );
  155. $form['advanced']['excerpt'] = array(
  156. '#type' => 'checkbox',
  157. '#title' => t('Return an excerpt for all results'),
  158. '#description' => t("If search keywords are given, use Solr's capabilities to create a highlighted search excerpt for each result. " .
  159. 'Whether the excerpts will actually be displayed depends on the settings of the search, though.'),
  160. '#default_value' => $options['excerpt'],
  161. );
  162. $form['advanced']['retrieve_data'] = array(
  163. '#type' => 'checkbox',
  164. '#title' => t('Retrieve result data from Solr'),
  165. '#description' => t('When checked, result data will be retrieved directly from the Solr server. ' .
  166. 'This might make item loads unnecessary. Only indexed fields can be retrieved. ' .
  167. 'Note also that the returned field data might not always be correct, due to preprocessing and caching issues.'),
  168. '#default_value' => $options['retrieve_data'],
  169. );
  170. $form['advanced']['highlight_data'] = array(
  171. '#type' => 'checkbox',
  172. '#title' => t('Highlight retrieved data'),
  173. '#description' => t('When retrieving result data from the Solr server, try to highlight the search terms in the returned fulltext fields.'),
  174. '#default_value' => $options['highlight_data'],
  175. );
  176. // Highlighting retrieved data only makes sense when we retrieve data.
  177. // (Actually, internally it doesn't really matter. However, from a user's
  178. // perspective, having to check both probably makes sense.)
  179. $form['advanced']['highlight_data']['#states']['invisible']
  180. [':input[name="options[form][advanced][retrieve_data]"]']['checked'] = FALSE;
  181. $form['advanced']['http_method'] = array(
  182. '#type' => 'select',
  183. '#title' => t('HTTP method'),
  184. '#description' => t('The HTTP method to use for sending queries. GET will often fail with larger queries, while POST should not be cached. AUTO will use GET when possible, and POST for queries that are too large.'),
  185. '#default_value' => $options['http_method'],
  186. '#options' => array(
  187. 'AUTO' => t('AUTO'),
  188. 'POST' => 'POST',
  189. 'GET' => 'GET',
  190. ),
  191. );
  192. if (module_exists('search_api_autocomplete')) {
  193. $form['advanced']['autocomplete'] = array(
  194. '#type' => 'fieldset',
  195. '#title' => t('Autocomplete'),
  196. '#collapsible' => TRUE,
  197. '#collapsed' => TRUE,
  198. );
  199. $form['advanced']['autocomplete']['autocorrect_spell'] = array(
  200. '#type' => 'checkbox',
  201. '#title' => t('Use spellcheck for autocomplete suggestions'),
  202. '#description' => t('If activated, spellcheck suggestions ("Did you mean") will be included in the autocomplete suggestions. Since the used dictionary contains words from all indexes, this might lead to leaking of sensitive data, depending on your setup.'),
  203. '#default_value' => $options['autocorrect_spell'],
  204. );
  205. $form['advanced']['autocomplete']['autocorrect_suggest_words'] = array(
  206. '#type' => 'checkbox',
  207. '#title' => t('Suggest additional words'),
  208. '#description' => t('If activated and the user enters a complete word, Solr will suggest additional words the user wants to search, which are often found (not searched!) together. This has been known to lead to strange results in some configurations – if you see inappropriate additional-word suggestions, you might want to deactivate this option.'),
  209. '#default_value' => $options['autocorrect_suggest_words'],
  210. );
  211. }
  212. return $form;
  213. }
  214. /**
  215. * Overrides SearchApiAbstractService::configurationFormValidate().
  216. */
  217. public function configurationFormValidate(array $form, array &$values, array &$form_state) {
  218. if (isset($values['port']) && (!is_numeric($values['port']) || $values['port'] < 0 || $values['port'] > 65535)) {
  219. form_error($form['port'], t('The port has to be an integer between 0 and 65535.'));
  220. }
  221. }
  222. /**
  223. * Overrides SearchApiAbstractService::configurationFormSubmit().
  224. */
  225. public function configurationFormSubmit(array $form, array &$values, array &$form_state) {
  226. // Since the form is nested into another, we can't simply use #parents for
  227. // doing this array restructuring magic. (At least not without creating an
  228. // unnecessary dependency on internal implementation.)
  229. $values += $values['http'];
  230. $values += $values['advanced'];
  231. $values += !empty($values['autocomplete']) ? $values['autocomplete'] : array();
  232. unset($values['http'], $values['advanced'], $values['autocomplete']);
  233. // Highlighting retrieved data only makes sense when we retrieve data.
  234. $values['highlight_data'] &= $values['retrieve_data'];
  235. // For password fields, there is no default value, they're empty by default.
  236. // Therefore we ignore empty submissions if the user didn't change either.
  237. if ($values['http_pass'] === ''
  238. && isset($this->options['http_user'])
  239. && $values['http_user'] === $this->options['http_user']) {
  240. $values['http_pass'] = $this->options['http_pass'];
  241. }
  242. parent::configurationFormSubmit($form, $values, $form_state);
  243. }
  244. /**
  245. * {@inheritdoc}
  246. */
  247. public function supportsFeature($feature) {
  248. // First, check the features we always support.
  249. $supported = drupal_map_assoc(array(
  250. 'search_api_autocomplete',
  251. 'search_api_facets',
  252. 'search_api_facets_operator_or',
  253. 'search_api_grouping',
  254. 'search_api_mlt',
  255. 'search_api_multi',
  256. 'search_api_service_extra',
  257. 'search_api_spellcheck',
  258. 'search_api_data_type_location',
  259. 'search_api_data_type_geohash',
  260. ));
  261. if (isset($supported[$feature])) {
  262. return TRUE;
  263. }
  264. // If it is a custom data type, maybe we support it automatically via
  265. // search_api_solr_hook_search_api_data_type_info().
  266. if (substr($feature, 0, 21) != 'search_api_data_type_') {
  267. return FALSE;
  268. }
  269. $type = substr($feature, 21);
  270. $type = search_api_get_data_type_info($type);
  271. // We only support it if the "prefix" key is set.
  272. return $type && !empty($type['prefix']);
  273. }
  274. /**
  275. * Overrides SearchApiAbstractService::viewSettings().
  276. *
  277. * Returns an empty string since information is instead added via
  278. * getExtraInformation().
  279. */
  280. public function viewSettings() {
  281. return '';
  282. }
  283. /**
  284. * {@inheritdoc}
  285. */
  286. public function getExtraInformation() {
  287. $info = array();
  288. $info[] = array(
  289. 'label' => t('Solr server URI'),
  290. 'info' => $this->getServerLink(),
  291. );
  292. if ($this->options['http_user']) {
  293. $vars = array(
  294. '@user' => $this->options['http_user'],
  295. '@pass' => str_repeat('*', strlen($this->options['http_pass'])),
  296. );
  297. $http = t('Username: @user; Password: @pass', $vars);
  298. $info[] = array(
  299. 'label' => t('Basic HTTP authentication'),
  300. 'info' => $http,
  301. );
  302. }
  303. if ($this->server->enabled) {
  304. // If the server is enabled, check whether Solr can be reached.
  305. $ping = $this->ping();
  306. if ($ping) {
  307. $msg = t('The Solr server could be reached (latency: @millisecs ms).', array('@millisecs' => $ping * 1000));
  308. }
  309. else {
  310. $msg = t('The Solr server could not be reached. Further data is therefore unavailable.');
  311. }
  312. $info[] = array(
  313. 'label' => t('Connection'),
  314. 'info' => $msg,
  315. 'status' => $ping ? 'ok' : 'error',
  316. );
  317. if ($ping) {
  318. try {
  319. // If Solr can be reached, provide more information. This isn't done
  320. // often (only when an admin views the server details), so we clear the
  321. // cache to get the current data.
  322. $this->connect();
  323. $this->solr->clearCache();
  324. $data = $this->solr->getLuke();
  325. if (isset($data->index->numDocs)) {
  326. // Collect the stats
  327. $stats_summary = $this->solr->getStatsSummary();
  328. $pending_msg = $stats_summary['@pending_docs'] ? t('(@pending_docs sent but not yet processed)', $stats_summary) : '';
  329. $index_msg = $stats_summary['@index_size'] ? t('(@index_size on disk)', $stats_summary) : '';
  330. $indexed_message = t('@num items !pending !index_msg', array(
  331. '@num' => $data->index->numDocs,
  332. '!pending' => $pending_msg,
  333. '!index_msg' => $index_msg,
  334. ));
  335. $info[] = array(
  336. 'label' => t('Indexed'),
  337. 'info' => $indexed_message,
  338. );
  339. if (!empty($stats_summary['@deletes_total'])) {
  340. $info[] = array(
  341. 'label' => t('Pending Deletions'),
  342. 'info' => $stats_summary['@deletes_total'],
  343. );
  344. }
  345. $info[] = array(
  346. 'label' => t('Delay'),
  347. 'info' => t('@autocommit_time before updates are processed.', $stats_summary),
  348. );
  349. $status = 'ok';
  350. if (substr($stats_summary['@schema_version'], 0, 10) == 'search-api') {
  351. drupal_set_message(t('Your schema.xml version is too old. Please replace all configuration files with the ones packaged with this module and re-index you data.'), 'error');
  352. $status = 'error';
  353. }
  354. elseif (substr($stats_summary['@schema_version'], 0, 9) != 'drupal-4.') {
  355. $variables['@url'] = url(drupal_get_path('module', 'search_api_solr') . '/INSTALL.txt');
  356. $message = t('You are using an incompatible schema.xml configuration file. Please follow the instructions in the <a href="@url">INSTALL.txt</a> file for setting up Solr.', $variables);
  357. drupal_set_message($message, 'error');
  358. $status = 'error';
  359. }
  360. $info[] = array(
  361. 'label' => t('Schema'),
  362. 'info' => $stats_summary['@schema_version'],
  363. 'status' => $status,
  364. );
  365. if (!empty($stats_summary['@core_name'])) {
  366. $info[] = array(
  367. 'label' => t('Solr Core Name'),
  368. 'info' => $stats_summary['@core_name'],
  369. );
  370. }
  371. }
  372. }
  373. catch (SearchApiException $e) {
  374. $info[] = array(
  375. 'label' => t('Additional information'),
  376. 'info' => t('An error occurred while trying to retrieve additional information from the Solr server: @msg.', array('@msg' => $e->getMessage())),
  377. 'status' => 'error',
  378. );
  379. }
  380. }
  381. }
  382. return $info;
  383. }
  384. /**
  385. * Returns a link to the Solr server, if the necessary options are set.
  386. */
  387. public function getServerLink() {
  388. if (!$this->options) {
  389. return '';
  390. }
  391. $host = $this->options['host'];
  392. if ($host == 'localhost' && !empty($_SERVER['SERVER_NAME'])) {
  393. $host = $_SERVER['SERVER_NAME'];
  394. }
  395. $url = $this->options['scheme'] . '://' . $host . ':' . $this->options['port'] . $this->options['path'];
  396. return l($url, $url);
  397. }
  398. /**
  399. * Create a connection to the Solr server as configured in $this->options.
  400. */
  401. protected function connect() {
  402. if (!$this->solr) {
  403. if (!class_exists($this->connection_class)) {
  404. throw new SearchApiException(t('Invalid class @class set as Solr connection class.', array('@class' => $this->connection_class)));
  405. }
  406. $options = $this->options + array('server' => $this->server->machine_name);
  407. $this->solr = new $this->connection_class($options);
  408. if (!($this->solr instanceof SearchApiSolrConnectionInterface)) {
  409. $this->solr = NULL;
  410. throw new SearchApiException(t('Invalid class @class set as Solr connection class.', array('@class' => $this->connection_class)));
  411. }
  412. }
  413. }
  414. /**
  415. * Overrides SearchApiAbstractService::addIndex().
  416. */
  417. public function addIndex(SearchApiIndex $index) {
  418. if (module_exists('search_api_multi') && module_exists('search_api_views')) {
  419. views_invalidate_cache();
  420. }
  421. }
  422. /**
  423. * Overrides SearchApiAbstractService::fieldsUpdated().
  424. */
  425. public function fieldsUpdated(SearchApiIndex $index) {
  426. if (module_exists('search_api_multi') && module_exists('search_api_views')) {
  427. views_invalidate_cache();
  428. }
  429. return TRUE;
  430. }
  431. /**
  432. * Overrides SearchApiAbstractService::removeIndex().
  433. */
  434. public function removeIndex($index) {
  435. if (module_exists('search_api_multi') && module_exists('search_api_views')) {
  436. views_invalidate_cache();
  437. }
  438. $id = is_object($index) ? $index->machine_name : $index;
  439. // Only delete the index's data if the index isn't read-only.
  440. if (!is_object($index) || empty($index->read_only)) {
  441. $this->connect();
  442. try {
  443. $this->solr->deleteByQuery("index_id:" . $this->getIndexId($id));
  444. }
  445. catch (Exception $e) {
  446. throw new SearchApiException($e->getMessage());
  447. }
  448. }
  449. }
  450. /**
  451. * Implements SearchApiServiceInterface::indexItems().
  452. */
  453. public function indexItems(SearchApiIndex $index, array $items) {
  454. $documents = array();
  455. $ret = array();
  456. $index_id = $this->getIndexId($index->machine_name);
  457. $fields = $this->getFieldNames($index);
  458. foreach ($items as $id => $item) {
  459. try {
  460. $doc = new SearchApiSolrDocument();
  461. $doc->setField('id', $this->createId($index_id, $id));
  462. $doc->setField('index_id', $index_id);
  463. $doc->setField('item_id', $id);
  464. foreach ($item as $key => $field) {
  465. if (!isset($fields[$key])) {
  466. throw new SearchApiException(t('Unknown field @field.', array('@field' => $key)));
  467. }
  468. $this->addIndexField($doc, $fields[$key], $field['value'], $field['type']);
  469. }
  470. $documents[] = $doc;
  471. $ret[] = $id;
  472. }
  473. catch (Exception $e) {
  474. watchdog_exception('search_api_solr', $e, "%type while indexing @type with ID @id: !message in %function (line %line of %file).", array('@type' => $index->item_type, '@id' => $id), WATCHDOG_WARNING);
  475. }
  476. }
  477. // Let other modules alter documents before sending them to solr.
  478. drupal_alter('search_api_solr_documents', $documents, $index, $items);
  479. $this->alterSolrDocuments($documents, $index, $items);
  480. if (!$documents) {
  481. return array();
  482. }
  483. try {
  484. $this->connect();
  485. $this->solr->addDocuments($documents);
  486. if (!empty($index->options['index_directly'])) {
  487. $this->scheduleCommit();
  488. }
  489. return $ret;
  490. }
  491. catch (SearchApiException $e) {
  492. watchdog_exception('search_api_solr', $e, "%type while indexing: !message in %function (line %line of %file).");
  493. }
  494. return array();
  495. }
  496. /**
  497. * Creates an ID used as the unique identifier at the Solr server.
  498. *
  499. * This has to consist of both index and item ID.
  500. */
  501. protected function createId($index_id, $item_id) {
  502. return "$index_id-$item_id";
  503. }
  504. /**
  505. * Create a list of all indexed field names mapped to their Solr field names.
  506. *
  507. * The special fields "search_api_id" and "search_api_relevance" are also
  508. * included. Any Solr fields that exist on search results are mapped back to
  509. * to their local field names in the final result set.
  510. *
  511. * @see SearchApiSolrService::search()
  512. */
  513. public function getFieldNames(SearchApiIndex $index, $reset = FALSE) {
  514. if (!isset($this->fieldNames[$index->machine_name]) || $reset) {
  515. // This array maps "local property name" => "solr doc property name".
  516. $ret = array(
  517. 'search_api_id' => 'item_id',
  518. 'search_api_relevance' => 'score',
  519. );
  520. // Add the names of any fields configured on the index.
  521. $fields = (isset($index->options['fields']) ? $index->options['fields'] : array());
  522. foreach ($fields as $key => $field) {
  523. // Generate a field name; this corresponds with naming conventions in
  524. // our schema.xml
  525. $type = $field['type'];
  526. // Use the real type of the field if the server supports this type.
  527. if (isset($field['real_type'])) {
  528. $custom_type = search_api_extract_inner_type($field['real_type']);
  529. if ($this->supportsFeature('search_api_data_type_' . $custom_type)) {
  530. $type = $field['real_type'];
  531. }
  532. }
  533. $inner_type = search_api_extract_inner_type($type);
  534. $type_info = search_api_solr_get_data_type_info($inner_type);
  535. $pref = isset($type_info['prefix']) ? $type_info['prefix']: '';
  536. if (empty($type_info['always multiValued'])) {
  537. $pref .= ($type == $inner_type) ? 's' : 'm';
  538. }
  539. if (!empty($this->options['clean_ids'])) {
  540. $name = $pref . '_' . str_replace(':', '$', $key);
  541. }
  542. else {
  543. $name = $pref . '_' . $key;
  544. }
  545. $ret[$key] = $name;
  546. }
  547. // Let modules adjust the field mappings.
  548. drupal_alter('search_api_solr_field_mapping', $index, $ret);
  549. $this->fieldNames[$index->machine_name] = $ret;
  550. }
  551. return $this->fieldNames[$index->machine_name];
  552. }
  553. /**
  554. * Helper method for indexing.
  555. *
  556. * Adds $value with field name $key to the document $doc. The format of $value
  557. * is the same as specified in SearchApiServiceInterface::indexItems().
  558. */
  559. protected function addIndexField(SearchApiSolrDocument $doc, $key, $value, $type, $multi_valued = FALSE) {
  560. // Don't index empty values (i.e., when field is missing).
  561. if (!isset($value)) {
  562. return;
  563. }
  564. if (search_api_is_list_type($type)) {
  565. $type = substr($type, 5, -1);
  566. foreach ($value as $v) {
  567. $this->addIndexField($doc, $key, $v, $type, TRUE);
  568. }
  569. return;
  570. }
  571. switch ($type) {
  572. case 'tokens':
  573. foreach ($value as $v) {
  574. $doc->addField($key, $v['value']);
  575. }
  576. return;
  577. case 'boolean':
  578. $value = $value ? 'true' : 'false';
  579. break;
  580. case 'date':
  581. $value = is_numeric($value) ? (int) $value : strtotime($value);
  582. if ($value === FALSE) {
  583. return;
  584. }
  585. $value = format_date($value, 'custom', self::SOLR_DATE_FORMAT, 'UTC');
  586. break;
  587. case 'integer':
  588. $value = (int) $value;
  589. break;
  590. case 'decimal':
  591. $value = (float) $value;
  592. break;
  593. }
  594. if ($multi_valued) {
  595. $doc->addField($key, $value);
  596. }
  597. else {
  598. $doc->setField($key, $value);
  599. }
  600. }
  601. /**
  602. * Applies custom modifications to indexed Solr documents.
  603. *
  604. * This method allows subclasses to easily apply custom changes before the
  605. * documents are sent to Solr. The method is empty by default.
  606. *
  607. * @param array $documents
  608. * An array of SearchApiSolrDocument objects ready to be indexed, generated
  609. * from $items array.
  610. * @param SearchApiIndex $index
  611. * The search index for which items are being indexed.
  612. * @param array $items
  613. * An array of items being indexed.
  614. *
  615. * @see hook_search_api_solr_documents_alter()
  616. */
  617. protected function alterSolrDocuments(array &$documents, SearchApiIndex $index, array $items) {
  618. }
  619. /**
  620. * Implements SearchApiServiceInterface::deleteItems().
  621. *
  622. * This method has a custom, Solr-specific extension:
  623. *
  624. * If $ids is a string other than "all", it is treated as a Solr query. All
  625. * items matching that Solr query are then deleted. If $index is additionally
  626. * specified, then only those items also lying on that index will be deleted.
  627. *
  628. * It is up to the caller to ensure $ids is a valid query when the method is
  629. * called in this fashion.
  630. */
  631. public function deleteItems($ids = 'all', SearchApiIndex $index = NULL) {
  632. $this->connect();
  633. if ($index) {
  634. $index_id = $this->getIndexId($index->machine_name);
  635. if (is_array($ids)) {
  636. $solr_ids = array();
  637. foreach ($ids as $id) {
  638. $solr_ids[] = $this->createId($index_id, $id);
  639. }
  640. $this->solr->deleteByMultipleIds($solr_ids);
  641. }
  642. elseif ($ids == 'all') {
  643. $this->solr->deleteByQuery("index_id:" . $index_id);
  644. }
  645. else {
  646. $this->solr->deleteByQuery("index_id:" . $index_id . ' (' . $ids . ')');
  647. }
  648. }
  649. else {
  650. $q = $ids == 'all' ? '*:*' : $ids;
  651. $this->solr->deleteByQuery($q);
  652. }
  653. $this->scheduleCommit();
  654. }
  655. /**
  656. * Implements SearchApiServiceInterface::search().
  657. */
  658. public function search(SearchApiQueryInterface $query) {
  659. $time_method_called = microtime(TRUE);
  660. // Reset request handler.
  661. $this->request_handler = NULL;
  662. // Get field information.
  663. $index = $query->getIndex();
  664. $index_id = $this->getIndexId($index->machine_name);
  665. $fields = $this->getFieldNames($index);
  666. // Get Solr connection.
  667. $this->connect();
  668. $version = $this->solr->getSolrVersion();
  669. // Extract keys.
  670. $keys = $query->getKeys();
  671. if (is_array($keys)) {
  672. $keys = $this->flattenKeys($keys);
  673. }
  674. // Set searched fields.
  675. $options = $query->getOptions();
  676. $search_fields = $query->getFields();
  677. // Get the index fields to be able to retrieve boosts.
  678. $index_fields = $index->getFields();
  679. $qf = array();
  680. foreach ($search_fields as $f) {
  681. $boost = '';
  682. $boost = isset($index_fields[$f]['boost']) ? '^' . $index_fields[$f]['boost'] : '';
  683. $qf[] = $fields[$f] . $boost;
  684. }
  685. // Extract filters.
  686. $filter = $query->getFilter();
  687. $fq = $this->createFilterQueries($filter, $fields, $index->options['fields']);
  688. $fq[] = 'index_id:' . $index_id;
  689. // Extract sort.
  690. $sort = array();
  691. foreach ($query->getSort() as $field => $order) {
  692. $f = $fields[$field];
  693. if (substr($f, 0, 3) == 'ss_') {
  694. $f = 'sort_' . substr($f, 3);
  695. }
  696. $order = strtolower($order);
  697. $sort[$field] = "$f $order";
  698. }
  699. // Get facet fields.
  700. $facets = $query->getOption('search_api_facets', array());
  701. $facet_params = $this->getFacetParams($facets, $fields, $fq);
  702. // Handle highlighting.
  703. $highlight_params = $this->getHighlightParams($query);
  704. // Handle More Like This query.
  705. $mlt = $query->getOption('search_api_mlt');
  706. if ($mlt) {
  707. $mlt_params['qt'] = 'mlt';
  708. // The fields to look for similarities in.
  709. $mlt_fl = array();
  710. foreach($mlt['fields'] as $f) {
  711. // Solr 4 has a bug which results in numeric fields not being supported
  712. // in MLT queries.
  713. // Date fields don't seem to be supported at all.
  714. if ($fields[$f][0] === 'd' || ($version == 4 && in_array($fields[$f][0], array('i', 'f')))) {
  715. continue;
  716. }
  717. $mlt_fl[] = $fields[$f];
  718. // For non-text fields, set minimum word length to 0.
  719. if (isset($index->options['fields'][$f]['type']) && !search_api_is_text_type($index->options['fields'][$f]['type'])) {
  720. $mlt_params['f.' . $fields[$f] . '.mlt.minwl'] = 0;
  721. }
  722. }
  723. $mlt_params['mlt.fl'] = implode(',', $mlt_fl);
  724. $id = $this->createId($index_id, $mlt['id']);
  725. $id = call_user_func(array($this->connection_class, 'phrase'), $id);
  726. $keys = 'id:' . $id;
  727. }
  728. // Handle spatial filters.
  729. if ($spatials = $query->getOption('search_api_location')) {
  730. foreach ($spatials as $i => $spatial) {
  731. if (empty($spatial['field']) || empty($spatial['lat']) || empty($spatial['lon'])) {
  732. continue;
  733. }
  734. unset($radius);
  735. $field = $fields[$spatial['field']];
  736. $escaped_field = SearchApiSolrConnection::escapeFieldName($field);
  737. $point = ((float) $spatial['lat']) . ',' . ((float) $spatial['lon']);
  738. // Prepare the filter settings.
  739. if (isset($spatial['radius'])) {
  740. $radius = (float) $spatial['radius'];
  741. }
  742. $spatial_method = 'geofilt';
  743. if (isset($spatial['method']) && in_array($spatial['method'], array('geofilt', 'bbox'))) {
  744. $spatial_method = $spatial['method'];
  745. }
  746. // Change the fq facet ranges to the correct fq.
  747. foreach ($fq as $key => $value) {
  748. // If the fq consists only of a filter on this field, replace it with
  749. // a range.
  750. $preg_field = preg_quote($escaped_field, '/');
  751. if (preg_match('/^' . $preg_field . ':\["?(\*|\d+(?:\.\d+)?)"? TO "?(\*|\d+(?:\.\d+)?)"?\]$/', $value, $m)) {
  752. unset($fq[$key]);
  753. if ($m[1] && is_numeric($m[1])) {
  754. $min_radius = isset($min_radius) ? max($min_radius, $m[1]) : $m[1];
  755. }
  756. if (is_numeric($m[2])) {
  757. // Make the radius tighter accordingly.
  758. $radius = isset($radius) ? min($radius, $m[2]) : $m[2];
  759. }
  760. }
  761. }
  762. // If either a radius was given in the option, or a filter was
  763. // encountered, set a filter for the lowest value. If a lower boundary
  764. // was set (too), we can only set a filter for that if the field name
  765. // doesn't contains any colons.
  766. if (isset($min_radius) && strpos($field, ':') === FALSE) {
  767. $upper = isset($radius) ? " u=$radius" : '';
  768. $fq[] = "{!frange l=$min_radius$upper}geodist($field,$point)";
  769. }
  770. elseif (isset($radius)) {
  771. $fq[] = "{!$spatial_method pt=$point sfield=$field d=$radius}";
  772. }
  773. // Change sort on the field, if set (and not already changed).
  774. if (isset($sort[$spatial['field']]) && substr($sort[$spatial['field']], 0, strlen($field)) === $field) {
  775. if (strpos($field, ':') === FALSE) {
  776. $sort[$spatial['field']] = str_replace($field, "geodist($field,$point)", $sort[$spatial['field']]);
  777. }
  778. else {
  779. $link = l(t('edit server'), 'admin/config/search/search_api/server/' . $this->server->machine_name . '/edit');
  780. watchdog('search_api_solr', 'Location sort on field @field had to be ignored because unclean field identifiers are used.', array('@field' => $spatial['field']), WATCHDOG_WARNING, $link);
  781. }
  782. }
  783. // Change the facet parameters for spatial fields to return distance
  784. // facets.
  785. if (!empty($facets)) {
  786. if (!empty($facet_params['facet.field'])) {
  787. $facet_params['facet.field'] = array_diff($facet_params['facet.field'], array($field));
  788. }
  789. foreach ($facets as $delta => $facet) {
  790. if ($facet['field'] != $spatial['field']) {
  791. continue;
  792. }
  793. $steps = $facet['limit'] > 0 ? $facet['limit'] : 5;
  794. $step = (isset($radius) ? $radius : 100) / $steps;
  795. for ($k = $steps - 1; $k > 0; --$k) {
  796. $distance = $step * $k;
  797. $key = "spatial-$delta-$distance";
  798. $facet_params['facet.query'][] = "{!$spatial_method pt=$point sfield=$field d=$distance key=$key}";
  799. }
  800. foreach (array('limit', 'mincount', 'missing') as $setting) {
  801. unset($facet_params["f.$field.facet.$setting"]);
  802. }
  803. }
  804. }
  805. }
  806. }
  807. // Normal sorting on location fields isn't possible.
  808. foreach ($sort as $field => $sort_param) {
  809. if (substr($sort_param, 0, 3) === 'loc') {
  810. unset($sort[$field]);
  811. }
  812. }
  813. // Handle field collapsing / grouping.
  814. $grouping = $query->getOption('search_api_grouping');
  815. if (!empty($grouping['use_grouping'])) {
  816. $group_params['group'] = 'true';
  817. // We always want the number of groups returned so that we get pagers done
  818. // right.
  819. $group_params['group.ngroups'] = 'true';
  820. if (!empty($grouping['truncate'])) {
  821. $group_params['group.truncate'] = 'true';
  822. }
  823. if (!empty($grouping['group_facet'])) {
  824. $group_params['group.facet'] = 'true';
  825. }
  826. foreach ($grouping['fields'] as $collapse_field) {
  827. $type = $index_fields[$collapse_field]['type'];
  828. // Only single-valued fields are supported.
  829. if ($version < 4) {
  830. // For Solr 3.x, only string and boolean fields are supported.
  831. if (search_api_is_list_type($type) || !search_api_is_text_type($type, array('string', 'boolean', 'uri'))) {
  832. $warnings[] = t('Grouping is not supported for field @field. ' .
  833. 'Only single-valued fields of type "String", "Boolean" or "URI" are supported.',
  834. array('@field' => $index_fields[$collapse_field]['name']));
  835. continue;
  836. }
  837. }
  838. else {
  839. if (search_api_is_list_type($type) || search_api_is_text_type($type)) {
  840. $warnings[] = t('Grouping is not supported for field @field. ' .
  841. 'Only single-valued fields not indexed as "Fulltext" are supported.',
  842. array('@field' => $index_fields[$collapse_field]['name']));
  843. continue;
  844. }
  845. }
  846. $group_params['group.field'][] = $fields[$collapse_field];
  847. }
  848. if (empty($group_params['group.field'])) {
  849. unset($group_params);
  850. }
  851. else {
  852. if (!empty($grouping['group_sort'])) {
  853. foreach ($grouping['group_sort'] as $group_sort_field => $order) {
  854. if (isset($fields[$group_sort_field])) {
  855. $f = $fields[$group_sort_field];
  856. if (substr($f, 0, 3) == 'ss_') {
  857. $f = 'sort_' . substr($f, 3);
  858. }
  859. $order = strtolower($order);
  860. $group_params['group.sort'][] = $f . ' ' . $order;
  861. }
  862. }
  863. if (!empty($group_params['group.sort'])) {
  864. $group_params['group.sort'] = implode(', ', $group_params['group.sort']);
  865. }
  866. }
  867. if (!empty($grouping['group_limit']) && ($grouping['group_limit'] != 1)) {
  868. $group_params['group.limit'] = $grouping['group_limit'];
  869. }
  870. }
  871. }
  872. // Set defaults.
  873. if (!$keys) {
  874. $keys = NULL;
  875. }
  876. // Collect parameters.
  877. $params = array(
  878. 'fl' => 'item_id,score',
  879. 'qf' => $qf,
  880. 'fq' => $fq,
  881. );
  882. if (isset($options['offset'])) {
  883. $params['start'] = $options['offset'];
  884. }
  885. $params['rows'] = isset($options['limit']) ? $options['limit'] : 1000000;
  886. if ($sort) {
  887. $params['sort'] = implode(', ', $sort);
  888. }
  889. if (!empty($facet_params['facet.field'])) {
  890. $params += $facet_params;
  891. }
  892. if (!empty($highlight_params)) {
  893. $params += $highlight_params;
  894. }
  895. if (!empty($options['search_api_spellcheck'])) {
  896. $params['spellcheck'] = 'true';
  897. }
  898. if (!empty($mlt_params['mlt.fl'])) {
  899. $params += $mlt_params;
  900. }
  901. if (!empty($group_params)) {
  902. $params += $group_params;
  903. }
  904. if (!empty($this->options['retrieve_data'])) {
  905. $params['fl'] = '*,score';
  906. }
  907. // Retrieve http method from server options.
  908. $http_method = !empty($this->options['http_method']) ? $this->options['http_method'] : 'AUTO';
  909. $call_args = array(
  910. 'query' => &$keys,
  911. 'params' => &$params,
  912. 'http_method' => &$http_method,
  913. );
  914. if ($this->request_handler) {
  915. $this->setRequestHandler($this->request_handler, $call_args);
  916. }
  917. try {
  918. // Send search request.
  919. $time_processing_done = microtime(TRUE);
  920. drupal_alter('search_api_solr_query', $call_args, $query);
  921. $this->preQuery($call_args, $query);
  922. $response = $this->solr->search($keys, $params, $http_method);
  923. $time_query_done = microtime(TRUE);
  924. // Extract results.
  925. $results = $this->extractResults($query, $response);
  926. // Add warnings, if present.
  927. if (!empty($warnings)) {
  928. $results['warnings'] = isset($results['warnings']) ? array_merge($warnings, $results['warnings']) : $warnings;
  929. }
  930. // Extract facets.
  931. if ($facets = $this->extractFacets($query, $response)) {
  932. $results['search_api_facets'] = $facets;
  933. }
  934. drupal_alter('search_api_solr_search_results', $results, $query, $response);
  935. $this->postQuery($results, $query, $response);
  936. // Compute performance.
  937. $time_end = microtime(TRUE);
  938. $results['performance'] = array(
  939. 'complete' => $time_end - $time_method_called,
  940. 'preprocessing' => $time_processing_done - $time_method_called,
  941. 'execution' => $time_query_done - $time_processing_done,
  942. 'postprocessing' => $time_end - $time_query_done,
  943. );
  944. return $results;
  945. }
  946. catch (SearchApiException $e) {
  947. throw new SearchApiException(t('An error occurred while trying to search with Solr: @msg.', array('@msg' => $e->getMessage())));
  948. }
  949. }
  950. /**
  951. * Extract results from a Solr response.
  952. *
  953. * @param object $response
  954. * A HTTP response object.
  955. *
  956. * @return array
  957. * An array with two keys:
  958. * - result count: The number of total results.
  959. * - results: An array of search results, as specified by
  960. * SearchApiQueryInterface::execute().
  961. */
  962. protected function extractResults(SearchApiQueryInterface $query, $response) {
  963. $index = $query->getIndex();
  964. $fields = $this->getFieldNames($index);
  965. $field_options = $index->options['fields'];
  966. // Set up the results array.
  967. $results = array();
  968. $results['results'] = array();
  969. // Keep a copy of the response in the results so it's possible to extract
  970. // further useful information out of it, if necessary.
  971. $results['search_api_solr_response'] = $response;
  972. // In some rare cases (e.g., MLT query with nonexistent ID) the response
  973. // will be NULL.
  974. if (!isset($response->response) && !isset($response->grouped)) {
  975. $results['result count'] = 0;
  976. return $results;
  977. }
  978. // If field collapsing has been enabled for this query, we need to process
  979. // the results differently.
  980. $grouping = $query->getOption('search_api_grouping');
  981. if (!empty($grouping['use_grouping']) && !empty($response->grouped)) {
  982. $docs = array();
  983. $results['result count'] = 0;
  984. foreach ($grouping['fields'] as $field) {
  985. if (!empty($response->grouped->{$fields[$field]})) {
  986. $results['result count'] += $response->grouped->{$fields[$field]}->ngroups;
  987. foreach ($response->grouped->{$fields[$field]}->groups as $group) {
  988. foreach ($group->doclist->docs as $doc) {
  989. $docs[] = $doc;
  990. }
  991. }
  992. }
  993. }
  994. }
  995. else {
  996. $results['result count'] = $response->response->numFound;
  997. $docs = $response->response->docs;
  998. }
  999. // Add each search result to the results array.
  1000. foreach ($docs as $doc) {
  1001. // Blank result array.
  1002. $result = array(
  1003. 'id' => NULL,
  1004. 'score' => NULL,
  1005. 'fields' => array(),
  1006. );
  1007. // Extract properties from the Solr document, translating from Solr to
  1008. // Search API property names. This reverses the mapping in
  1009. // SearchApiSolrService::getFieldNames().
  1010. foreach ($fields as $search_api_property => $solr_property) {
  1011. if (isset($doc->{$solr_property})) {
  1012. $result['fields'][$search_api_property] = $doc->{$solr_property};
  1013. // Date fields need some special treatment to become valid date values
  1014. // (i.e., timestamps) again.
  1015. if (isset($field_options[$search_api_property]['type'])
  1016. && $field_options[$search_api_property]['type'] == 'date'
  1017. && preg_match('/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z$/', $result['fields'][$search_api_property])) {
  1018. $result['fields'][$search_api_property] = strtotime($result['fields'][$search_api_property]);
  1019. }
  1020. }
  1021. }
  1022. // We can find the item id and score in the special 'search_api_*'
  1023. // properties. Mappings are provided for these properties in
  1024. // SearchApiSolrService::getFieldNames().
  1025. $result['id'] = $result['fields']['search_api_id'];
  1026. $result['score'] = $result['fields']['search_api_relevance'];
  1027. $index_id = $this->getIndexId($index->machine_name);
  1028. $solr_id = $this->createId($index_id, $result['id']);
  1029. $excerpt = $this->getExcerpt($response, $solr_id, $result['fields'], $fields);
  1030. if ($excerpt) {
  1031. $result['excerpt'] = $excerpt;
  1032. }
  1033. // Use the result's id as the array key. By default, 'id' is mapped to
  1034. // 'item_id' in SearchApiSolrService::getFieldNames().
  1035. if ($result['id']) {
  1036. $results['results'][$result['id']] = $result;
  1037. }
  1038. }
  1039. // Check for spellcheck suggestions.
  1040. if (module_exists('search_api_spellcheck') && $query->getOption('search_api_spellcheck')) {
  1041. $results['search_api_spellcheck'] = new SearchApiSpellcheckSolr($response);
  1042. }
  1043. return $results;
  1044. }
  1045. /**
  1046. * Extract and format highlighting information for a specific item from a Solr response.
  1047. *
  1048. * Will also use highlighted fields to replace retrieved field data, if the
  1049. * corresponding option is set.
  1050. */
  1051. protected function getExcerpt($response, $id, array &$fields, array $field_mapping) {
  1052. if (!isset($response->highlighting->$id)) {
  1053. return FALSE;
  1054. }
  1055. $output = '';
  1056. if (!empty($this->options['excerpt']) && !empty($response->highlighting->$id->spell)) {
  1057. foreach ($response->highlighting->$id->spell as $snippet) {
  1058. $snippet = strip_tags($snippet);
  1059. $snippet = preg_replace('/^.*>|<.*$/', '', $snippet);
  1060. $snippet = $this->formatHighlighting($snippet);
  1061. // The created fragments sometimes have leading or trailing punctuation.
  1062. // We remove that here for all common cases, but take care not to remove
  1063. // < or > (so HTML tags stay valid).
  1064. $snippet = trim($snippet, "\00..\x2F:;=\x3F..\x40\x5B..\x60");
  1065. $output .= $snippet . ' … ';
  1066. }
  1067. }
  1068. if (!empty($this->options['highlight_data'])) {
  1069. foreach ($field_mapping as $search_api_property => $solr_property) {
  1070. if (substr($solr_property, 0, 3) == 'tm_' && !empty($response->highlighting->$id->$solr_property)) {
  1071. // Contrary to above, we here want to preserve HTML, so we just
  1072. // replace the [HIGHLIGHT] tags with the appropriate format.
  1073. $fields[$search_api_property] = $this->formatHighlighting($response->highlighting->$id->$solr_property);
  1074. }
  1075. }
  1076. }
  1077. return $output;
  1078. }
  1079. /**
  1080. * Changes highlighting tags from our custom, HTML-safe ones to HTML.
  1081. *
  1082. * @param string|array $snippet
  1083. * The snippet(s) to format.
  1084. *
  1085. * @return string|array
  1086. * The snippet(s), properly formatted as HTML.
  1087. */
  1088. protected function formatHighlighting($snippet) {
  1089. return preg_replace('#\[(/?)HIGHLIGHT\]#', '<$1strong>', $snippet);
  1090. }
  1091. /**
  1092. * Extract facets from a Solr response.
  1093. *
  1094. * @param object $response
  1095. * A response object from SolrPhpClient.
  1096. *
  1097. * @return array
  1098. * An array describing facets that apply to the current results.
  1099. */
  1100. protected function extractFacets(SearchApiQueryInterface $query, $response) {
  1101. $facets = array();
  1102. if (!isset($response->facet_counts)) {
  1103. return $facets;
  1104. }
  1105. $index = $query->getIndex();
  1106. $fields = $this->getFieldNames($index);
  1107. $extract_facets = $query->getOption('search_api_facets', array());
  1108. if (isset($response->facet_counts->facet_fields)) {
  1109. $facet_fields = $response->facet_counts->facet_fields;
  1110. foreach ($extract_facets as $delta => $info) {
  1111. $field = $fields[$info['field']];
  1112. if (!empty($facet_fields->$field)) {
  1113. $min_count = $info['min_count'];
  1114. $terms = $facet_fields->$field;
  1115. if ($info['missing']) {
  1116. // We have to correctly incorporate the "_empty_" term.
  1117. // This will ensure that the term with the least results is dropped,
  1118. // if the limit would be exceeded.
  1119. if (isset($terms->_empty_)) {
  1120. if ($terms->_empty_ < $min_count) {
  1121. unset($terms->_empty_);
  1122. }
  1123. else {
  1124. $terms = (array) $terms;
  1125. arsort($terms);
  1126. if ($info['limit'] > 0 && count($terms) > $info['limit']) {
  1127. array_pop($terms);
  1128. }
  1129. }
  1130. }
  1131. }
  1132. elseif (isset($terms->_empty_)) {
  1133. $terms = clone $terms;
  1134. unset($terms->_empty_);
  1135. }
  1136. $type = isset($index->options['fields'][$info['field']]['type']) ? search_api_extract_inner_type($index->options['fields'][$info['field']]['type']) : 'string';
  1137. foreach ($terms as $term => $count) {
  1138. if ($count >= $min_count) {
  1139. if ($term === '_empty_') {
  1140. $term = '!';
  1141. }
  1142. elseif ($type == 'boolean') {
  1143. if ($term == 'true') {
  1144. $term = '"1"';
  1145. }
  1146. elseif ($term == 'false') {
  1147. $term = '"0"';
  1148. }
  1149. }
  1150. elseif ($type == 'date') {
  1151. $term = $term ? '"' . strtotime($term) . '"' : NULL;
  1152. }
  1153. else {
  1154. $term = "\"$term\"";
  1155. }
  1156. if ($term) {
  1157. $facets[$delta][] = array(
  1158. 'filter' => $term,
  1159. 'count' => $count,
  1160. );
  1161. }
  1162. }
  1163. }
  1164. if (empty($facets[$delta])) {
  1165. unset($facets[$delta]);
  1166. }
  1167. }
  1168. }
  1169. }
  1170. if (isset($response->facet_counts->facet_queries)) {
  1171. if ($spatials = $query->getOption('search_api_location')) {
  1172. $queries = array();
  1173. foreach ($response->facet_counts->facet_queries as $key => $count) {
  1174. if (!preg_match('/^spatial-(.*)-(\d+(?:\.\d+)?)$/', $key, $m)) {
  1175. continue;
  1176. }
  1177. if (empty($extract_facets[$m[1]])) {
  1178. continue;
  1179. }
  1180. $facet = $extract_facets[$m[1]];
  1181. if ($count >= $facet['min_count']) {
  1182. $facets[$m[1]][] = array(
  1183. 'filter' => "[* {$m[2]}]",
  1184. 'count' => $count,
  1185. );
  1186. }
  1187. }
  1188. }
  1189. }
  1190. return $facets;
  1191. }
  1192. /**
  1193. * Flatten a keys array into a single search string.
  1194. *
  1195. * @param array $keys
  1196. * The keys array to flatten, formatted as specified by
  1197. * SearchApiQueryInterface::getKeys().
  1198. *
  1199. * @return string
  1200. * A Solr query string representing the same keys.
  1201. */
  1202. protected function flattenKeys(array $keys) {
  1203. $k = array();
  1204. $or = $keys['#conjunction'] == 'OR';
  1205. $neg = !empty($keys['#negation']);
  1206. foreach (element_children($keys) as $i) {
  1207. $key = $keys[$i];
  1208. if (!$key) {
  1209. continue;
  1210. }
  1211. if (is_array($key)) {
  1212. $subkeys = $this->flattenKeys($key);
  1213. if ($subkeys) {
  1214. $nested_expressions = TRUE;
  1215. // If this is a negated OR expression, we can't just use nested keys
  1216. // as-is, but have to put them into parantheses.
  1217. if ($or && $neg) {
  1218. $subkeys = "($subkeys)";
  1219. }
  1220. $k[] = $subkeys;
  1221. }
  1222. }
  1223. else {
  1224. $key = trim($key);
  1225. $key = call_user_func(array($this->connection_class, 'phrase'), $key);
  1226. $k[] = $key;
  1227. }
  1228. }
  1229. if (!$k) {
  1230. return '';
  1231. }
  1232. // Formatting the keys into a Solr query can be a bit complex. The following
  1233. // code will produce filters that look like this:
  1234. //
  1235. // #conjunction | #negation | return value
  1236. // ----------------------------------------------------------------
  1237. // AND | FALSE | A B C
  1238. // AND | TRUE | -(A AND B AND C)
  1239. // OR | FALSE | ((A) OR (B) OR (C))
  1240. // OR | TRUE | -A -B -C
  1241. // If there was just a single, unnested key, we can ignore all this.
  1242. if (count($k) == 1 && empty($nested_expressions)) {
  1243. $k = reset($k);
  1244. return $neg ? "*:* AND -$k" : $k;
  1245. }
  1246. if ($or) {
  1247. if ($neg) {
  1248. return '*:* AND -' . implode(' AND -', $k);
  1249. }
  1250. return '((' . implode(') OR (', $k) . '))';
  1251. }
  1252. $k = implode($neg ? ' AND ' : ' ', $k);
  1253. return $neg ? "*:* AND -($k)" : $k;
  1254. }
  1255. /**
  1256. * Transforms a query filter into a flat array of Solr filter queries, using
  1257. * the field names in $fields.
  1258. */
  1259. protected function createFilterQueries(SearchApiQueryFilterInterface $filter, array $solr_fields, array $fields) {
  1260. $or = $filter->getConjunction() == 'OR';
  1261. $fq = array();
  1262. foreach ($filter->getFilters() as $f) {
  1263. if (is_array($f)) {
  1264. if (!isset($fields[$f[0]])) {
  1265. throw new SearchApiException(t('Filter term on unknown or unindexed field @field.', array('@field' => $f[0])));
  1266. }
  1267. if ($f[1] !== '') {
  1268. $fq[] = $this->createFilterQuery($solr_fields[$f[0]], $f[1], $f[2], $fields[$f[0]]);
  1269. }
  1270. }
  1271. else {
  1272. $q = $this->createFilterQueries($f, $solr_fields, $fields);
  1273. if ($filter->getConjunction() != $f->getConjunction()) {
  1274. // $or == TRUE means the nested filter has conjunction AND, and vice versa
  1275. $sep = $or ? ' ' : ' OR ';
  1276. $fq[] = count($q) == 1 ? reset($q) : '((' . implode(')' . $sep . '(', $q) . '))';
  1277. }
  1278. else {
  1279. $fq = array_merge($fq, $q);
  1280. }
  1281. }
  1282. }
  1283. return ($or && count($fq) > 1) ? array('((' . implode(') OR (', $fq) . '))') : $fq;
  1284. }
  1285. /**
  1286. * Create a single search query string according to the given field, value
  1287. * and operator.
  1288. */
  1289. protected function createFilterQuery($field, $value, $operator, $field_info) {
  1290. $field = call_user_func(array($this->connection_class, 'escapeFieldName'), $field);
  1291. if ($value === NULL) {
  1292. return ($operator == '=' ? '*:* AND -' : '') . "$field:[* TO *]";
  1293. }
  1294. $value = trim($value);
  1295. $value = $this->formatFilterValue($value, search_api_extract_inner_type($field_info['type']));
  1296. switch ($operator) {
  1297. case '<>':
  1298. return "*:* AND -($field:$value)";
  1299. case '<':
  1300. return "$field:{* TO $value}";
  1301. case '<=':
  1302. return "$field:[* TO $value]";
  1303. case '>=':
  1304. return "$field:[$value TO *]";
  1305. case '>':
  1306. return "$field:{{$value} TO *}";
  1307. default:
  1308. return "$field:$value";
  1309. }
  1310. }
  1311. /**
  1312. * Format a value for filtering on a field of a specific type.
  1313. */
  1314. protected function formatFilterValue($value, $type) {
  1315. switch ($type) {
  1316. case 'boolean':
  1317. $value = $value ? 'true' : 'false';
  1318. break;
  1319. case 'date':
  1320. $value = is_numeric($value) ? (int) $value : strtotime($value);
  1321. if ($value === FALSE) {
  1322. return 0;
  1323. }
  1324. $value = format_date($value, 'custom', self::SOLR_DATE_FORMAT, 'UTC');
  1325. break;
  1326. }
  1327. return call_user_func(array($this->connection_class, 'phrase'), $value);
  1328. }
  1329. /**
  1330. * Helper method for creating the facet field parameters.
  1331. */
  1332. protected function getFacetParams(array $facets, array $fields, array &$fq = array()) {
  1333. if (!$facets) {
  1334. return array();
  1335. }
  1336. $facet_params['facet'] = 'true';
  1337. $facet_params['facet.sort'] = 'count';
  1338. $facet_params['facet.limit'] = 10;
  1339. $facet_params['facet.mincount'] = 1;
  1340. $facet_params['facet.missing'] = 'false';
  1341. $taggedFields = array();
  1342. foreach ($facets as $info) {
  1343. if (empty($fields[$info['field']])) {
  1344. continue;
  1345. }
  1346. // String fields have their own corresponding facet fields.
  1347. $field = $fields[$info['field']];
  1348. // Check for the "or" operator.
  1349. if (isset($info['operator']) && $info['operator'] === 'or') {
  1350. // Remember that filters for this field should be tagged.
  1351. $escaped = call_user_func(array($this->connection_class, 'escapeFieldName'), $fields[$info['field']]);
  1352. $taggedFields[$escaped] = "{!tag=$escaped}";
  1353. // Add the facet field.
  1354. $facet_params['facet.field'][] = "{!ex=$escaped}$field";
  1355. }
  1356. else {
  1357. // Add the facet field.
  1358. $facet_params['facet.field'][] = $field;
  1359. }
  1360. // Set limit, unless it's the default.
  1361. if ($info['limit'] != 10) {
  1362. $facet_params["f.$field.facet.limit"] = $info['limit'] ? $info['limit'] : -1;
  1363. }
  1364. // Set mincount, unless it's the default.
  1365. if ($info['min_count'] != 1) {
  1366. $facet_params["f.$field.facet.mincount"] = $info['min_count'];
  1367. }
  1368. // Set missing, if specified.
  1369. if ($info['missing']) {
  1370. $facet_params["f.$field.facet.missing"] = 'true';
  1371. }
  1372. }
  1373. // Tag filters of fields with "OR" facets.
  1374. foreach ($taggedFields as $field => $tag) {
  1375. $regex = '#(?<![^( ])' . preg_quote($field, '#') . ':#';
  1376. foreach ($fq as $i => $filter) {
  1377. // Solr can't handle two tags on the same filter, so we don't add two.
  1378. // Another option here would even be to remove the other tag, too,
  1379. // since we can be pretty sure that this filter does not originate from
  1380. // a facet – however, wrong results would still be possible, and this is
  1381. // definitely an edge case, so don't bother.
  1382. if (preg_match($regex, $filter) && substr($filter, 0, 6) != '{!tag=') {
  1383. $fq[$i] = $tag . $filter;
  1384. }
  1385. }
  1386. }
  1387. return $facet_params;
  1388. }
  1389. /**
  1390. * Helper method for creating the highlighting parameters.
  1391. *
  1392. * (The $query parameter currently isn't used and only here for the potential
  1393. * sake of subclasses.)
  1394. *
  1395. * @param SearchApiQueryInterface|SearchApiMultiQueryInterface $query
  1396. * The query object, either for a normal Search API query or a multi-index
  1397. * query.
  1398. *
  1399. * @return array
  1400. * An array of parameters to be added to the Solr search request.
  1401. */
  1402. protected function getHighlightParams($query) {
  1403. $highlight_params = array();
  1404. if (!empty($this->options['excerpt']) || !empty($this->options['highlight_data'])) {
  1405. $highlight_params['hl'] = 'true';
  1406. $highlight_params['hl.fl'] = 'spell';
  1407. $highlight_params['hl.simple.pre'] = '[HIGHLIGHT]';
  1408. $highlight_params['hl.simple.post'] = '[/HIGHLIGHT]';
  1409. $highlight_params['hl.snippets'] = 3;
  1410. $highlight_params['hl.fragsize'] = 70;
  1411. $highlight_params['hl.mergeContiguous'] = 'true';
  1412. }
  1413. if (!empty($this->options['highlight_data'])) {
  1414. $highlight_params['hl.fl'] = 'tm_*';
  1415. $highlight_params['hl.snippets'] = 1;
  1416. $highlight_params['hl.fragsize'] = 0;
  1417. if (!empty($this->options['excerpt'])) {
  1418. // If we also generate a "normal" excerpt, set the settings for the
  1419. // "spell" field (which we use to generate the excerpt) back to the
  1420. // above values.
  1421. $highlight_params['f.spell.hl.snippets'] = 3;
  1422. $highlight_params['f.spell.hl.fragsize'] = 70;
  1423. // It regrettably doesn't seem to be possible to set hl.fl to several
  1424. // values, if one contains wild cards (i.e., "t_*,spell" wouldn't work).
  1425. $highlight_params['hl.fl'] = '*';
  1426. }
  1427. }
  1428. return $highlight_params;
  1429. }
  1430. /**
  1431. * Sets the request handler.
  1432. *
  1433. * This should also make the needed adjustments to the request parameters.
  1434. *
  1435. * @param $handler
  1436. * Name of the handler to set.
  1437. * @param array $call_args
  1438. * An associative array containing all three arguments to the
  1439. * SearchApiSolrConnectionInterface::search() call ("query", "params" and
  1440. * "method") as references.
  1441. *
  1442. * @return bool
  1443. * TRUE iff this method invocation handled the given handler. This allows
  1444. * subclasses to recognize whether the request handler was already set by
  1445. * this method.
  1446. */
  1447. protected function setRequestHandler($handler, array &$call_args) {
  1448. if ($handler == 'pinkPony') {
  1449. $call_args['params']['qt'] = $handler;
  1450. return TRUE;
  1451. }
  1452. return FALSE;
  1453. }
  1454. /**
  1455. * Empty method called before sending a search query to Solr.
  1456. *
  1457. * This allows subclasses to apply custom changes before the query is sent to
  1458. * Solr. Works exactly like hook_search_api_solr_query_alter().
  1459. *
  1460. * @param array $call_args
  1461. * An associative array containing all three arguments to the
  1462. * SearchApiSolrConnectionInterface::search() call ("query", "params" and
  1463. * "method") as references.
  1464. * @param SearchApiQueryInterface $query
  1465. * The SearchApiQueryInterface object representing the executed search query.
  1466. */
  1467. protected function preQuery(array &$call_args, SearchApiQueryInterface $query) {
  1468. }
  1469. /**
  1470. * Empty method to allow subclasses to apply custom changes before search results are returned.
  1471. *
  1472. * Works exactly like hook_search_api_solr_search_results_alter().
  1473. *
  1474. * @param array $results
  1475. * The results array that will be returned for the search.
  1476. * @param SearchApiQueryInterface $query
  1477. * The SearchApiQueryInterface object representing the executed search query.
  1478. * @param object $response
  1479. * The response object returned by Solr.
  1480. */
  1481. protected function postQuery(array &$results, SearchApiQueryInterface $query, $response) {
  1482. }
  1483. //
  1484. // Autocompletion feature
  1485. //
  1486. /**
  1487. * Implements SearchApiAutocompleteInterface::getAutocompleteSuggestions().
  1488. */
  1489. // Largely copied from the apachesolr_autocomplete module.
  1490. public function getAutocompleteSuggestions(SearchApiQueryInterface $query, SearchApiAutocompleteSearch $search, $incomplete_key, $user_input) {
  1491. $suggestions = array();
  1492. // Reset request handler
  1493. $this->request_handler = NULL;
  1494. // Turn inputs to lower case, otherwise we get case sensivity problems.
  1495. $incomp = drupal_strtolower($incomplete_key);
  1496. $index = $query->getIndex();
  1497. $fields = $this->getFieldNames($index);
  1498. $complete = $query->getOriginalKeys();
  1499. // Extract keys
  1500. $keys = $query->getKeys();
  1501. if (is_array($keys)) {
  1502. $keys_array = array();
  1503. while ($keys) {
  1504. reset($keys);
  1505. if (!element_child(key($keys))) {
  1506. array_shift($keys);
  1507. continue;
  1508. }
  1509. $key = array_shift($keys);
  1510. if (is_array($key)) {
  1511. $keys = array_merge($keys, $key);
  1512. }
  1513. else {
  1514. $keys_array[$key] = $key;
  1515. }
  1516. }
  1517. $keys = $this->flattenKeys($query->getKeys());
  1518. }
  1519. else {
  1520. $keys_array = drupal_map_assoc(preg_split('/[-\s():{}\[\]\\\\"]+/', $keys, -1, PREG_SPLIT_NO_EMPTY));
  1521. }
  1522. if (!$keys) {
  1523. $keys = NULL;
  1524. }
  1525. // Set searched fields
  1526. $options = $query->getOptions();
  1527. $search_fields = $query->getFields();
  1528. $qf = array();
  1529. foreach ($search_fields as $f) {
  1530. $qf[] = $fields[$f];
  1531. }
  1532. // Extract filters
  1533. $fq = $this->createFilterQueries($query->getFilter(), $fields, $index->options['fields']);
  1534. $fq[] = 'index_id:' . $this->getIndexId($index->machine_name);
  1535. // Autocomplete magic
  1536. $facet_fields = array();
  1537. foreach ($search_fields as $f) {
  1538. $facet_fields[] = $fields[$f];
  1539. }
  1540. $limit = $query->getOption('limit', 10);
  1541. $params = array(
  1542. 'qf' => $qf,
  1543. 'fq' => $fq,
  1544. 'rows' => 0,
  1545. 'facet' => 'true',
  1546. 'facet.field' => $facet_fields,
  1547. 'facet.prefix' => $incomp,
  1548. 'facet.limit' => $limit * 5,
  1549. 'facet.mincount' => 1,
  1550. 'spellcheck' => (!isset($this->options['autocorrect_spell']) || $this->options['autocorrect_spell']) ? 'true' : 'false',
  1551. 'spellcheck.count' => 1,
  1552. );
  1553. // Retrieve http method from server options.
  1554. $http_method = !empty($this->options['http_method']) ? $this->options['http_method'] : 'AUTO';
  1555. $call_args = array(
  1556. 'query' => &$keys,
  1557. 'params' => &$params,
  1558. 'http_method' => &$http_method,
  1559. );
  1560. if ($this->request_handler) {
  1561. $this->setRequestHandler($this->request_handler, $call_args);
  1562. }
  1563. $second_pass = !isset($this->options['autocorrect_suggest_words']) || $this->options['autocorrect_suggest_words'];
  1564. for ($i = 0; $i < ($second_pass ? 2 : 1); ++$i) {
  1565. try {
  1566. // Send search request
  1567. $this->connect();
  1568. drupal_alter('search_api_solr_query', $call_args, $query);
  1569. $this->preQuery($call_args, $query);
  1570. $response = $this->solr->search($keys, $params, $http_method);
  1571. if (!empty($response->spellcheck->suggestions)) {
  1572. $replace = array();
  1573. foreach ($response->spellcheck->suggestions as $word => $data) {
  1574. $replace[$word] = $data->suggestion[0];
  1575. }
  1576. $corrected = str_ireplace(array_keys($replace), array_values($replace), $user_input);
  1577. if ($corrected != $user_input) {
  1578. array_unshift($suggestions, array(
  1579. 'prefix' => t('Did you mean') . ':',
  1580. 'user_input' => $corrected,
  1581. ));
  1582. }
  1583. }
  1584. $matches = array();
  1585. if (isset($response->facet_counts->facet_fields)) {
  1586. foreach ($response->facet_counts->facet_fields as $terms) {
  1587. foreach ($terms as $term => $count) {
  1588. if (isset($matches[$term])) {
  1589. // If we just add the result counts, we can easily get over the
  1590. // total number of results if terms appear in multiple fields.
  1591. // Therefore, we just take the highest value from any field.
  1592. $matches[$term] = max($matches[$term], $count);
  1593. }
  1594. else {
  1595. $matches[$term] = $count;
  1596. }
  1597. }
  1598. }
  1599. if ($matches) {
  1600. // Eliminate suggestions that are too short or already in the query.
  1601. foreach ($matches as $term => $count) {
  1602. if (strlen($term) < 3 || isset($keys_array[$term])) {
  1603. unset($matches[$term]);
  1604. }
  1605. }
  1606. // Don't suggest terms that are too frequent (by default in more
  1607. // than 90% of results).
  1608. $result_count = $response->response->numFound;
  1609. $max_occurrences = $result_count * variable_get('search_api_solr_autocomplete_max_occurrences', 0.9);
  1610. if (($max_occurrences >= 1 || $i > 0) && $max_occurrences < $result_count) {
  1611. foreach ($matches as $match => $count) {
  1612. if ($count > $max_occurrences) {
  1613. unset($matches[$match]);
  1614. }
  1615. }
  1616. }
  1617. // The $count in this array is actually a score. We want the
  1618. // highest ones first.
  1619. arsort($matches);
  1620. // Shorten the array to the right ones.
  1621. $additional_matches = array_slice($matches, $limit - count($suggestions), NULL, TRUE);
  1622. $matches = array_slice($matches, 0, $limit, TRUE);
  1623. // Build suggestions using returned facets
  1624. $incomp_length = strlen($incomp);
  1625. foreach ($matches as $term => $count) {
  1626. if (drupal_strtolower(substr($term, 0, $incomp_length)) == $incomp) {
  1627. $suggestions[] = array(
  1628. 'suggestion_suffix' => substr($term, $incomp_length),
  1629. 'term' => $term,
  1630. 'results' => $count,
  1631. );
  1632. }
  1633. else {
  1634. $suggestions[] = array(
  1635. 'suggestion_suffix' => ' ' . $term,
  1636. 'term' => $term,
  1637. 'results' => $count,
  1638. );
  1639. }
  1640. }
  1641. }
  1642. }
  1643. }
  1644. catch (SearchApiException $e) {
  1645. watchdog_exception('search_api_solr', $e, "%type during autocomplete Solr query: !message in %function (line %line of %file).", array(), WATCHDOG_WARNING);
  1646. }
  1647. if (count($suggestions) >= $limit) {
  1648. break;
  1649. }
  1650. // Change parameters for second query.
  1651. unset($params['facet.prefix']);
  1652. $keys = trim ($keys . ' ' . $incomplete_key);
  1653. }
  1654. return $suggestions;
  1655. }
  1656. //
  1657. // SearchApiMultiServiceInterface methods
  1658. //
  1659. /**
  1660. * Implements SearchApiMultiServiceInterface::queryMultiple().
  1661. */
  1662. public function queryMultiple(array $options = array()) {
  1663. return new SearchApiMultiQuery($this->server, $options);
  1664. }
  1665. /**
  1666. * Implements SearchApiMultiServiceInterface::searchMultiple().
  1667. */
  1668. public function searchMultiple(SearchApiMultiQueryInterface $query) {
  1669. $time_method_called = microtime(TRUE);
  1670. // Get field information
  1671. $solr_fields = array(
  1672. 'search_api_id' => 'item_id',
  1673. 'search_api_relevance' => 'score',
  1674. 'search_api_multi_index' => 'index_id',
  1675. );
  1676. $fields = array(
  1677. 'search_api_multi_index' => array(
  1678. 'type' => 'string',
  1679. ),
  1680. );
  1681. foreach ($query->getIndexes() as $index) {
  1682. if (empty($index->options['fields'])) {
  1683. continue;
  1684. }
  1685. $prefix = $this->getIndexId($index->machine_name) . ':';
  1686. foreach ($this->getFieldNames($index) as $field => $key) {
  1687. if (!isset($solr_fields[$field])) {
  1688. $solr_fields[$prefix . $field] = $key;
  1689. }
  1690. }
  1691. foreach ($index->options['fields'] as $field => $info) {
  1692. $fields[$prefix . $field] = $info;
  1693. }
  1694. }
  1695. // Extract keys
  1696. $keys = $query->getKeys();
  1697. if (is_array($keys)) {
  1698. $keys = $this->flattenKeys($keys);
  1699. }
  1700. // Set searched fields
  1701. $search_fields = $query->getFields();
  1702. $qf = array();
  1703. foreach ($search_fields as $f) {
  1704. $qf[] = $solr_fields[$f];
  1705. }
  1706. // Extract filters
  1707. $filter = $query->getFilter();
  1708. $fq = $this->createFilterQueries($filter, $solr_fields, $fields);
  1709. // Restrict search to searched indexes.
  1710. $index_filter = array();
  1711. foreach ($query->getIndexes() as $index) {
  1712. $index_id = $this->getIndexId($index->machine_name);
  1713. $index_filter[] = 'index_id:' . call_user_func(array($this->connection_class, 'phrase'), $index_id);
  1714. }
  1715. $fq[] = implode(' OR ', $index_filter);
  1716. // Extract sort
  1717. $sort = array();
  1718. foreach ($query->getSort() as $f => $order) {
  1719. $f = $solr_fields[$f];
  1720. if (substr($f, 0, 3) == 'ss_') {
  1721. $f = 'sort_' . substr($f, 3);
  1722. }
  1723. $order = strtolower($order);
  1724. $sort[] = "$f $order";
  1725. }
  1726. // Get facet fields
  1727. $facets = $query->getOption('search_api_facets') ? $query->getOption('search_api_facets') : array();
  1728. $facet_params = $this->getFacetParams($facets, $solr_fields, $fq);
  1729. // Handle highlighting.
  1730. $highlight_params = $this->getHighlightParams($query);
  1731. // Set defaults
  1732. if (!$keys) {
  1733. $keys = NULL;
  1734. }
  1735. $options = $query->getOptions();
  1736. // Collect parameters
  1737. $params = array(
  1738. 'fl' => 'item_id,index_id,score',
  1739. 'qf' => $qf,
  1740. 'fq' => $fq,
  1741. );
  1742. if (isset($options['offset'])) {
  1743. $params['start'] = $options['offset'];
  1744. }
  1745. if (isset($options['limit'])) {
  1746. $params['rows'] = $options['limit'];
  1747. }
  1748. if ($sort) {
  1749. $params['sort'] = implode(', ', $sort);
  1750. }
  1751. if (!empty($facet_params['facet.field'])) {
  1752. $params += $facet_params;
  1753. }
  1754. if (!empty($highlight_params)) {
  1755. $params += $highlight_params;
  1756. }
  1757. // Retrieve http method from server options.
  1758. $http_method = !empty($this->options['http_method']) ? $this->options['http_method'] : 'AUTO';
  1759. // Send search request
  1760. $time_processing_done = microtime(TRUE);
  1761. $this->connect();
  1762. $call_args = array(
  1763. 'query' => &$keys,
  1764. 'params' => &$params,
  1765. 'http_method' => &$http_method,
  1766. );
  1767. drupal_alter('search_api_solr_multi_query', $call_args, $query);
  1768. $response = $this->solr->search($keys, $params, $http_method);
  1769. $time_query_done = microtime(TRUE);
  1770. // Extract results
  1771. $results = array();
  1772. $results['result count'] = $response->response->numFound;
  1773. $results['results'] = array();
  1774. $tmp = array();
  1775. foreach ($response->response->docs as $id => $doc) {
  1776. $result = array(
  1777. 'id' => $doc->item_id,
  1778. 'index_id' => $doc->index_id,
  1779. 'score' => $doc->score,
  1780. );
  1781. $solr_id = $this->createId($doc->index_id, $result['id']);
  1782. $excerpt = $this->getExcerpt($response, $solr_id, $tmp, array());
  1783. if ($excerpt) {
  1784. $result['excerpt'] = $excerpt;
  1785. }
  1786. $results['results'][$id] = $result;
  1787. }
  1788. // Extract facets
  1789. if (isset($response->facet_counts->facet_fields)) {
  1790. $results['search_api_facets'] = array();
  1791. $facet_fields = $response->facet_counts->facet_fields;
  1792. foreach ($facets as $delta => $info) {
  1793. $field = $solr_fields[$info['field']];
  1794. if (!empty($facet_fields->$field)) {
  1795. $min_count = $info['min_count'];
  1796. $terms = $facet_fields->$field;
  1797. if ($info['missing']) {
  1798. // We have to correctly incorporate the "_empty_" term.
  1799. // This will ensure that the term with the least results is dropped,
  1800. // if the limit would be exceeded.
  1801. if (isset($terms->_empty_)) {
  1802. if ($terms->_empty_ < $min_count) {
  1803. unset($terms->_empty_);
  1804. }
  1805. else {
  1806. $terms = (array) $terms;
  1807. arsort($terms);
  1808. if ($info['limit'] > 0 && count($terms) > $info['limit']) {
  1809. array_pop($terms);
  1810. }
  1811. }
  1812. }
  1813. }
  1814. elseif (isset($terms->_empty_)) {
  1815. $terms = clone $terms;
  1816. unset($terms->_empty_);
  1817. }
  1818. $type = isset($fields[$info['field']]['type']) ? search_api_extract_inner_type($fields[$info['field']]['type']) : 'string';
  1819. foreach ($terms as $term => $count) {
  1820. if ($count >= $min_count) {
  1821. if ($term === '_empty_') {
  1822. $term = '!';
  1823. }
  1824. elseif ($type == 'boolean') {
  1825. if ($term == 'true') {
  1826. $term = '"1"';
  1827. }
  1828. elseif ($term == 'false') {
  1829. $term = '"0"';
  1830. }
  1831. }
  1832. elseif ($type == 'date') {
  1833. $term = $term ? '"' . strtotime($term) . '"' : NULL;
  1834. }
  1835. else {
  1836. $term = "\"$term\"";
  1837. }
  1838. if ($term) {
  1839. $results['search_api_facets'][$delta][] = array(
  1840. 'filter' => $term,
  1841. 'count' => $count,
  1842. );
  1843. }
  1844. }
  1845. }
  1846. if (empty($results['search_api_facets'][$delta])) {
  1847. unset($results['search_api_facets'][$delta]);
  1848. }
  1849. }
  1850. }
  1851. }
  1852. // Compute performance
  1853. $time_end = microtime(TRUE);
  1854. $results['performance'] = array(
  1855. 'complete' => $time_end - $time_method_called,
  1856. 'preprocessing' => $time_processing_done - $time_method_called,
  1857. 'execution' => $time_query_done - $time_processing_done,
  1858. 'postprocessing' => $time_end - $time_query_done,
  1859. );
  1860. return $results;
  1861. }
  1862. //
  1863. // Additional methods that might be used when knowing the service class.
  1864. //
  1865. /**
  1866. * Ping the Solr server to tell whether it can be accessed.
  1867. *
  1868. * Uses the admin/ping request handler.
  1869. */
  1870. public function ping() {
  1871. $this->connect();
  1872. return $this->solr->ping();
  1873. }
  1874. /**
  1875. * Sends a commit command to the Solr server.
  1876. */
  1877. public function commit() {
  1878. try {
  1879. $this->connect();
  1880. return $this->solr->commit(FALSE);
  1881. }
  1882. catch (SearchApiException $e) {
  1883. watchdog_exception('search_api_solr', $e,
  1884. '%type while trying to commit on server @server: !message in %function (line %line of %file).',
  1885. array('@server' => $this->server->machine_name), WATCHDOG_WARNING);
  1886. }
  1887. }
  1888. /**
  1889. * Schedules a commit operation for this server.
  1890. *
  1891. * The commit will be sent at the end of the current page request. Multiple
  1892. * calls to this method will still only result in one commit operation.
  1893. */
  1894. public function scheduleCommit() {
  1895. if (!$this->commitScheduled) {
  1896. $this->commitScheduled = TRUE;
  1897. drupal_register_shutdown_function(array($this, 'commit'));
  1898. }
  1899. }
  1900. /**
  1901. * Gets the Solr connection class used by this service.
  1902. *
  1903. * @return string
  1904. * The name of a class which implements SearchApiSolrConnectionInterface.
  1905. */
  1906. public function getConnectionClass() {
  1907. return $this->connection_class;
  1908. }
  1909. /**
  1910. * Sets the Solr connection class used by this service.
  1911. *
  1912. * @param string $class
  1913. * The name of a class which implements SearchApiSolrConnectionInterface.
  1914. */
  1915. public function setConnectionClass($class) {
  1916. $this->connection_class = $class;
  1917. $this->solr = NULL;
  1918. }
  1919. /**
  1920. * Gets the currently used Solr connection object.
  1921. *
  1922. * @return SearchApiSolrConnectionInterface
  1923. * The solr connection object used by this server.
  1924. */
  1925. public function getSolrConnection() {
  1926. $this->connect();
  1927. return $this->solr;
  1928. }
  1929. /**
  1930. * Get metadata about fields in the Solr/Lucene index.
  1931. *
  1932. * @param int $num_terms
  1933. * Number of 'top terms' to return.
  1934. *
  1935. * @return array
  1936. * An array of SearchApiSolrField objects.
  1937. *
  1938. * @see SearchApiSolrConnectionInterface::getFields()
  1939. */
  1940. public function getFields($num_terms = 0) {
  1941. $this->connect();
  1942. return $this->solr->getFields($num_terms);
  1943. }
  1944. /**
  1945. * Retrieves a config file or file list from the Solr server.
  1946. *
  1947. * Uses the admin/file request handler.
  1948. *
  1949. * @param string|null $file
  1950. * (optional) The name of the file to retrieve. If the file is a directory,
  1951. * the directory contents are instead listed and returned. NULL represents
  1952. * the root config directory.
  1953. *
  1954. * @return object
  1955. * A HTTP response object containing either the file contents or a file list.
  1956. */
  1957. public function getFile($file = NULL) {
  1958. $this->connect();
  1959. $file_servlet_name = constant($this->connection_class . '::FILE_SERVLET');
  1960. $params['contentType'] = 'text/xml;charset=utf-8';
  1961. if ($file) {
  1962. $params['file'] = $file;
  1963. }
  1964. return $this->solr->makeServletRequest($file_servlet_name, $params);
  1965. }
  1966. /**
  1967. * Prefixes an index ID as configured.
  1968. *
  1969. * The resulting ID will be a concatenation of the following strings:
  1970. * - If set, the "search_api_solr_index_prefix" variable.
  1971. * - If set, the index-specific "search_api_solr_index_prefix_INDEX" variable.
  1972. * - The index's machine name.
  1973. *
  1974. * @param string $machine_name
  1975. * The index's machine name.
  1976. *
  1977. * @return string
  1978. * The prefixed machine name.
  1979. */
  1980. protected function getIndexId($machine_name) {
  1981. // Prepend per-index prefix.
  1982. $id = variable_get('search_api_solr_index_prefix_' . $machine_name, '') . $machine_name;
  1983. // Prepend environment prefix.
  1984. $id = variable_get('search_api_solr_index_prefix', '') . $id;
  1985. return $id;
  1986. }
  1987. }