1846860-8--dynamic_field_info.patch 6.2 KB

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