Exemplo n.º 1
0
/**
 * Alter the query after it's prepared and cached.
 *
 * Any module performing a search should call
 * drupal_alter('apachesolr_query', $query). That function then invokes this
 * hook. It allows modules to modify the query object and its parameters.
 *
 * A module implementing HOOK_apachesolr_query_alter() may set
 * $query->abort_search to TRUE to flag the query to be aborted.
 *
 * @param DrupalSolrQueryInterface $query
 *   An object implementing DrupalSolrQueryInterface. No need for &.
 */
function hook_apachesolr_query_alter(DrupalSolrQueryInterface $query)
{
    // I only want to see articles by the admin.
    //
    // NOTE: this "is_uid" filter does NOT refer to the English word "is"
    // It is a combination of flags representing Integer-Single, which is
    // abbreviated with the letters i and s.
    //
    // @see the <dynamicField> definitions in schema.xml or schema-solr3.xml
    $query->addFilter("is_uid", 1);
    // Only search titles.
    $query->replaceParam('qf', 'label');
}
Exemplo n.º 2
0
/**
 * Alter the query after it's prepared and cached.
 *
 * Any module performing a search should call
 * drupal_alter('apachesolr_query', $query). That function then invokes this
 * hook. It allows modules to modify the query object and its parameters.
 *
 * A module implementing HOOK_apachesolr_query_alter() may set
 * $query->abort_search to TRUE to flag the query to be aborted.
 *
 * @param DrupalSolrQueryInterface $query
 *   An object implementing DrupalSolrQueryInterface. No need for &.
 *
 * @see /admin/reports/apachesolr
 * - This report displays the active solr index fields and can help you
 *   create Solr filters based on the data currently in your system
 */
function hook_apachesolr_query_alter(DrupalSolrQueryInterface $query)
{
    // I only want to see articles by the admin.
    //
    // NOTE: this "is_uid" filter does NOT refer to the English word "is"
    // It is a combination of flags representing Integer-Single, which is
    // abbreviated with the letters i and s.
    //
    // @see the <dynamicField> definitions in schema.xml or schema-solr3.xml
    $query->addFilter("is_uid", 1);
    // Only search titles.
    $query->replaceParam('qf', 'label');
    // Restrict results to a single content type (use machine name).
    $query->addFilter('bundle', 'my_content_type');
    // Exclude results by setting the third argument of addFilter to TRUE.
    // This filter will return all content types EXCEPT my_content_type nodes.
    $query->addFilter('bundle', 'my_content_type', TRUE);
    // Restrict results to several content types (use machine names).
    // You could also solve this using the SolrFilterSubQuery object and append it
    // to the original query.
    $content_types = array('content_type_1', 'content_type_2');
    $query->addFilter('bundle', '(' . implode(' OR ', $content_types) . ')');
}