You are here

public function DevelController::entitySolr in Search API Solr 4.x

Same name and namespace in other branches
  1. 8.3 modules/search_api_solr_devel/src/Controller/DevelController.php \Drupal\search_api_solr_devel\Controller\DevelController::entitySolr()
  2. 8.2 search_api_solr_devel/src/Controller/DevelController.php \Drupal\search_api_solr_devel\Controller\DevelController::entitySolr()

Prints the document structure to be indexed by Solr.

Parameters

\Drupal\Core\Routing\RouteMatchInterface $route_match: A RouteMatch object.

Return value

array Array of page elements to render.

File

modules/search_api_solr_devel/src/Controller/DevelController.php, line 145

Class

DevelController
Returns responses for devel module routes.

Namespace

Drupal\search_api_solr_devel\Controller

Code

public function entitySolr(RouteMatchInterface $route_match) {
  $output_details = [];
  $table_rows = [];
  $num = 0;
  $parameter_name = $route_match
    ->getRouteObject()
    ->getOption('_devel_entity_type_id');
  $entity = $route_match
    ->getParameter($parameter_name);
  if ($entity && $entity instanceof EntityInterface) {
    foreach ($this
      ->getBackends() as $backend_id) {

      /** @var \Drupal\search_api\ServerInterface[] $servers */
      $servers = $this->storage
        ->loadByProperties([
        'backend' => $backend_id,
        'status' => TRUE,
      ]);
      foreach ($servers as $server) {

        /** @var \Drupal\search_api\ServerInterface $server */

        /** @var \Drupal\search_api_solr\SolrBackendInterface $backend */
        $backend = $server
          ->getBackend();

        /** @var \Drupal\search_api\IndexInterface[] $indexes */
        $indexes = $server
          ->getIndexes();
        $solr = $backend
          ->getSolrConnector();
        foreach ($indexes as $index) {
          if ($index
            ->status()) {
            foreach ($index
              ->getDatasourceIds() as $datasource_id) {
              list(, $entity_type) = Utility::splitPropertyPath($datasource_id);
              if ($entity
                ->getEntityTypeId() == $entity_type) {
                foreach (array_keys($entity
                  ->getTranslationLanguages()) as $langcode) {

                  // @todo improve that ID generation?
                  $item_id = $datasource_id . '/' . $entity
                    ->id() . ':' . $langcode;
                  $items = [];
                  $base_summary_row = $this
                    ->getBaseRow($server, $index, $datasource_id, $entity, $langcode, $item_id);

                  // @TODO: Run a timer on this process and report it?
                  $items[$item_id] = $this->fieldsHelper
                    ->createItemFromObject($index, $entity
                    ->getTranslation($langcode)
                    ->getTypedData(), $item_id);

                  // Alter and preprocess the items to indexed.
                  $index
                    ->alterIndexedItems($items);
                  $this->moduleHandler
                    ->alter('search_api_index_items', $index, $items);
                  $index
                    ->preprocessIndexItems($items);

                  // Gather documents generated by Search API.
                  $documents = $backend
                    ->getDocuments($index, $items);
                  foreach ($documents as $document) {
                    $summary_row = $base_summary_row;
                    $summary_row['num'] = $num + 1;
                    $fields = $document
                      ->getFields();
                    $summary_row['object_size'] = format_size(strlen(json_encode($fields)));
                    ksort($fields);
                    $details_id = $fields['id'];
                    $output_details[$details_id] = [
                      '#type' => 'details',
                      '#title' => $this
                        ->t('Row #@num: local and Solr indexing data', [
                        '@num' => $num + 1,
                      ]),
                    ];
                    $output_details[$details_id][] = [
                      '#markup' => '<h3>' . $this
                        ->t('Locally-generated data that would be sent to Solr during indexing:') . '</h3>',
                    ];
                    $output_details[$details_id][] = [
                      '#markup' => $this->develDumperManager
                        ->dumpOrExport($fields, $this
                        ->t('Locally-generated data'), TRUE),
                    ];

                    // Show current data for this item from the Solr backend.

                    /** @var \Drupal\search_api_solr\SolrConnectorInterface $solr */

                    /** @var \Solarium\QueryType\Select\Query\Query $query */
                    $output_details[$details_id][] = [
                      '#markup' => '<h3>' . $this
                        ->t('Current data in Solr for this item:') . '</h3>',
                    ];
                    $summary_row['solr_id'] = $fields['id'];
                    $query = $solr
                      ->getSelectQuery();
                    $query
                      ->setQuery('id:"' . $fields['id'] . '"');
                    $query
                      ->setFields('*');
                    try {

                      // @TODO: Run a timer on this process and report it?
                      $results = $solr
                        ->execute($query, $backend
                        ->getCollectionEndpoint($index));
                      $num_found = $results
                        ->getNumFound();
                      $summary_row['solr_exists'] = $this
                        ->t('yes');
                    } catch (\Exception $e) {
                      $results = [];
                      $num_found = -1;
                      $output_details[$details_id][] = [
                        '#markup' => $this
                          ->t('Error querying the Solr server!') . '<pre>' . $e
                          ->getMessage() . '</pre>',
                      ];
                      $summary_row['solr_exists'] = $this
                        ->t('error');
                    }

                    // If no item found in Solr, report it.
                    if ($num_found == 0) {
                      $summary_row['solr_exists'] = $this
                        ->t('no');
                      $output_details[$details_id][] = [
                        '#markup' => $this
                          ->t('No Solr document found with the ID %id', [
                          '%id' => $fields['id'],
                        ]),
                      ];
                    }
                    if ($num_found == 1) {

                      // Show Solr documents for this item.
                      $solr_documents = $results
                        ->getDocuments();
                      $fields = $solr_documents[0]
                        ->getFields();
                      $summary_row['solr_size'] = format_size(strlen(json_encode($fields)));
                      if (!empty($fields['timestamp'])) {
                        $summary_row['solr_changed'] = $this
                          ->showTimeAndTimeAgo(strtotime($fields['timestamp']));
                      }
                      ksort($fields);
                      $output_details[$details_id][] = [
                        '#markup' => $this->develDumperManager
                          ->dumpOrExport($fields, $this
                          ->t('Solr data'), TRUE),
                      ];
                    }
                    $table_rows[$num++] = $summary_row;
                  }
                }
              }
            }
          }
        }
      }
    }
  }

  // Message for no output.
  if (empty($output_details)) {
    return [
      '#markup' => $this
        ->t('No enabled indexes with a Solr backend that contain this item were found.'),
    ];
  }
  return array_merge([
    '#title' => $this
      ->t('Search API Solr devel status'),
    'header' => [
      '#markup' => $this
        ->t('This page shows Search API and Solr indexing data for the current entity.'),
    ],
    'summary_table' => [
      '#type' => 'table',
      '#prefix' => '<h3>' . $this
        ->t('Summary') . '</h3><p>' . $this
        ->t("Each row in the table represents an item generated from this entity according to the Search API index configuration (data sources, fields, processors, hook implementations, etc.). The first columns correspond to the Search API tracking information (kept in the site's database), and the rest of the columns show information about the corresponding Solr document for each item. See the <a href=\"https://www.drupal.org/docs/8/modules/search-api/developer-documentation\">developer documentation</a>.") . '</p>',
      '#header' => $this
        ->summaryTableHeader(),
      '#rows' => $table_rows,
    ],
  ], $output_details);
}