search_api_solr.api.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. /**
  3. * @file
  4. * Hooks provided by the Search API Solr search module.
  5. */
  6. /**
  7. * @addtogroup hooks
  8. * @{
  9. */
  10. /**
  11. * Lets modules alter a Solr search request before sending it.
  12. *
  13. * Apache_Solr_Service::search() is called afterwards with these parameters.
  14. * Please see this method for details on what should be altered where and what
  15. * is set afterwards.
  16. *
  17. * @param array $call_args
  18. * An associative array containing all three arguments to the
  19. * SearchApiSolrConnectionInterface::search() call ("query", "params" and
  20. * "method") as references.
  21. * @param SearchApiQueryInterface $query
  22. * The SearchApiQueryInterface object representing the executed search query.
  23. */
  24. function hook_search_api_solr_query_alter(array &$call_args, SearchApiQueryInterface $query) {
  25. if ($query->getOption('foobar')) {
  26. $call_args['params']['foo'] = 'bar';
  27. }
  28. }
  29. /**
  30. * Change the way the index's field names are mapped to Solr field names.
  31. *
  32. * @param SearchApiIndex $index
  33. * The index whose field mappings are altered.
  34. * @param array $fields
  35. * An associative array containing the index field names mapped to their Solr
  36. * counterparts. The special fields 'search_api_id' and 'search_api_relevance'
  37. * are also included.
  38. */
  39. function hook_search_api_solr_field_mapping_alter(SearchApiIndex $index, array &$fields) {
  40. if ($index->entity_type == 'node' && isset($fields['body:value'])) {
  41. $fields['body:value'] = 'text';
  42. }
  43. }
  44. /**
  45. * Alter Solr documents before they are sent to Solr for indexing.
  46. *
  47. * @param array $documents
  48. * An array of SearchApiSolrDocument objects ready to be indexed, generated
  49. * from $items array.
  50. * @param SearchApiIndex $index
  51. * The search index for which items are being indexed.
  52. * @param array $items
  53. * An array of items being indexed.
  54. */
  55. function hook_search_api_solr_documents_alter(array &$documents, SearchApiIndex $index, array $items) {
  56. // Adds a "foo" field with value "bar" to all documents.
  57. foreach ($documents as $document) {
  58. $document->setField('foo', 'bar');
  59. }
  60. }
  61. /**
  62. * Lets modules alter the search results returned from a Solr search.
  63. *
  64. * @param array $results
  65. * The results array that will be returned for the search.
  66. * @param SearchApiQueryInterface $query
  67. * The SearchApiQueryInterface object representing the executed search query.
  68. * @param object $response
  69. * The Solr response object.
  70. */
  71. function hook_search_api_solr_search_results_alter(array &$results, SearchApiQueryInterface $query, $response) {
  72. if (isset($response->facet_counts->facet_fields->custom_field)) {
  73. // Do something with $results.
  74. }
  75. }
  76. /**
  77. * Lets modules alter a Solr search request for a multi-index search.
  78. *
  79. * SearchApiSolrConnectionInterface::search() is called afterwards with these
  80. * parameters. Please see this method for details on what should be altered
  81. * where and what is set afterwards.
  82. *
  83. * @param array $call_args
  84. * An associative array containing all three arguments to the
  85. * SearchApiSolrConnectionInterface::search() call ("query", "params" and
  86. * "method") as references.
  87. * @param SearchApiMultiQueryInterface $query
  88. * The object representing the executed search query.
  89. */
  90. function hook_search_api_solr_multi_query_alter(array &$call_args, SearchApiMultiQueryInterface $query) {
  91. if ($query->getOption('foobar')) {
  92. $call_args['params']['foo'] = 'bar';
  93. }
  94. }
  95. /**
  96. * Lets modules alter the search results returned from a multi-index search.
  97. *
  98. * @param array $results
  99. * The results array that will be returned for the search.
  100. * @param SearchApiMultiQueryInterface $query
  101. * The executed multi-index search query.
  102. * @param object $response
  103. * The Solr response object.
  104. */
  105. function hook_search_api_solr_multi_search_results_alter(array &$results, SearchApiMultiQueryInterface $query, $response) {
  106. if (isset($response->facet_counts->facet_fields->custom_field)) {
  107. // Do something with $results.
  108. }
  109. }
  110. /**
  111. * Provide Solr dynamic fields as Search API data types.
  112. *
  113. * This serves as a placeholder for documenting additional keys for
  114. * hook_search_api_data_type_info() which are recognized by this module to
  115. * automatically support dynamic field types from the schema.
  116. *
  117. * @return array
  118. * In addition to the keys for the individual types that are defined by
  119. * hook_search_api_data_type_info(), the following keys are regonized:
  120. * - prefix: The Solr field name prefix to use for this type. Should match
  121. * two existing dynamic fields definitions with names "{PREFIX}s_*" and
  122. * "{PREFIX}m_*".
  123. * - always multiValued: (optional) If TRUE, only the dynamic field name
  124. * prefix (without the "_*" portion) with multiValued="true" should be given
  125. * by "prefix", instead of the common prefix part for both the single-valued
  126. * and the multi-valued field. This should be the case for all fulltext
  127. * fields, since they might already be tokenized by the Search API. Defaults
  128. * to FALSE.
  129. *
  130. *@see hook_search_api_data_type_info()
  131. */
  132. function search_api_solr_hook_search_api_data_type_info() {
  133. return array(
  134. // You can use any identifier you want here, but it makes sense to use the
  135. // field type name from schema.xml.
  136. 'edge_n2_kw_text' => array(
  137. // Stock hook_search_api_data_type_info() info:
  138. 'name' => t('Fulltext (w/ partial matching)'),
  139. 'fallback' => 'text',
  140. // Dynamic field with name="te_*".
  141. 'prefix' => 'te',
  142. // Fulltext types should always be multi-valued.
  143. 'always multiValued' => TRUE,
  144. ),
  145. 'tlong' => array(
  146. // Stock hook_search_api_data_type_info() info:
  147. 'name' => t('TrieLong'),
  148. 'fallback' => 'integer',
  149. // Dynamic fields with name="its_*" and name="itm_*".
  150. 'prefix' => 'it',
  151. ),
  152. );
  153. }
  154. /**
  155. * Alter autocomplete suggestions returned from Solr servers.
  156. *
  157. * @param array $suggestions
  158. * An array of suggestions to be altered, in the structure documented in
  159. * SearchApiAutocompleteSuggesterInterface::getAutocompleteSuggestions().
  160. * @param array $alter_data
  161. * An associative array of data about the search, with the following keys:
  162. * "search", "query", "incomplete_key", "user_input", which correspond to the
  163. * arguments to SearchApiAutocompleteInterface::getAutocompleteSuggestions();
  164. * and "responses", an array containing the Solr response objects used for
  165. * constructing the suggestions.
  166. */
  167. function hook_search_api_solr_autocomplete_suggestions_alter(array &$suggestions, array &$alter_data) {
  168. // Always also suggest the original user input.
  169. array_unshift($suggestions, trim($alter_data['user_input']));
  170. }
  171. /**
  172. * @} End of "addtogroup hooks".
  173. */