You are here

class SearchApiGroupingGrouping in Search API Grouping 7

Processor for grouping up items on behalf of user defined fields.

Hierarchy

Expanded class hierarchy of SearchApiGroupingGrouping

1 string reference to 'SearchApiGroupingGrouping'
search_api_grouping_search_api_processor_info in ./search_api_grouping.module
Implements hook_search_api_processor_info().

File

includes/processor_grouping.inc, line 11
Processor for grouping support.

View source
class SearchApiGroupingGrouping extends SearchApiAbstractProcessor {

  /**
   * Check if the index is supports this feature.
   */
  public function supportsIndex(SearchApiIndex $index) {
    return $index
      ->server()
      ->supportsFeature('search_api_grouping');
  }

  /**
   * Return the settings form for this processor.
   */
  public function configurationForm() {
    $form = parent::configurationForm();
    $supported_fields = $this
      ->getSupportedFields();
    $form['fields'] = array(
      '#type' => 'checkboxes',
      '#title' => t('Fields to collapse on'),
      '#options' => $supported_fields['field_options'],
      '#default_value' => $supported_fields['default_fields'],
      '#attributes' => array(
        'class' => array(
          'search-api-checkboxes-list',
        ),
      ),
      '#description' => t('Choose the fields upon which to collapse the results into groups. Note that while selecting multiple fields is technicially supported, it may result in unexpected behaviour.'),
    );

    // Apache solr specific options.
    if ($this->index
      ->server()->class == 'search_api_solr_service' || is_subclass_of($this->index
      ->server()->class, 'search_api_solr_service')) {
      $default_sort = isset($this->options['group_sort']) ? $this->options['group_sort'] : '';
      $form['group_sort'] = array(
        '#type' => 'select',
        '#title' => t('Group sort'),
        '#options' => $supported_fields['field_sorts'],
        '#default_value' => $default_sort,
        '#description' => t('Choose the field by to sort within each group, the groups themselves will be sorted by the main query sorts.'),
      );
      $default_sort_direction = isset($this->options['group_sort_direction']) ? $this->options['group_sort_direction'] : '';
      $form['group_sort_direction'] = array(
        '#type' => 'select',
        '#title' => t('Group sort direction'),
        '#options' => array(
          'asc' => t('Ascending'),
          'desc' => t('Descending'),
        ),
        '#default_value' => $default_sort_direction,
      );
      $default_truncate = isset($this->options['truncate']) ? $this->options['truncate'] : TRUE;
      $form['truncate'] = array(
        '#type' => 'checkbox',
        '#title' => t('Truncate results before facets'),
        '#description' => t('If checked, facet counts are based on the most relevant document of each group matching the query, otherwise they are calculated for all documents before grouping.'),
        '#default_value' => $default_truncate,
      );
    }
    return $form;
  }

  /**
   * Returns an array of supported fields to choose of.
   *
   * This function respects the server behind the index to provide only valid
   * fields.
   *
   * @return array
   *   An associative array with child arrays for the supported fields for each
   *   feature:
   *   array(
   *    'field_options' => array(),
   *    'field_sorts' => array(),
   *    'field' => array(),
   *   );
   */
  protected function getSupportedFields() {
    $this->index
      ->server()->class;
    $fields = $this->index
      ->getFields();
    $supported_fields = array(
      'field_options' => array(),
      'field_sorts' => array(
        '' => t('None'),
        'score' => t('Score/Relevance'),
      ),
      'default_fields' => array(),
    );
    if (isset($this->options['fields'])) {
      $supported_fields['default_fields'] = drupal_map_assoc(array_keys($this->options['fields']));
    }
    foreach ($fields as $name => $field) {

      // We can only rely on indexed fields.
      if ($field['indexed']) {

        // @TODO Add other supported servers.
        switch (TRUE) {

          // Apache solr server.
          case $this->index
            ->server()->class == 'search_api_solr_service' || is_subclass_of($this->index
            ->server()->class, 'search_api_solr_service'):

          // Currently Solr is only compatible with single valued, indexed,
          // string/integer fields.
          default:
            if (!search_api_is_list_type($field['type'])) {
              if ($field['type'] == 'string' || $field['type'] == 'integer') {
                $supported_fields['field_options'][$name] = $field['name'];
                if (!empty($default_fields[$name]) || !isset($this->options['fields']) && $this
                  ->testField($name, $field)) {
                  $supported_fields['default_fields'][$name] = $name;
                }
              }

              // We can only sort according to single-valued fields.
              if ($field['type'] == search_api_extract_inner_type($field['type'])) {
                $supported_fields['field_sorts'][$name] = $field['name'];
              }
            }
            break;
        }
      }
    }
    return $supported_fields;
  }

  /**
   * Set the options so the server adapter can use the to implement grouping.
   */
  public function preprocessSearchQuery(SearchApiQuery $query) {

    // We move the options from our options array into where the Solr Service is
    // expecting them.
    $options = array(
      'use_grouping' => TRUE,
      'fields' => isset($this->options['fields']) ? array_keys($this->options['fields']) : array(),
      // Contains also Apache solr specific options.
      'group_sort' => isset($this->options['group_sort']) ? $this->options['group_sort'] : '',
      'group_sort_direction' => isset($this->options['group_sort_direction']) ? $this->options['group_sort_direction'] : 'asc',
      'truncate' => isset($this->options['truncate']) ? $this->options['truncate'] : TRUE,
    );
    $query
      ->setOption('search_api_grouping', $options);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SearchApiAbstractProcessor::$index protected property
SearchApiAbstractProcessor::$options protected property
SearchApiAbstractProcessor::configurationFormSubmit public function Submit callback for the form returned by configurationForm(). Overrides SearchApiProcessorInterface::configurationFormSubmit
SearchApiAbstractProcessor::configurationFormValidate public function Validation callback for the form returned by configurationForm(). Overrides SearchApiProcessorInterface::configurationFormValidate 4
SearchApiAbstractProcessor::implodeTokens protected function Internal helper function for imploding tokens into a single string.
SearchApiAbstractProcessor::normalizeTokens protected function Internal helper function for normalizing tokens.
SearchApiAbstractProcessor::postprocessSearchResults public function Does nothing. Overrides SearchApiProcessorInterface::postprocessSearchResults 2
SearchApiAbstractProcessor::preprocessIndexItems public function Calls processField() for all appropriate fields. Overrides SearchApiProcessorInterface::preprocessIndexItems
SearchApiAbstractProcessor::process protected function Function that is ultimately called for all text by the standard implementation, and does nothing by default. 5
SearchApiAbstractProcessor::processField protected function Method for preprocessing field data.
SearchApiAbstractProcessor::processFieldValue protected function Called for processing a single text element in a field. The default implementation just calls process(). 2
SearchApiAbstractProcessor::processFilters protected function Method for preprocessing query filters.
SearchApiAbstractProcessor::processFilterValue protected function Called for processing a single filter value. The default implementation just calls process().
SearchApiAbstractProcessor::processKey protected function Called for processing a single search keyword. The default implementation just calls process().
SearchApiAbstractProcessor::processKeys protected function Method for preprocessing search keys.
SearchApiAbstractProcessor::testField protected function Determines whether to process data from the given field.
SearchApiAbstractProcessor::testType protected function Determines whether fields of the given type should normally be processed.
SearchApiAbstractProcessor::__construct public function Constructor, saving its arguments into properties. Overrides SearchApiProcessorInterface::__construct 2
SearchApiGroupingGrouping::configurationForm public function Return the settings form for this processor. Overrides SearchApiAbstractProcessor::configurationForm
SearchApiGroupingGrouping::getSupportedFields protected function Returns an array of supported fields to choose of.
SearchApiGroupingGrouping::preprocessSearchQuery public function Set the options so the server adapter can use the to implement grouping. Overrides SearchApiAbstractProcessor::preprocessSearchQuery
SearchApiGroupingGrouping::supportsIndex public function Check if the index is supports this feature. Overrides SearchApiAbstractProcessor::supportsIndex