You are here

function apachesolr_search_add_facet_params in Apache Solr Search 6.2

Same name and namespace in other branches
  1. 5.2 apachesolr_search.module \apachesolr_search_add_facet_params()
  2. 6 apachesolr_search.module \apachesolr_search_add_facet_params()
2 calls to apachesolr_search_add_facet_params()
apachesolr_search_browse in ./apachesolr_search.module
Execute an empty search (match all documents) and show a listing of all enabled facets.
apachesolr_search_execute in ./apachesolr_search.module
Execute a search results based on keyword, filter, and sort strings.

File

./apachesolr_search.module, line 467
Provides a content search implementation for node content for use with the Apache Solr search application.

Code

function apachesolr_search_add_facet_params(&$params, $query) {
  $facet_query_limits = variable_get('apachesolr_facet_query_limits', array());
  $facet_missing = variable_get('apachesolr_facet_missing', array());
  $facet_info = apachesolr_get_facet_definitions();
  foreach (apachesolr_get_enabled_facets() as $module => $module_facets) {
    foreach ($module_facets as $delta => $facet_field) {

      // Checking the block visibility saves us from adding facet fields to the query
      // which won't be used.
      if (apachesolr_block_visibility($query, $module, $delta)) {

        // TODO: generalize handling of date and range facets.
        // TODO: put field type in the facet definitions.
        // NOTE: This is a dependency on the apachesolr_date module. Using function_exists to avoid problems.
        if (isset($facet_info[$module][$delta]['field_type']) && in_array($facet_info[$module][$delta]['field_type'], array(
          'date',
          'datetime',
          'datestamp',
        )) && function_exists('apachesolr_date_search_date_range')) {
          list($start, $end, $gap) = apachesolr_date_search_date_range($query, $facet_field);
          if ($gap) {
            $params['facet.date'][] = $facet_field;
            $params['f.' . $facet_field . '.facet.date.start'] = $start;
            $params['f.' . $facet_field . '.facet.date.end'] = $end;
            $params['f.' . $facet_field . '.facet.date.gap'] = $gap;
          }
        }
        elseif ($module == 'apachesolr_search' && ($facet_field == 'created' || $facet_field == 'changed')) {
          list($start, $end, $gap) = apachesolr_search_date_range($query, $facet_field);
          if ($gap) {
            $params['facet.date'][] = $facet_field;
            $params['f.' . $facet_field . '.facet.date.start'] = $start;
            $params['f.' . $facet_field . '.facet.date.end'] = $end;
            $params['f.' . $facet_field . '.facet.date.gap'] = $gap;
          }
        }
        else {
          $ex = '';
          if ($facet_info[$module][$delta]['operator'] == 'OR') {
            $ex = "{!ex={$facet_field}}";
          }
          $params['facet.field'][] = $ex . $facet_field;

          // Facet limits
          if (isset($facet_query_limits[$module][$delta])) {
            $params['f.' . $facet_field . '.facet.limit'] = $facet_query_limits[$module][$delta];
          }

          // Facet missing
          if (!empty($facet_missing[$module][$delta])) {
            $params['f.' . $facet_field . '.facet.missing'] = 'true';
          }
        }
      }
    }
  }
  if (!empty($params['facet.field'])) {

    // Add a default limit for fields where no limit was set.
    $params['facet.limit'] = variable_get('apachesolr_facet_query_limit_default', 20);
  }
}