search_api_solr.api.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. * @} End of "addtogroup hooks".
  97. */