You are here

public function SearchApiSolrBackend::getSolrFieldNames in Search API Solr 8.2

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::getSolrFieldNames()
  2. 8 src/Plugin/search_api/backend/SearchApiSolrBackend.php \Drupal\search_api_solr\Plugin\search_api\backend\SearchApiSolrBackend::getSolrFieldNames()
  3. 4.x src/Plugin/search_api/backend/SearchApiSolrBackend.php \Drupal\search_api_solr\Plugin\search_api\backend\SearchApiSolrBackend::getSolrFieldNames()

Creates a list of all indexed field names mapped to their Solr field names.

The special fields "search_api_id" and "search_api_relevance" are also included. Any Solr fields that exist on search results are mapped back to to their local field names in the final result set.

Parameters

\Drupal\search_api\IndexInterface $index: The Search Api index.

bool $reset: (optional) Whether to reset the static cache.

Overrides SolrBackendInterface::getSolrFieldNames

See also

SearchApiSolrBackend::search()

12 calls to SearchApiSolrBackend::getSolrFieldNames()
AbstractSearchApiSolrMultilingualBackend::alterSolrDocuments in src/Plugin/search_api/backend/AbstractSearchApiSolrMultilingualBackend.php
Replaces language unspecific fulltext fields by language specific ones.
AbstractSearchApiSolrMultilingualBackend::alterSolrResponseBody in src/Plugin/search_api/backend/AbstractSearchApiSolrMultilingualBackend.php
@inheritdoc
AbstractSearchApiSolrMultilingualBackend::getAutocompleteFields in src/Plugin/search_api/backend/AbstractSearchApiSolrMultilingualBackend.php
Get the fields to search for autocomplete terms.
AbstractSearchApiSolrMultilingualBackend::preQuery in src/Plugin/search_api/backend/AbstractSearchApiSolrMultilingualBackend.php
Modify the query before it is sent to solr.
SearchApiSolrAnySchemaBackend::getSolrFieldNames in src/Plugin/search_api/backend/SearchApiSolrAnySchemaBackend.php
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.

... See full list

1 method overrides SearchApiSolrBackend::getSolrFieldNames()
SearchApiSolrAnySchemaBackend::getSolrFieldNames in src/Plugin/search_api/backend/SearchApiSolrAnySchemaBackend.php
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.

File

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

Class

SearchApiSolrBackend
Apache Solr backend for search api.

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) {

    // This array maps "local property name" => "solr doc property name".
    $ret = [
      'search_api_relevance' => 'score',
      'search_api_random' => 'random',
    ];

    // Add the names of any fields configured on the index.
    $fields = $index
      ->getFields();
    $fields += $this
      ->getSpecialFields($index);
    foreach ($fields as $key => $field) {
      if (empty($ret[$key])) {

        // Generate a field name; this corresponds with naming conventions in
        // our schema.xml.
        $type = $field
          ->getType();
        if ('solr_text_suggester' == $type) {

          // Any field of this type will be indexed in the same Solr field.
          // The 'twm_suggest' is the backend for the suggester component.
          $ret[$key] = 'twm_suggest';
          continue;
        }
        $type_info = Utility::getDataTypeInfo($type);
        $pref = isset($type_info['prefix']) ? $type_info['prefix'] : '';
        if (strpos($pref, 't') === 0) {

          // All text types need to be treated as multiple because some Search
          // API processors produce boosted string tokens for a single valued
          // drupal field. We need to store such tokens and their boost, too.
          $pref .= 'm';
        }
        else {
          if ($this->fieldsHelper
            ->isFieldIdReserved($key)) {
            $pref .= 's';
          }
          else {
            if ($field
              ->getDataDefinition()
              ->isList() || $this
              ->isHierarchicalField($field)) {
              $pref .= 'm';
            }
            else {
              try {
                $datasource = $field
                  ->getDatasource();
                if (!$datasource) {
                  throw new SearchApiException();
                }
                else {
                  $pref .= $this
                    ->getPropertyPathCardinality($field
                    ->getPropertyPath(), $datasource
                    ->getPropertyDefinitions()) != 1 ? 'm' : 's';
                }
              } catch (SearchApiException $e) {

                // Thrown by $field->getDatasource(). Assume multi value to be
                // safe.
                $pref .= 'm';
              }
            }
          }
        }
        $name = $pref . '_' . $key;
        $ret[$key] = Utility::encodeSolrName($name);

        // Add a distance pseudo field for any location field. These fields
        // don't really exist in the solr core, but we tell solr to name the
        // distance calculation results that way. Later we directly pass these
        // as "fields" to Drupal and especially Views.
        if ($type == 'location') {

          // Solr returns the calculated distance value as a single decimal
          // value (even for multi-valued location fields). Therefore we have
          // to prefix the field name accordingly by fts_*. This ensures that
          // this field works as for sorting, too. 'ft' is the prefix for
          // decimal (at the moment).
          $dist_info = Utility::getDataTypeInfo('decimal');
          $ret[$key . '__distance'] = Utility::encodeSolrName($dist_info['prefix'] . 's_' . $key . '__distance');
        }
      }
    }

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