You are here

protected function SearchApiSolrBackend::doGetMaxDocumentVersions 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::doGetMaxDocumentVersions()

Get the max document versions, in total and per site / index / datasource.

_version_ numbers are important for replication and checkpoints.

Parameters

\Solarium\Core\Client\Endpoint $endpoint:

Return value

array An associative array of max document versions.

Throws

\Drupal\Component\Plugin\Exception\PluginException

\Drupal\search_api\SearchApiException

\Drupal\search_api_solr\SearchApiSolrException

1 call to SearchApiSolrBackend::doGetMaxDocumentVersions()
SearchApiSolrBackend::getMaxDocumentVersions in src/Plugin/search_api/backend/SearchApiSolrBackend.php
Get the max document versions, in total and per site / index / datasource.

File

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

Class

SearchApiSolrBackend
Apache Solr backend for search api.

Namespace

Drupal\search_api_solr\Plugin\search_api\backend

Code

protected function doGetMaxDocumentVersions(Endpoint $endpoint) : array {
  $connector = $this
    ->getSolrConnector();
  $document_versions = [
    '#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();
    $facet_set
      ->createJsonFacetAggregation([
      'local_key' => 'maxVersion',
      'function' => 'max(_version_)',
    ]);
    $siteHashes = $facet_set
      ->createJsonFacetTerms([
      'local_key' => 'siteHashes',
      'limit' => -1,
      'field' => 'hash',
    ]);
    $indexes = $facet_set
      ->createJsonFacetTerms([
      'local_key' => 'indexes',
      'limit' => -1,
      'field' => 'index_id',
    ], FALSE);
    $dataSources = $facet_set
      ->createJsonFacetTerms([
      'local_key' => 'dataSources',
      'limit' => -1,
      'field' => 'ss_search_api_datasource',
    ], FALSE);
    $maxVersionPerDataSource = $facet_set
      ->createJsonFacetAggregation([
      'local_key' => 'maxVersionPerDataSource',
      'function' => 'max(_version_)',
    ], FALSE);
    $dataSources
      ->addFacet($maxVersionPerDataSource);
    $indexes
      ->addFacet($dataSources);
    $siteHashes
      ->addFacet($indexes);

    /** @var \Solarium\QueryType\Select\Result\Result $result */
    $result = $connector
      ->execute($query, $endpoint);
  } catch (\Exception $e) {
    $query = $connector
      ->getSelectQuery()
      ->setRows(1);
    $facet_set = $query
      ->getFacetSet();
    $facet_set
      ->createJsonFacetAggregation([
      'local_key' => 'maxVersion',
      'function' => 'max(_version_)',
    ]);

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

  /** @var \Solarium\Component\Result\Facet\Aggregation $maxVersion */
  if ($maxVersion = $facet_set
    ->getFacet('maxVersion')) {
    $document_versions = [
      '#total' => $maxVersion
        ->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('indexes') as $index_bucket) {
          $index = $index_bucket
            ->getValue();

          /** @var \Solarium\Component\Result\Facet\Bucket $datasource_bucket */
          if ($datsources_facet = $index_bucket
            ->getFacetSet()
            ->getFacet('dataSources')) {
            foreach ($datsources_facet as $datasource_bucket) {
              $datasource = $datasource_bucket
                ->getValue();

              /** @var \Solarium\Component\Result\Facet\Aggregation $maxVersionPerDataSource */
              if ($maxVersionPerDataSource = $datasource_bucket
                ->getFacetSet()
                ->getFacet('maxVersionPerDataSource')) {
                $document_versions[$site_hash][$index][$datasource] = $maxVersionPerDataSource
                  ->getValue();
              }
            }
          }
        }
      }
    }
  }
  return $document_versions;
}