You are here

class SearchApiSolrDateSortSolrService in Search Api Solr Date Sort 7

Extension class with generic implementation of most service methods.

Hierarchy

Expanded class hierarchy of SearchApiSolrDateSortSolrService

1 string reference to 'SearchApiSolrDateSortSolrService'
search_api_solr_date_sort_search_api_service_info_alter in ./search_api_solr_date_sort.module
Implements hook_search_api_service_info_alter().

File

includes/service.inc, line 10
Contains SearchApiViewsEventsSolrService.

View source
class SearchApiSolrDateSortSolrService extends SearchApiSolrService {

  /**
   * {@inheritdoc}
   */
  public function query(SearchApiIndex $index, $options = array()) {

    // To Override the sort, we need to instantiate a different method.
    return new SearchApiSolrDateSortQuery($index, $options);
  }

  /**
   * {@inheritdoc}
   *
   * This alteration adds the first value of a multiple value date field as a string.
   * It then duplicates the events that have multiple dates, and inserts them as
   * a new document for each start date into the solr db.
   */
  protected function alterSolrDocuments(array &$documents, SearchApiIndex $index, array $items) {
    parent::alterSolrDocuments($documents, $index, $items);

    // Grab only the start date fields. Duplication isn't needed for end dates.
    $date_fields = _search_api_solr_date_sort_date_fields($index, 'dm');

    // Loop through each document to check for the date fields.
    foreach ($documents as $document) {

      // Loop Through each date field.
      //
      // @todo: Might want to limit this to a single field.
      foreach ($date_fields as $field) {

        // Grab all the date field values.
        $start_dates = $document
          ->getField($field);
        $end_dates = $document
          ->getField($field . '2');

        // If the start is not empty append the field.
        if (!empty($start_dates['value'])) {
          $string_field = substr_replace($field, 'ds_', 0, 3);

          // We account for multiple values of dates by storing each entity as a
          // new instance.
          $batches = array_chunk($start_dates['value'], 25, TRUE);
          foreach ($batches as $batch) {

            // Create a new array of new documents that will get added into the solr index.
            $new_documents = array();
            foreach ($batch as $i => $date) {
              if ($i == 0) {

                // For the first one, set the first start date.
                $document
                  ->setField($string_field, $date);

                // Set the end date.
                $document
                  ->setField($string_field . '2', $end_dates['value'][$i]);
              }
              else {

                // Clone the document to add it to the index.
                $new_document = clone $document;

                // If there are multiple dates, we need an id for each date. Thus,
                // append -###.
                $item_id = $document
                  ->getField('item_id');
                $item_id = $item_id['value'] . '-' . $i;

                // Set the new id as well
                $new_document
                  ->setField('item_id', $item_id);
                unset($new_document->{$field});
                $field_2 = $field . '2';
                unset($new_document->{$field_2});
                $hash = $document
                  ->getField('hash');
                $index_id = $document
                  ->getField('index_id');

                // Create a new id.
                $new_document
                  ->setField('id', $hash['value'] . '_' . $index_id['value'] . '_' . $item_id);

                // Add the specific date for this document.
                $new_document
                  ->setField($string_field, $date);

                // Set the end date.
                $new_document
                  ->setField($string_field . '2', $end_dates['value'][$i]);
                $new_documents[] = $new_document;
              }
            }
            $this
              ->SearchApiSolrDateSortAddNewDocs($new_documents);
          }
        }
      }
    }
  }

  /**
   * Custom function to add the new documents.
   *
   * @param array $new_documents
   *   Array of duplcicated documents
   */
  protected function SearchApiSolrDateSortAddNewDocs($new_documents) {
    $ret = array();
    if (!empty($new_documents)) {
      try {
        $this
          ->connect();
        $this->solr
          ->addDocuments($new_documents);
        if (!empty($index->options['index_directly'])) {
          $this
            ->scheduleCommit();
        }
        return $ret;
      } catch (SearchApiException $e) {
        watchdog_exception('search_api_solr', $e, "%type while indexing: !message in %function (line %line of %file).");
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SearchApiAbstractService::$options protected property Direct reference to the server's $options property.
SearchApiAbstractService::$server protected property
SearchApiAbstractService::postCreate public function Implements SearchApiServiceInterface::__construct(). Overrides SearchApiServiceInterface::postCreate
SearchApiAbstractService::postUpdate public function Implements SearchApiServiceInterface::__construct(). Overrides SearchApiServiceInterface::postUpdate
SearchApiAbstractService::preDelete public function Implements SearchApiServiceInterface::__construct(). Overrides SearchApiServiceInterface::preDelete 1
SearchApiAbstractService::__construct public function Implements SearchApiServiceInterface::__construct(). Overrides SearchApiServiceInterface::__construct
SearchApiSolrDateSortSolrService::alterSolrDocuments protected function This alteration adds the first value of a multiple value date field as a string. It then duplicates the events that have multiple dates, and inserts them as a new document for each start date into the solr db. Overrides SearchApiSolrService::alterSolrDocuments
SearchApiSolrDateSortSolrService::query public function Implements SearchApiServiceInterface::__construct(). Overrides SearchApiAbstractService::query
SearchApiSolrDateSortSolrService::SearchApiSolrDateSortAddNewDocs protected function Custom function to add the new documents.
SearchApiSolrService::$commitScheduled protected property Saves whether a commit operation was already scheduled for this server.
SearchApiSolrService::$connection_class protected property The connection class used by this service.
SearchApiSolrService::$fieldNames protected property Static cache for getFieldNames().
SearchApiSolrService::$fields protected property Metadata describing fields on the Solr/Lucene index.
SearchApiSolrService::$request_handler protected property Request handler to use for this search query.
SearchApiSolrService::$solr protected property A connection to the Solr server.
SearchApiSolrService::addIndex public function Implements SearchApiServiceInterface::__construct(). Overrides SearchApiAbstractService::addIndex
SearchApiSolrService::addIndexField protected function Helper method for indexing.
SearchApiSolrService::commit public function Sends a commit command to the Solr server.
SearchApiSolrService::configurationForm public function Implements SearchApiServiceInterface::__construct(). Overrides SearchApiAbstractService::configurationForm
SearchApiSolrService::configurationFormSubmit public function Implements SearchApiServiceInterface::__construct(). Overrides SearchApiAbstractService::configurationFormSubmit
SearchApiSolrService::configurationFormValidate public function Implements SearchApiServiceInterface::__construct(). Overrides SearchApiAbstractService::configurationFormValidate
SearchApiSolrService::connect protected function Create a connection to the Solr server as configured in $this->options.
SearchApiSolrService::createFilterQueries protected function Transforms a query filter into a flat array of Solr filter queries, using the field names in $fields.
SearchApiSolrService::createFilterQuery protected function Create a single search query string according to the given field, value and operator.
SearchApiSolrService::createId public function Creates an ID used as the unique identifier at the Solr server.
SearchApiSolrService::deleteItems public function Implements SearchApiServiceInterface::deleteItems(). Overrides SearchApiServiceInterface::deleteItems
SearchApiSolrService::extractFacets protected function Extract facets from a Solr response.
SearchApiSolrService::extractHighlightingSnippets protected function Extracts short snippets with highlighting from highlighted field values.
SearchApiSolrService::extractResults protected function Extract results from a Solr response.
SearchApiSolrService::fieldsUpdated public function Implements SearchApiServiceInterface::__construct(). Overrides SearchApiAbstractService::fieldsUpdated
SearchApiSolrService::flattenKeys protected function Flatten a keys array into a single search string.
SearchApiSolrService::formatFilterValue protected function Format a value for filtering on a field of a specific type.
SearchApiSolrService::formatHighlighting protected function Changes highlighting tags from our custom, HTML-safe ones to HTML.
SearchApiSolrService::getAutocompleteSuggestions public function
SearchApiSolrService::getConnectionClass public function Gets the Solr connection class used by this service.
SearchApiSolrService::getExcerpt protected function Extract and format highlighting information for a specific item from a Solr response.
SearchApiSolrService::getExtraInformation public function Returns additional, service-specific information about this server. Overrides SearchApiAbstractService::getExtraInformation
SearchApiSolrService::getFacetParams protected function Helper method for creating the facet field parameters.
SearchApiSolrService::getFieldNames public function Create a list of all indexed field names mapped to their Solr field names.
SearchApiSolrService::getFields public function Get metadata about fields in the Solr/Lucene index.
SearchApiSolrService::getFile public function Retrieves a config file or file list from the Solr server.
SearchApiSolrService::getHighlightingPrefixSuffix protected function Returns the prefix and suffix for highlighting matches in the excerpt.
SearchApiSolrService::getHighlightParams protected function Helper method for creating the highlighting parameters.
SearchApiSolrService::getIndexId protected function Prefixes an index ID as configured.
SearchApiSolrService::getQueryFields protected function Retrieves the effective fulltext fields from the query.
SearchApiSolrService::getServerLink public function Returns a link to the Solr server, if the necessary options are set.
SearchApiSolrService::getSolrConnection public function Gets the currently used Solr connection object.
SearchApiSolrService::indexItems public function Indexes the specified items. Overrides SearchApiServiceInterface::indexItems
SearchApiSolrService::ping public function Ping the Solr server to tell whether it can be accessed.
SearchApiSolrService::postQuery protected function Empty method to allow subclasses to apply custom changes before search results are returned.
SearchApiSolrService::preQuery protected function Empty method called before sending a search query to Solr.
SearchApiSolrService::queryMultiple public function Implements SearchApiMultiServiceInterface::queryMultiple().
SearchApiSolrService::removeIndex public function Implements SearchApiServiceInterface::__construct(). Overrides SearchApiAbstractService::removeIndex
SearchApiSolrService::sanitizeAndFormatExcerptSnippet protected function
SearchApiSolrService::sanitizeHighlightValue protected function Sanitizes a highlighted field value.
SearchApiSolrService::scheduleCommit public function Schedules a commit operation for this server.
SearchApiSolrService::search public function Executes a search on the server represented by this object. Overrides SearchApiServiceInterface::search
SearchApiSolrService::searchMultiple public function Implements SearchApiMultiServiceInterface::searchMultiple().
SearchApiSolrService::setConnectionClass public function Sets the Solr connection class used by this service.
SearchApiSolrService::setRequestHandler protected function Sets the request handler.
SearchApiSolrService::SOLR_DATE_FORMAT constant The date format that Solr uses, in PHP date() syntax.
SearchApiSolrService::supportsFeature public function Implements SearchApiServiceInterface::__construct(). Overrides SearchApiAbstractService::supportsFeature
SearchApiSolrService::viewSettings public function Overrides SearchApiAbstractService::viewSettings(). Overrides SearchApiAbstractService::viewSettings