1846860-1-search_api_solr-custom_dynamic_fields_0.patch 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. diff --git a/search_api_solr.api.php b/search_api_solr.api.php
  2. index f407407..ec4b190 100644
  3. --- a/search_api_solr.api.php
  4. +++ b/search_api_solr.api.php
  5. @@ -85,5 +85,45 @@ function hook_search_api_solr_multi_query_alter(array &$call_args, SearchApiMult
  6. }
  7. /**
  8. + * Define how Search API Solr should index different data types.
  9. + *
  10. + * It is important to make sure that any types you define are also declared to
  11. + * Search API using hook_search_api_data_type_info().
  12. + *
  13. + * @return array
  14. + * An array containing data type definitions, keyed by their type identifier
  15. + * and containing the following keys:
  16. + * - prefix: The prefix used by the dynamic field type.
  17. + * - always multiValued: (optional) Whether the single/multiple prefix should
  18. + * be skipped for this data type. Defaults to FALSE.
  19. + *
  20. + * @see hook_search_api_solr_dynamic_field_info_alter()
  21. + * @see search_api_solr_get_dynamic_field_info()
  22. + * @see hook_search_api_data_type_info().
  23. + */
  24. +function hook_search_api_solr_dynamic_field_info() {
  25. + return array(
  26. + 'example_type' => array(
  27. + 'prefix' => 'ex',
  28. + // Could be omitted, as FALSE is the default.
  29. + 'always multiValued' => FALSE,
  30. + ),
  31. + );
  32. +}
  33. +
  34. +/**
  35. + * Alter the data type indexing info.
  36. + *
  37. + * @param array $infos
  38. + * The item type info array, keyed by type identifier.
  39. + *
  40. + * @see hook_search_api_solr_dynamic_field_info()
  41. + */
  42. +function hook_search_api_solr_dynamic_field_info_alter(array &$infos) {
  43. + // Change the prefix used for example_type.
  44. + $info['example_type']['prefix'] = 'ex2';
  45. +}
  46. +
  47. +/**
  48. * @} End of "addtogroup hooks".
  49. */
  50. diff --git a/search_api_solr.module b/search_api_solr.module
  51. index 9e30397..b75319b 100644
  52. --- a/search_api_solr.module
  53. +++ b/search_api_solr.module
  54. @@ -143,3 +143,81 @@ function search_api_solr_cron() {
  55. }
  56. }
  57. }
  58. +
  59. +/**
  60. + * Returns either all dynamic field types, or a specific one.
  61. + *
  62. + * @param $type
  63. + * If specified, the type whose definition should be returned.
  64. + *
  65. + * @return array
  66. + * If $type was not given, an array containing all custom dynamic fields, in
  67. + * the format specified by hook_search_api_solr_dynamic_field_info().
  68. + * Otherwise, the definition for the given type, or NULL if it is unknown.
  69. + *
  70. + * @see hook_search_api_solr_dynamic_field_info().
  71. + */
  72. +function search_api_solr_get_dynamic_field_info($type = NULL) {
  73. + $types = &drupal_static(__FUNCTION__);
  74. + if (!isset($types)) {
  75. + $types = module_invoke_all('search_api_solr_dynamic_field_info');
  76. + $types = $types ? $types : array();
  77. + drupal_alter('search_api_solr_dynamic_field_info', $types);
  78. + }
  79. + if (isset($type)) {
  80. + return isset($types[$type]) ? $types[$type] : NULL;
  81. + }
  82. + return $types;
  83. +}
  84. +
  85. +/**
  86. + * Implements hook_search_api_solr_dynamic_field_info().
  87. + */
  88. +function search_api_solr_search_api_solr_dynamic_field_info() {
  89. + return array(
  90. + 'text' => array(
  91. + 'prefix' => 'tm',
  92. + 'always multiValued' => TRUE,
  93. + ),
  94. + 'tokens' => array(
  95. + 'prefix' => 'tm',
  96. + 'always multiValued' => TRUE,
  97. + ),
  98. + 'string' => array(
  99. + 'prefix' => 's',
  100. + 'always multiValued' => FALSE,
  101. + ),
  102. + 'integer' => array(
  103. + 'prefix' => 'i',
  104. + 'always multiValued' => FALSE,
  105. + ),
  106. + 'decimal' => array(
  107. + 'prefix' => 'f',
  108. + 'always multiValued' => FALSE,
  109. + ),
  110. + 'date' => array(
  111. + 'prefix' => 'd',
  112. + 'always multiValued' => FALSE,
  113. + ),
  114. + 'duration' => array(
  115. + 'i',
  116. + 'always multiValued' => FALSE,
  117. + ),
  118. + 'boolean' => array(
  119. + 'b',
  120. + 'always multiValued' => FALSE,
  121. + ),
  122. + 'uri' => array(
  123. + 's',
  124. + 'always multiValued' => FALSE,
  125. + ),
  126. + 'location' => array(
  127. + 'loc',
  128. + 'always multiValued' => FALSE,
  129. + ),
  130. + 'geohash' => array(
  131. + 'geohash',
  132. + 'always multiValued' => FALSE,
  133. + ),
  134. + );
  135. +}
  136. diff --git a/service.inc b/service.inc
  137. index 0e8dbee..5054c51 100644
  138. --- a/service.inc
  139. +++ b/service.inc
  140. @@ -18,26 +18,6 @@ class SearchApiSolrService extends SearchApiAbstractService {
  141. protected $solr;
  142. /**
  143. - * An array of all recognized types mapped to a prefix used for identifying
  144. - * them in the Solr schema.
  145. - *
  146. - * @var array
  147. - */
  148. - protected static $type_prefixes = array(
  149. - 'text' => 'tm',
  150. - 'tokens' => 'tm',
  151. - 'string' => 's',
  152. - 'integer' => 'i',
  153. - 'decimal' => 'f',
  154. - 'date' => 'd',
  155. - 'duration' => 'i',
  156. - 'boolean' => 'b',
  157. - 'uri' => 's',
  158. - 'location' => 'loc',
  159. - 'geohash' => 'geohash',
  160. - );
  161. -
  162. - /**
  163. * @var array
  164. */
  165. protected $fieldNames = array();
  166. @@ -222,7 +202,8 @@ class SearchApiSolrService extends SearchApiAbstractService {
  167. }
  168. public function supportsFeature($feature) {
  169. - $supported = drupal_map_assoc(array(
  170. + // Search API features.
  171. + $supported = array(
  172. 'search_api_autocomplete',
  173. 'search_api_facets',
  174. 'search_api_facets_operator_or',
  175. @@ -231,7 +212,14 @@ class SearchApiSolrService extends SearchApiAbstractService {
  176. 'search_api_spellcheck',
  177. 'search_api_data_type_location',
  178. 'search_api_data_type_geohash',
  179. - ));
  180. + );
  181. +
  182. + // Custom data types.
  183. + foreach (search_api_solr_get_dynamic_field_info() as $type => $info) {
  184. + $supported[] = 'search_api_data_type_' . $type;
  185. + }
  186. +
  187. + $supported = drupal_map_assoc($supported);
  188. return isset($supported[$feature]);
  189. }
  190. @@ -395,8 +383,9 @@ class SearchApiSolrService extends SearchApiAbstractService {
  191. }
  192. $inner_type = search_api_extract_inner_type($type);
  193. - $pref = isset(self::$type_prefixes[$inner_type]) ? self::$type_prefixes[$inner_type] : '';
  194. - if ($pref != 'tm') {
  195. + $type_info = search_api_solr_get_dynamic_field_info($inner_type);
  196. + $pref = isset($type_info['prefix']) ? $type_info['prefix']: '';
  197. + if (empty($type_info['always multiValued'])) {
  198. $pref .= $type == $inner_type ? 's' : 'm';
  199. }
  200. $name = $pref . '_' . $key;