patch add custom field types

Signed-off-by: bachy <git@g-u-i.net>
This commit is contained in:
bachy 2013-03-12 19:15:12 +01:00
parent 6a402551d7
commit c9912105d5
4 changed files with 336 additions and 24 deletions

View File

@ -0,0 +1,205 @@
diff --git a/search_api_solr.api.php b/search_api_solr.api.php
index f407407..ec4b190 100644
--- a/search_api_solr.api.php
+++ b/search_api_solr.api.php
@@ -85,5 +85,45 @@ function hook_search_api_solr_multi_query_alter(array &$call_args, SearchApiMult
}
/**
+ * Define how Search API Solr should index different data types.
+ *
+ * It is important to make sure that any types you define are also declared to
+ * Search API using hook_search_api_data_type_info().
+ *
+ * @return array
+ * An array containing data type definitions, keyed by their type identifier
+ * and containing the following keys:
+ * - prefix: The prefix used by the dynamic field type.
+ * - always multiValued: (optional) Whether the single/multiple prefix should
+ * be skipped for this data type. Defaults to FALSE.
+ *
+ * @see hook_search_api_solr_dynamic_field_info_alter()
+ * @see search_api_solr_get_dynamic_field_info()
+ * @see hook_search_api_data_type_info().
+ */
+function hook_search_api_solr_dynamic_field_info() {
+ return array(
+ 'example_type' => array(
+ 'prefix' => 'ex',
+ // Could be omitted, as FALSE is the default.
+ 'always multiValued' => FALSE,
+ ),
+ );
+}
+
+/**
+ * Alter the data type indexing info.
+ *
+ * @param array $infos
+ * The item type info array, keyed by type identifier.
+ *
+ * @see hook_search_api_solr_dynamic_field_info()
+ */
+function hook_search_api_solr_dynamic_field_info_alter(array &$infos) {
+ // Change the prefix used for example_type.
+ $info['example_type']['prefix'] = 'ex2';
+}
+
+/**
* @} End of "addtogroup hooks".
*/
diff --git a/search_api_solr.module b/search_api_solr.module
index 9e30397..b75319b 100644
--- a/search_api_solr.module
+++ b/search_api_solr.module
@@ -143,3 +143,81 @@ function search_api_solr_cron() {
}
}
}
+
+/**
+ * Returns either all dynamic field types, or a specific one.
+ *
+ * @param $type
+ * If specified, the type whose definition should be returned.
+ *
+ * @return array
+ * If $type was not given, an array containing all custom dynamic fields, in
+ * the format specified by hook_search_api_solr_dynamic_field_info().
+ * Otherwise, the definition for the given type, or NULL if it is unknown.
+ *
+ * @see hook_search_api_solr_dynamic_field_info().
+ */
+function search_api_solr_get_dynamic_field_info($type = NULL) {
+ $types = &drupal_static(__FUNCTION__);
+ if (!isset($types)) {
+ $types = module_invoke_all('search_api_solr_dynamic_field_info');
+ $types = $types ? $types : array();
+ drupal_alter('search_api_solr_dynamic_field_info', $types);
+ }
+ if (isset($type)) {
+ return isset($types[$type]) ? $types[$type] : NULL;
+ }
+ return $types;
+}
+
+/**
+ * Implements hook_search_api_solr_dynamic_field_info().
+ */
+function search_api_solr_search_api_solr_dynamic_field_info() {
+ return array(
+ 'text' => array(
+ 'prefix' => 'tm',
+ 'always multiValued' => TRUE,
+ ),
+ 'tokens' => array(
+ 'prefix' => 'tm',
+ 'always multiValued' => TRUE,
+ ),
+ 'string' => array(
+ 'prefix' => 's',
+ 'always multiValued' => FALSE,
+ ),
+ 'integer' => array(
+ 'prefix' => 'i',
+ 'always multiValued' => FALSE,
+ ),
+ 'decimal' => array(
+ 'prefix' => 'f',
+ 'always multiValued' => FALSE,
+ ),
+ 'date' => array(
+ 'prefix' => 'd',
+ 'always multiValued' => FALSE,
+ ),
+ 'duration' => array(
+ 'i',
+ 'always multiValued' => FALSE,
+ ),
+ 'boolean' => array(
+ 'b',
+ 'always multiValued' => FALSE,
+ ),
+ 'uri' => array(
+ 's',
+ 'always multiValued' => FALSE,
+ ),
+ 'location' => array(
+ 'loc',
+ 'always multiValued' => FALSE,
+ ),
+ 'geohash' => array(
+ 'geohash',
+ 'always multiValued' => FALSE,
+ ),
+ );
+}
diff --git a/service.inc b/service.inc
index 0e8dbee..5054c51 100644
--- a/service.inc
+++ b/service.inc
@@ -18,26 +18,6 @@ class SearchApiSolrService extends SearchApiAbstractService {
protected $solr;
/**
- * An array of all recognized types mapped to a prefix used for identifying
- * them in the Solr schema.
- *
- * @var array
- */
- protected static $type_prefixes = array(
- 'text' => 'tm',
- 'tokens' => 'tm',
- 'string' => 's',
- 'integer' => 'i',
- 'decimal' => 'f',
- 'date' => 'd',
- 'duration' => 'i',
- 'boolean' => 'b',
- 'uri' => 's',
- 'location' => 'loc',
- 'geohash' => 'geohash',
- );
-
- /**
* @var array
*/
protected $fieldNames = array();
@@ -222,7 +202,8 @@ class SearchApiSolrService extends SearchApiAbstractService {
}
public function supportsFeature($feature) {
- $supported = drupal_map_assoc(array(
+ // Search API features.
+ $supported = array(
'search_api_autocomplete',
'search_api_facets',
'search_api_facets_operator_or',
@@ -231,7 +212,14 @@ class SearchApiSolrService extends SearchApiAbstractService {
'search_api_spellcheck',
'search_api_data_type_location',
'search_api_data_type_geohash',
- ));
+ );
+
+ // Custom data types.
+ foreach (search_api_solr_get_dynamic_field_info() as $type => $info) {
+ $supported[] = 'search_api_data_type_' . $type;
+ }
+
+ $supported = drupal_map_assoc($supported);
return isset($supported[$feature]);
}
@@ -395,8 +383,9 @@ class SearchApiSolrService extends SearchApiAbstractService {
}
$inner_type = search_api_extract_inner_type($type);
- $pref = isset(self::$type_prefixes[$inner_type]) ? self::$type_prefixes[$inner_type] : '';
- if ($pref != 'tm') {
+ $type_info = search_api_solr_get_dynamic_field_info($inner_type);
+ $pref = isset($type_info['prefix']) ? $type_info['prefix']: '';
+ if (empty($type_info['always multiValued'])) {
$pref .= $type == $inner_type ? 's' : 'm';
}
$name = $pref . '_' . $key;

View File

@ -84,6 +84,46 @@ function hook_search_api_solr_multi_query_alter(array &$call_args, SearchApiMult
}
}
/**
* Define how Search API Solr should index different data types.
*
* It is important to make sure that any types you define are also declared to
* Search API using hook_search_api_data_type_info().
*
* @return array
* An array containing data type definitions, keyed by their type identifier
* and containing the following keys:
* - prefix: The prefix used by the dynamic field type.
* - always multiValued: (optional) Whether the single/multiple prefix should
* be skipped for this data type. Defaults to FALSE.
*
* @see hook_search_api_solr_dynamic_field_info_alter()
* @see search_api_solr_get_dynamic_field_info()
* @see hook_search_api_data_type_info().
*/
function hook_search_api_solr_dynamic_field_info() {
return array(
'example_type' => array(
'prefix' => 'ex',
// Could be omitted, as FALSE is the default.
'always multiValued' => FALSE,
),
);
}
/**
* Alter the data type indexing info.
*
* @param array $infos
* The item type info array, keyed by type identifier.
*
* @see hook_search_api_solr_dynamic_field_info()
*/
function hook_search_api_solr_dynamic_field_info_alter(array &$infos) {
// Change the prefix used for example_type.
$info['example_type']['prefix'] = 'ex2';
}
/**
* @} End of "addtogroup hooks".
*/

View File

@ -143,3 +143,81 @@ function search_api_solr_cron() {
}
}
}
/**
* Returns either all dynamic field types, or a specific one.
*
* @param $type
* If specified, the type whose definition should be returned.
*
* @return array
* If $type was not given, an array containing all custom dynamic fields, in
* the format specified by hook_search_api_solr_dynamic_field_info().
* Otherwise, the definition for the given type, or NULL if it is unknown.
*
* @see hook_search_api_solr_dynamic_field_info().
*/
function search_api_solr_get_dynamic_field_info($type = NULL) {
$types = &drupal_static(__FUNCTION__);
if (!isset($types)) {
$types = module_invoke_all('search_api_solr_dynamic_field_info');
$types = $types ? $types : array();
drupal_alter('search_api_solr_dynamic_field_info', $types);
}
if (isset($type)) {
return isset($types[$type]) ? $types[$type] : NULL;
}
return $types;
}
/**
* Implements hook_search_api_solr_dynamic_field_info().
*/
function search_api_solr_search_api_solr_dynamic_field_info() {
return array(
'text' => array(
'prefix' => 'tm',
'always multiValued' => TRUE,
),
'tokens' => array(
'prefix' => 'tm',
'always multiValued' => TRUE,
),
'string' => array(
'prefix' => 's',
'always multiValued' => FALSE,
),
'integer' => array(
'prefix' => 'i',
'always multiValued' => FALSE,
),
'decimal' => array(
'prefix' => 'f',
'always multiValued' => FALSE,
),
'date' => array(
'prefix' => 'd',
'always multiValued' => FALSE,
),
'duration' => array(
'i',
'always multiValued' => FALSE,
),
'boolean' => array(
'b',
'always multiValued' => FALSE,
),
'uri' => array(
's',
'always multiValued' => FALSE,
),
'location' => array(
'loc',
'always multiValued' => FALSE,
),
'geohash' => array(
'geohash',
'always multiValued' => FALSE,
),
);
}

View File

@ -17,26 +17,6 @@ class SearchApiSolrService extends SearchApiAbstractService {
*/
protected $solr;
/**
* An array of all recognized types mapped to a prefix used for identifying
* them in the Solr schema.
*
* @var array
*/
protected static $type_prefixes = array(
'text' => 'tm',
'tokens' => 'tm',
'string' => 's',
'integer' => 'i',
'decimal' => 'f',
'date' => 'd',
'duration' => 'i',
'boolean' => 'b',
'uri' => 's',
'location' => 'loc',
'geohash' => 'geohash',
);
/**
* @var array
*/
@ -222,7 +202,8 @@ class SearchApiSolrService extends SearchApiAbstractService {
}
public function supportsFeature($feature) {
$supported = drupal_map_assoc(array(
// Search API features.
$supported = array(
'search_api_autocomplete',
'search_api_facets',
'search_api_facets_operator_or',
@ -231,7 +212,14 @@ class SearchApiSolrService extends SearchApiAbstractService {
'search_api_spellcheck',
'search_api_data_type_location',
'search_api_data_type_geohash',
));
);
// Custom data types.
foreach (search_api_solr_get_dynamic_field_info() as $type => $info) {
$supported[] = 'search_api_data_type_' . $type;
}
$supported = drupal_map_assoc($supported);
return isset($supported[$feature]);
}
@ -395,8 +383,9 @@ class SearchApiSolrService extends SearchApiAbstractService {
}
$inner_type = search_api_extract_inner_type($type);
$pref = isset(self::$type_prefixes[$inner_type]) ? self::$type_prefixes[$inner_type] : '';
if ($pref != 'tm') {
$type_info = search_api_solr_get_dynamic_field_info($inner_type);
$pref = isset($type_info['prefix']) ? $type_info['prefix']: '';
if (empty($type_info['always multiValued'])) {
$pref .= $type == $inner_type ? 's' : 'm';
}
$name = $pref . '_' . $key;