service.inc 69 KB

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