You are here

class SearchApiSolrDateSortQuery in Search Api Solr Date Sort 7

Base extension to override the sorting to allow for the date to sort.

Hierarchy

Expanded class hierarchy of SearchApiSolrDateSortQuery

File

includes/query.inc, line 10
Contains SearchApiViewsQuery.

View source
class SearchApiSolrDateSortQuery extends SearchApiQuery {

  /**
   * {@inheritdoc}
   */

  // Largely copied from SearchApiQuery but adds handling for date string
  public function sort($field, $order = 'ASC') {
    $fields = $this->index->options['fields'];
    $fields += array(
      'search_api_relevance' => array(
        'type' => 'decimal',
      ),
      'search_api_id' => array(
        'type' => 'integer',
      ),
      'search_api_random' => array(
        'type' => 'integer',
      ),
    );

    // Add Date Handling for the sort.
    $date_fields = array();
    foreach ($fields as $k => $v) {

      // If a date field, process the name into the defined string.
      if ($v['type'] == 'list<date>') {
        $date_fields[search_api_solr_date_sort_to_string($k)] = array(
          'type' => 'date',
        );
      }
    }
    $fields += $date_fields;
    if (empty($fields[$field])) {
      throw new SearchApiException(t('Trying to sort on unknown field @field.', array(
        '@field' => $field,
      )));
    }
    $type = $fields[$field]['type'];

    // Add the check to make sure the list<date> isn't include, but other lists are excluded.
    if ($type != 'list<date>' && (search_api_is_list_type($type) || search_api_is_text_type($type))) {
      throw new SearchApiException(t('Trying to sort on field @field of illegal type @type.', array(
        '@field' => $field,
        '@type' => $type,
      )));
    }
    if ($field != 'search_api_random') {
      $order = strtoupper(trim($order)) == 'DESC' ? 'DESC' : 'ASC';
    }
    else {
      if ($order == 'ASC' || $order == "DESC") {
        $order = 'random_' . rand(1, 200) . ' asc';
      }
    }
    $this->sort[$field] = $order;
    return $this;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SearchApiQuery::$fields protected property The fields that will be searched for the keys.
SearchApiQuery::$filter protected property The search filter associated with this query.
SearchApiQuery::$index protected property The index this query will use.
SearchApiQuery::$index_id protected property The index's machine name.
SearchApiQuery::$keys protected property The search keys. If NULL, this will be a filter-only search.
SearchApiQuery::$options protected property Search options configuring this query.
SearchApiQuery::$orig_keys protected property The unprocessed search keys, as passed to the keys() method.
SearchApiQuery::$pre_execute protected property Flag for whether preExecute() was already called for this query.
SearchApiQuery::$sort protected property The sort associated with this query.
SearchApiQuery::addLanguages protected function Adds language filters for the query.
SearchApiQuery::condition public function Adds a new ($field $operator $value) condition filter. Overrides SearchApiQueryInterface::condition
SearchApiQuery::createFilter public function Creates a new filter to use with this query object. Overrides SearchApiQueryInterface::createFilter
SearchApiQuery::execute public function Executes this search query. Overrides SearchApiQueryInterface::execute
SearchApiQuery::fields public function Overrides SearchApiQueryInterface::fields
SearchApiQuery::filter public function Adds a subfilter to this query's filter. Overrides SearchApiQueryInterface::filter
SearchApiQuery::getFields public function Retrieves the fulltext fields that will be searched for the search keys. Overrides SearchApiQueryInterface::getFields
SearchApiQuery::getFilter public function Retrieves the filter object associated with this search query. Overrides SearchApiQueryInterface::getFilter
SearchApiQuery::getIndex public function Retrieves the index associated with this search. Overrides SearchApiQueryInterface::getIndex
SearchApiQuery::getKeys public function Retrieves the search keys for this query. Overrides SearchApiQueryInterface::getKeys
SearchApiQuery::getOption public function Retrieves an option set on this search query. Overrides SearchApiQueryInterface::getOption
SearchApiQuery::getOptions public function Retrieves all options set for this search query. Overrides SearchApiQueryInterface::getOptions
SearchApiQuery::getOriginalKeys public function Retrieves the unparsed search keys for this query as originally entered. Overrides SearchApiQueryInterface::getOriginalKeys
SearchApiQuery::getSort public function Retrieves the sorts set for this query. Overrides SearchApiQueryInterface::getSort
SearchApiQuery::keys public function Sets the keys to search for. Overrides SearchApiQueryInterface::keys
SearchApiQuery::parseKeys protected function
SearchApiQuery::parseModes public function Retrieves the parse modes supported by this query class. Overrides SearchApiQueryInterface::parseModes
SearchApiQuery::postExecute public function Postprocesses the search results before they are returned. Overrides SearchApiQueryInterface::postExecute
SearchApiQuery::preExecute public function Prepares the query object for the search. Overrides SearchApiQueryInterface::preExecute
SearchApiQuery::range public function Adds a range of results to return. Overrides SearchApiQueryInterface::range
SearchApiQuery::sanitizeOptions protected function Sanitizes an array of options in a way that plays nice with var_export().
SearchApiQuery::setOption public function Sets an option for this search query. Overrides SearchApiQueryInterface::setOption
SearchApiQuery::__clone public function Implements the magic __clone() method to clone the filter, too.
SearchApiQuery::__construct public function Constructs a new search query. Overrides SearchApiQueryInterface::__construct
SearchApiQuery::__sleep public function Implements the magic __sleep() method to avoid serializing the index.
SearchApiQuery::__toString public function Implements the magic __toString() method to simplify debugging.
SearchApiQuery::__wakeup public function Implements the magic __wakeup() method to reload the query's index.
SearchApiSolrDateSortQuery::sort public function Adds a sort directive to this search query. Overrides SearchApiQuery::sort