You are here

public function SearchApiSolrAnySchemaBackend::getSolrFieldNames in Search API Solr 8.2

Override the default fields that Search API Solr sets up. In particular, set the ID field to the one that is configured via the datasource config form.

Also, map the index's field names to the original property paths. Search API Solr adds prefixes to the paths because it assumes that it has done the indexing according to its schema.xml rules. Of course, in our case it hasn't and we need it to use the raw paths. Any field machine names that have been altered in the field list will have their mapping corrected by this step too.

Overrides SearchApiSolrBackend::getSolrFieldNames

See also

\Drupal\search_api_solr\Plugin\search_api\backend\SearchApiSolrBackend::getSolrFieldNames()

File

src/Plugin/search_api/backend/SearchApiSolrAnySchemaBackend.php, line 169

Class

SearchApiSolrAnySchemaBackend
A read-only backend for any non-drupal schema.

Namespace

Drupal\search_api_solr\Plugin\search_api\backend

Code

public function getSolrFieldNames(IndexInterface $index, $reset = FALSE) {

  // @todo The field name mapping should be cached per index because custom
  //   queries needs to access it on every query. But we need to be aware of
  //   datasource additions and deletions.
  if (!isset($this->fieldNames[$index
    ->id()]) || $reset) {
    parent::getSolrFieldNames($index, $reset);

    // Do not alter mappings if the index does not use the solr_document
    // datasource.
    $datasources = $index
      ->getDatasources();
    if (isset($datasources['solr_document'])) {

      // Set the ID field.
      $config = $index
        ->getDatasource('solr_document')
        ->getConfiguration();
      $this->fieldNames[$index
        ->id()]['search_api_id'] = $config['id_field'];
      $this->fieldNames[$index
        ->id()]['search_api_language'] = $config['language_field'];

      /** @var \Drupal\search_api\Item\FieldInterface[] $index_fields */
      $index_fields = $index
        ->getFields();

      // Re-map the indexed fields.
      foreach ($this->fieldNames[$index
        ->id()] as $raw => $name) {

        // Ignore the Search API fields.
        if (strpos($raw, 'search_api_') === 0 || empty($index_fields[$raw]) || $index_fields[$raw]
          ->getDatasourceId() !== 'solr_document') {
          continue;
        }
        $this->fieldNames[$index
          ->id()][$raw] = $index_fields[$raw]
          ->getPropertyPath();
      }
    }
  }

  // Let modules adjust the field mappings.
  $this->moduleHandler
    ->alter('search_api_solr_field_mapping', $index, $this->fieldNames[$index
    ->id()]);
  return $this->fieldNames[$index
    ->id()];
}