You are here

protected function SearchApiSolrBackend::doDocumentCounts in Search API Solr 4.x

Same name and namespace in other branches
  1. 8.3 src/Plugin/search_api/backend/SearchApiSolrBackend.php \Drupal\search_api_solr\Plugin\search_api\backend\SearchApiSolrBackend::doDocumentCounts()

Perform document count for a given endpoint, in total and per site / index.

Parameters

\Solarium\Core\Client\Endpoint $endpoint:

Return value

array An associative array of document counts.

Throws

\Drupal\Component\Plugin\Exception\PluginException

\Drupal\search_api\SearchApiException

\Drupal\search_api_solr\SearchApiSolrException

1 call to SearchApiSolrBackend::doDocumentCounts()
SearchApiSolrBackend::getDocumentCounts in src/Plugin/search_api/backend/SearchApiSolrBackend.php
Get document counts for this server, in total and per site / index.

File

src/Plugin/search_api/backend/SearchApiSolrBackend.php, line 4673

Class

SearchApiSolrBackend
Apache Solr backend for search api.

Namespace

Drupal\search_api_solr\Plugin\search_api\backend

Code

protected function doDocumentCounts(Endpoint $endpoint) : array {
  $connector = $this
    ->getSolrConnector();
  if (version_compare($connector
    ->getSolrVersion(), '5.0.0', '<')) {

    // The code below doesn't work in Solr below 5.x anyway.
    return [
      '#total' => 0,
    ];
  }
  try {
    $query = $connector
      ->getSelectQuery()
      ->addFilterQuery(new FilterQuery([
      'local_key' => 'search_api',
      'query' => '+hash:* +index_id:*',
    ]))
      ->setRows(1)
      ->setFields('id');
    $facet_set = $query
      ->getFacetSet();
    $json_facet_query = $facet_set
      ->createJsonFacetTerms([
      'local_key' => 'siteHashes',
      'limit' => -1,
      'field' => 'hash',
    ]);
    $nested_json_facet_terms = $facet_set
      ->createJsonFacetTerms([
      'local_key' => 'numDocsPerIndex',
      'limit' => -1,
      'field' => 'index_id',
    ], FALSE);
    $json_facet_query
      ->addFacet($nested_json_facet_terms);

    /** @var \Solarium\QueryType\Select\Result\Result $result */
    $result = $connector
      ->execute($query, $endpoint);
  } catch (\Exception $e) {

    // For non drupal indexes we only use the implicit "count" aggregation.
    // Therefore we need one random facet. The only field we can be 99% sure
    // that it exists in any index is _version_. max(_version_) should be the
    // most minimalistic facet we can think of.
    $query = $connector
      ->getSelectQuery()
      ->setRows(1);
    $facet_set = $query
      ->getFacetSet();
    $facet_set
      ->createJsonFacetAggregation([
      'local_key' => 'maxVersion',
      'function' => 'max(_version_)',
    ]);
    if (version_compare($connector
      ->getSolrVersion(), '8.1.0', '>=')) {

      // For whatever reason since Solr 8.1.0 the facet query above leads to
      // a NullPointerException in Solr itself if headers are omitted. But
      // omit headers is the default!
      // @todo track if this issue persists for later Solr versions, too.
      // @see https://issues.apache.org/jira/browse/SOLR-13509
      $query
        ->setOmitHeader(FALSE);
    }

    /** @var \Solarium\QueryType\Select\Result\Result $result */
    $result = $connector
      ->execute($query, $endpoint);
  }
  $facet_set = $result
    ->getFacetSet();

  // The implicit "count" aggregation over all results matching the query
  // exists only any JSONFacet set.

  /** @var \Solarium\Component\Result\Facet\Aggregation $count */
  $count = $facet_set
    ->getFacet('count');
  $document_counts = [
    '#total' => $count
      ->getValue(),
  ];

  /** @var \Solarium\Component\Result\Facet\Buckets $site_hashes */
  if ($site_hashes = $facet_set
    ->getFacet('siteHashes')) {

    /** @var \Solarium\Component\Result\Facet\Bucket $site_hash_bucket */
    foreach ($site_hashes
      ->getBuckets() as $site_hash_bucket) {
      $site_hash = $site_hash_bucket
        ->getValue();

      /** @var \Solarium\Component\Result\Facet\Bucket $index_bucket */
      foreach ($site_hash_bucket
        ->getFacetSet()
        ->getFacet('numDocsPerIndex') as $index_bucket) {
        $index = $index_bucket
          ->getValue();
        $document_counts[$site_hash][$index] = $index_bucket
          ->getCount();
      }
    }
  }
  return $document_counts;
}