search_api_solr.api.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. * Provide Solr dynamic fields as Search API data types.
  97. *
  98. * This serves as a placeholder for documenting additional keys for
  99. * hook_search_api_data_type_info() which are recognized by this module to
  100. * automatically support dynamic field types from the schema.
  101. *
  102. * @return array
  103. * In addition to the keys for the individual types that are defined by
  104. * hook_search_api_data_type_info(), the following keys are regonized:
  105. * - prefix: The Solr field name prefix to use for this type. Should match
  106. * two existing dynamic fields definitions with names "{PREFIX}s_*" and
  107. * "{PREFIX}m_*".
  108. * - always multiValued: (optional) If TRUE, only the dynamic field name
  109. * prefix (without the "_*" portion) with multiValued="true" should be given
  110. * by "prefix", instead of the common prefix part for both the single-valued
  111. * and the multi-valued field. This should be the case for all fulltext
  112. * fields, since they might already be tokenized by the Search API. Defaults
  113. * to FALSE.
  114. *
  115. *@see hook_search_api_data_type_info()
  116. */
  117. function search_api_solr_hook_search_api_data_type_info() {
  118. return array(
  119. // You can use any identifier you want here, but it makes sense to use the
  120. // field type name from schema.xml.
  121. 'edge_n2_kw_text' => array(
  122. // Stock hook_search_api_data_type_info() info:
  123. 'name' => t('Fulltext (w/ partial matching)'),
  124. 'fallback' => 'text',
  125. // Dynamic field with name="te_*".
  126. 'prefix' => 'te',
  127. // Fulltext types should always be multi-valued.
  128. 'always multiValued' => TRUE,
  129. ),
  130. 'tlong' => array(
  131. // Stock hook_search_api_data_type_info() info:
  132. 'name' => t('TrieLong'),
  133. 'fallback' => 'integer',
  134. // Dynamic fields with name="its_*" and name="itm_*".
  135. 'prefix' => 'it',
  136. ),
  137. );
  138. }
  139. /**
  140. * @} End of "addtogroup hooks".
  141. */