Skip to content

Instantly share code, notes, and snippets.

@heddn
Created February 2, 2013 03:01
Show Gist options
  • Select an option

  • Save heddn/4695944 to your computer and use it in GitHub Desktop.

Select an option

Save heddn/4695944 to your computer and use it in GitHub Desktop.
apachesolr module examples
/**
* Per-document boosts.
*
* @param object $query
* An object implementing DrupalSolrQueryInterface
*/
function example_solr_promote($query) {
// ask to return the field from solr
$query->addParam('fl', array(
'bs_field_search_promote' // field_search_promote is a single value boolean field to turn on/off solr boosting
));
// Add an insane boost value
$promote_boost = variable_get('mercy_search_promote', '999999999.0');
$query->addParam('bq', array(
"bs_field_search_promote:true^$promote_boost"
));
}
/**
* Boosts search results.
*
* @param object $document
* An object implementing ApacheSolrDocument
* @param object $entity
* The entity being created.
*/
function example_solr_boost(ApacheSolrDocument $document, $entity) {
// Field search boost has allowed values of: array(0 => \'None\',3 => \'Minor\',5 => \'Moderate\',8 => \'Major\',)
if (isset($entity->field_search_boost[LANGUAGE_NONE][0]['value'])) {
$boost = $entity->field_search_boost[LANGUAGE_NONE][0]['value'] * variable_get('example_search_boost', .38);
$document->setBoost($boost);
}
}
/**
* Adds the correct content types & filters based on if a cookie is set
*
* @param object $query
* An object implementing DrupalSolrQueryInterface
*/
function example_solr_community_filter($query) {
// Do not include child page content type if no cookie selected.
if ($COOKIE['foo']) {
$query->addFilter('bundle', '(content_type_1 OR content_type_2 OR parent_content_type)');
}
else {
// Build our subqueries so we can pull all the appropriate "base" bundles
// and also apply the appropriate filters to the cookie-specific ones.
$subQueryParent = new SolrFilterSubQuery();
$baseBundles = new SolrFilterSubQuery();
$baseBundles->addFilter('bundle', '(parent_content_type)');
$subQueryParent->addFilterSubQuery($baseBundles);
$childPages = new SolrFilterSubQuery('AND');
$childPages->addFilter('bundle', '(content_type_1 OR content_type_2 OR parent_content_type)');
$childPages->addFilter('im_field_parent_reference', $COOKIE['foo']);
$subQueryParent->addFilterSubQuery($childPages);
$query->addFilterSubQuery($subQueryParent);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment