You are here

class ResultSet in Google Search Appliance 8

Defines a value object for a search response.

Hierarchy

Expanded class hierarchy of ResultSet

4 files declare their use of ResultSet
google_appliance.api.php in ./google_appliance.api.php
Hook documentation.
Parser.php in src/Service/Parser.php
Search.php in src/Service/Search.php
TestSearch.php in tests/modules/google_appliance_test/src/TestSearch.php

File

src/SearchResults/ResultSet.php, line 18

Namespace

Drupal\google_appliance\SearchResults
View source
class ResultSet {
  use StringTranslationTrait;
  const ERROR_PARSING = 'lib_xml_parse_error';
  const ERROR_NO_RESULTS = 'gsa_no_results';
  const ERROR_HTTP = 'http_error';

  /**
   * Display title.
   *
   * @var string
   */
  protected $searchTitle;

  /**
   * Response errors keyed by type.
   *
   * @var string[]
   */
  protected $errors = [];

  /**
   * Total results.
   *
   * @var int
   */
  protected $total = 0;

  /**
   * Result objects.
   *
   * @var \Drupal\google_appliance\SearchResults\Result[]
   */
  protected $results = [];

  /**
   * Spelling suggestions.
   *
   * @var string[]
   */
  protected $spellingSuggestions = [];

  /**
   * Key matches.
   *
   * @var \Drupal\google_appliance\SearchResults\KeyMatch[]
   */
  protected $keyMatches = [];

  /**
   * Synonyms.
   *
   * @var \Drupal\google_appliance\SearchResults\Synonym[]
   */
  protected $synonyms = [];

  /**
   * Last result.
   *
   * @var int
   */
  protected $lastResult = 0;

  /**
   * One box result keyed by module name.
   *
   * @var \Drupal\google_appliance\SearchResults\OneBoxResultSet[]
   */
  protected $oneBoxResultSets = [];

  /**
   * Original query object.
   *
   * @var \Drupal\google_appliance\SearchResults\SearchQuery
   */
  protected $query;

  /**
   * Results per page.
   *
   * @var int
   */
  protected $resultsPerPage;

  /**
   * Check if response is in error.
   *
   * @return bool
   *   TRUE if errors occurred.
   */
  public function hasErrors() {
    return (bool) array_filter($this->errors);
  }

  /**
   * Sets total.
   *
   * @param int $total
   *   Total.
   *
   * @return $this
   */
  public function setTotal($total) {
    $this->total = $total;
    return $this;
  }

  /**
   * Adds error.
   *
   * @param string $message
   *   Error.
   * @param string $type
   *   Error type.
   *
   * @return $this
   */
  public function addError($message, $type = self::ERROR_PARSING) {
    $this->errors[$type][] = $message;
    return $this;
  }

  /**
   * Adds spelling suggestion.
   *
   * @param string $string
   *   Suggestion.
   *
   * @return $this
   */
  public function addSpellingSuggestion($string) {
    $this->spellingSuggestions[] = $string;
    return $this;
  }

  /**
   * Adds key match.
   *
   * @param \Drupal\google_appliance\SearchResults\KeyMatch $match
   *   Match.
   *
   * @return $this
   */
  public function addKeyMatch(KeyMatch $match) {
    $this->keyMatches[] = $match;
    return $this;
  }

  /**
   * Adds synonym.
   *
   * @param \Drupal\google_appliance\SearchResults\Synonym $synonym
   *   Synonym.
   *
   * @return $this
   */
  public function addSynonym(Synonym $synonym) {
    $this->synonyms[] = $synonym;
    return $this;
  }

  /**
   * Adds a result.
   *
   * @param \Drupal\google_appliance\SearchResults\Result $result
   *   Result.
   *
   * @return $this
   */
  public function addResult(Result $result) {
    $this->results[] = $result;
    return $this;
  }

  /**
   * Gets value of errors.
   *
   * @return array
   *   Value of errors
   */
  public function getErrors() {
    return $this->errors;
  }

  /**
   * Gets value of total.
   *
   * @return int
   *   Value of total
   */
  public function getTotal() {
    return $this->total;
  }

  /**
   * Gets value of results.
   *
   * @return \Drupal\google_appliance\SearchResults\Result[]
   *   Value of results
   */
  public function getResults() {
    return $this->results;
  }

  /**
   * Gets value of spellingSuggestions.
   *
   * @return array
   *   Value of spellingSuggestions
   */
  public function getSpellingSuggestions() {
    return $this->spellingSuggestions;
  }

  /**
   * Gets value of keyMatches.
   *
   * @return array
   *   Value of keyMatches
   */
  public function getKeyMatch() {
    return $this->keyMatches;
  }

  /**
   * Gets value of synonyms.
   *
   * @return array
   *   Value of synonyms
   */
  public function getSynonyms() {
    return $this->synonyms;
  }

  /**
   * Gets value of lastResult.
   *
   * @return string
   *   Value of lastResult
   */
  public function getLastResult() {
    return $this->lastResult;
  }

  /**
   * Sets last result.
   *
   * @param string $lastResult
   *   New value for last result.
   *
   * @return $this
   */
  public function setLastResult($lastResult) {
    $this->lastResult = $lastResult;
    return $this;
  }

  /**
   * Adds one box result set.
   *
   * @param string $module_name
   *   One box module.
   * @param \Drupal\google_appliance\SearchResults\OneBoxResultSet $onebox
   *   Results set.
   *
   * @return $this
   */
  public function addOneBoxResultSet($module_name, OneBoxResultSet $onebox) {
    $this->oneBoxResultSets[$module_name] = $onebox;
    return $this;
  }

  /**
   * Check if there are results.
   *
   * @return bool
   *   TRUE if there are results.
   */
  public function hasResults() {
    return (bool) count($this->results);
  }

  /**
   * Check if there are synonyms.
   *
   * @return bool
   *   TRUE if synonyms exist.
   */
  public function hasSynonyms() {
    return (bool) count($this->synonyms);
  }

  /**
   * Check if there are key matches.
   *
   * @return bool
   *   TRUE if key matches exist.
   */
  public function hasKeyMatch() {
    return (bool) count($this->keyMatches);
  }

  /**
   * Check if there are suggestions.
   *
   * @return bool
   *   TRUE if suggestions exist.
   */
  public function hasSpellingSuggestions() {
    return (bool) count($this->spellingSuggestions);
  }

  /**
   * Gets value of searchTitle.
   *
   * @return string
   *   Value of searchTitle
   */
  public function getSearchTitle() {
    return $this->searchTitle;
  }

  /**
   * Sets searchTitle.
   *
   * @param string $searchTitle
   *   New value for searchTitle.
   *
   * @return $this
   */
  public function setSearchTitle($searchTitle) {
    $this->searchTitle = $searchTitle;
    return $this;
  }

  /**
   * Gets value of query.
   *
   * @return \Drupal\google_appliance\SearchResults\SearchQuery
   *   Value of query
   */
  public function getQuery() {
    return $this->query;
  }

  /**
   * Sets query.
   *
   * @param \Drupal\google_appliance\SearchResults\SearchQuery $query
   *   New value for query.
   *
   * @return \Drupal\google_appliance\SearchResults\ResultSet
   *   The ResultSet instance.
   */
  public function setQuery(SearchQuery $query) {
    $this->query = $query;
    return $this;
  }

  /**
   * Gets statistics information for search result.
   *
   * @return \Drupal\Core\StringTranslation\TranslatableMarkup
   *   Statistics information.
   */
  public function getStatistics() {
    return $this
      ->t('Showing results @first - @last for %query', [
      '@first' => (int) $this->lastResult - count($this->results) + 1,
      '@last' => $this->lastResult,
      '%query' => $this->query
        ->getSearchQuery(),
    ]);
  }

  /**
   * Returns an array of sort links.
   *
   * @return array
   *   Sort links.
   */
  public function getSortLinks() {
    $links = [];
    if ($this->query
      ->getSort() === SearchQuery::ORDER_DATE) {
      $links[] = Link::fromTextAndUrl($this
        ->t('Relevance'), Url::fromRoute(SearchViewRoute::ROUTE_NAME, [
        'search_query' => $this->query
          ->getSearchQuery(),
        'result_sort' => 'rel',
      ])
        ->setAbsolute()
        ->setOption('query', [
        'page' => (string) $this->query
          ->getPage(),
      ]));
      $links[] = $this
        ->t('Date');
      return $links;
    }
    $links[] = $this
      ->t('Relevance');
    $links[] = Link::fromTextAndUrl($this
      ->t('Date'), Url::fromRoute(SearchViewRoute::ROUTE_NAME, [
      'search_query' => $this->query
        ->getSearchQuery(),
      'result_sort' => 'date',
    ])
      ->setAbsolute()
      ->setOption('query', [
      'page' => (string) $this->query
        ->getPage(),
    ]));
    return $links;
  }

  /**
   * Returns render array for pager.
   *
   * @return array
   *   Render array.
   */
  public function getPager() {

    // Globals required to manually configure the pager.
    global $pager_page_array, $pager_total, $pager_total_items;
    $element = 0;

    /* @note
     * The total results count from the GSA is unreliable. The docs
     * state that it is an *approximation*, but if you click around on
     * enough searches, you'll find that it's off by a considerable amount -
     * enough to break the math used to create pager links. It's not all that
     * noticeable unless you click on the "last" link in the pager, and
     * notice that in some searches, the last page is paginated at a number
     * less than was previously viewable when the first page of results came
     * up on the initial search view.
     *
     * The problem is rooted in public vs. access-controlled indexing.
     * @see groups.google.com/group/Google-Search-Appliance-Help/browse_thread/thread/019b77fb3e7950c7
     * Access-controlled results are counted before we know if we can actually
     * view it. Device configuration can help.
     *
     * @TODO: perhaps a better solution is to just query the device for the
     * first X (up to 1000) results and cache them locally.
     */

    // Manually configure (fake) the pager.
    $page = $this->query
      ->getPage();

    // Convert page id to array.
    $pager_page_array = explode(',', $page);

    // Set the total results.
    $pager_total_items[$element] = (int) $this->total;

    // Set the total # of pages.
    $pager_total[$element] = ceil($pager_total_items[$element] / $this->resultsPerPage);
    $pager_page_array[$element] = max(0, min((int) $pager_page_array[$element], (int) $pager_total[$element] - 1));
    return [
      '#type' => 'pager',
    ];
  }

  /**
   * Sets results per page.
   *
   * @param int $resultsPerPage
   *   New value for resultsPerPage.
   *
   * @return $this
   */
  public function setResultsPerPage($resultsPerPage) {
    $this->resultsPerPage = $resultsPerPage;
    return $this;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ResultSet::$errors protected property Response errors keyed by type.
ResultSet::$keyMatches protected property Key matches.
ResultSet::$lastResult protected property Last result.
ResultSet::$oneBoxResultSets protected property One box result keyed by module name.
ResultSet::$query protected property Original query object.
ResultSet::$results protected property Result objects.
ResultSet::$resultsPerPage protected property Results per page.
ResultSet::$searchTitle protected property Display title.
ResultSet::$spellingSuggestions protected property Spelling suggestions.
ResultSet::$synonyms protected property Synonyms.
ResultSet::$total protected property Total results.
ResultSet::addError public function Adds error.
ResultSet::addKeyMatch public function Adds key match.
ResultSet::addOneBoxResultSet public function Adds one box result set.
ResultSet::addResult public function Adds a result.
ResultSet::addSpellingSuggestion public function Adds spelling suggestion.
ResultSet::addSynonym public function Adds synonym.
ResultSet::ERROR_HTTP constant
ResultSet::ERROR_NO_RESULTS constant
ResultSet::ERROR_PARSING constant
ResultSet::getErrors public function Gets value of errors.
ResultSet::getKeyMatch public function Gets value of keyMatches.
ResultSet::getLastResult public function Gets value of lastResult.
ResultSet::getPager public function Returns render array for pager.
ResultSet::getQuery public function Gets value of query.
ResultSet::getResults public function Gets value of results.
ResultSet::getSearchTitle public function Gets value of searchTitle.
ResultSet::getSortLinks public function Returns an array of sort links.
ResultSet::getSpellingSuggestions public function Gets value of spellingSuggestions.
ResultSet::getStatistics public function Gets statistics information for search result.
ResultSet::getSynonyms public function Gets value of synonyms.
ResultSet::getTotal public function Gets value of total.
ResultSet::hasErrors public function Check if response is in error.
ResultSet::hasKeyMatch public function Check if there are key matches.
ResultSet::hasResults public function Check if there are results.
ResultSet::hasSpellingSuggestions public function Check if there are suggestions.
ResultSet::hasSynonyms public function Check if there are synonyms.
ResultSet::setLastResult public function Sets last result.
ResultSet::setQuery public function Sets query.
ResultSet::setResultsPerPage public function Sets results per page.
ResultSet::setSearchTitle public function Sets searchTitle.
ResultSet::setTotal public function Sets total.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.