Skip to content

Instantly share code, notes, and snippets.

@dbolser
Created June 4, 2014 12:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dbolser/76154731b2c0f71d8a7c to your computer and use it in GitHub Desktop.
Save dbolser/76154731b2c0f71d8a7c to your computer and use it in GitHub Desktop.
<?php
/**
* @file
* Apache Solr external index module - example module for using external data.
*/
/**
* implements hook_apachesolr_query_alter().
*/
function apachesolr_external_index_apachesolr_query_alter(DrupalSolrQueryInterface $query) {
// Check if this is an 'external' environment...
$is_external = apachesolr_environment_variable_get(
$query->solr('getId'), 'is_external', FALSE);
// and alter the query accordingly
if($is_external) {
// Get external fields.
$query->replaceParam('fl',
array('id', 'obj_desc', 'obj_type',
'provider', 'species', 'src_url',
'timestamp', 'score', 'shard:[shard]'
)
);
// Specify the field to use for highlighting
$query->replaceParam('hl.fl', 'obj_desc');
}
}
/**
* implements hook_apachesolr_search_result_alter().
*/
function apachesolr_external_index_apachesolr_search_result_alter($doc, $extra, $query) {
// Check if this is an 'external' environment...
$is_external = apachesolr_environment_variable_get(
$query->solr('getId'), 'is_external', FALSE);
// and alter the results accordingly
if($is_external) {
// Hacking our results into a format expected by apachesolr
$doc->content = $doc->obj_desc;
$doc->teaser = $doc->obj_desc;
$doc->ds_created = $doc->timestamp;
$doc->ds_changed = $doc->timestamp;
$doc->tos_name = 'tos';
$doc->label = $doc->id; // Pretty label
$doc->path = $doc->src_url; // URL put on label
// apachesolr_search module expects entity_type to be set.
$doc->entity_type = $doc->obj_type[0];
}
}
/**
* Adds the 'external' option in the settings of any environment, if
* set, tweaks the environment a bit...
*/
function apachesolr_external_index_form_apachesolr_environment_edit_form_alter(&$form, &$form_state, $form_id) {
// If the environment already exists, we can lookup the existing
// value of 'is_external', else use FALSE
$is_external = FALSE;
if (count($form_state['build_info']['args'])) {
$environment = reset($form_state['build_info']['args']);
$is_external = apachesolr_environment_variable_get(
$environment['env_id'], 'is_external', FALSE);
}
$form['make_external'] = array(
'#type' => 'checkbox',
'#title' => t('Make this environment \'external\''),
'#default_value' => $is_external,
'#description' => t('Note, the external index will always be read-only.'),
);
// and store it appropriately in the form...
$form['actions']['save']['#submit'][] =
'apachesolr_external_index_environment_edit_submit';
$form['actions']['save_edit']['#submit'][] =
'apachesolr_external_index_environment_edit_submit';
}
/**
* Submit callback for saving an environment to make it 'external'
*/
function apachesolr_external_index_environment_edit_submit($form, &$form_state) {
$env_id = $form_state['values']['env_id'];
if ($form_state['values']['make_external']) {
apachesolr_environment_variable_set($env_id, 'is_external', TRUE);
// We also tweak the environment configuration here too...
apachesolr_environment_variable_set($env_id, 'apachesolr_read_only', 1);
// If we have facetapi installed, define our custom facets...
if(module_exists('facetapi')){
apachesolr_environment_variable_set($env_id, 'facet callbacks',
// See callback below
array('apachesolr_external_index_facet_callback'));
}
}
else {
apachesolr_environment_variable_del(
$env_id, 'is_external');
}
}
/**
* Implements an environment-specific facet callback called from
* apachesolr_facetapi_facet_info()
*
* Returns the same values as used by hook_facetapi_facet_info(). We
* could use that hook, but then our facets would be added to rather
* than replace the defaults defined by the apachesolr module.
*
* @param array $searcher_info
* The definition of the searcher that facets are being collected for.
*
* @return array
* An associative array keyed by unique name of the facet.
*/
function apachesolr_external_index_facet_callback($searcher_info) {
$facets = array();
$facets['obj_type'] = array(
'label' => t('Data type'),
'description' => t('Filter by data type.'),
'field' => 'obj_type',
'facet mincount allowed' => TRUE,
);
$facets['species'] = array(
'label' => t('Species'),
'description' => t('Filter by species.'),
'field' => 'species',
'facet mincount allowed' => TRUE,
);
$facets['provider'] = array(
'label' => t('Database'),
'description' => t('Filter by database.'),
'field' => 'provider',
'facet mincount allowed' => TRUE,
);
return $facets;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment