You are here

public function SearchApiElasticsearchBackend::getBackendDefinedFields in Elasticsearch Connector 8.5

Provides information on additional fields made available by the backend.

If a backend indexes additional data with items and wants to make this available as fixed fields on the index (for example, to be used with Views), it can implement this method to facilitate this.

Fields returned here are expected to work correctly with this server when used in query conditions, sorts or similar places.

Parameters

\Drupal\search_api\IndexInterface $index: The index for which fields are being determined.

Return value

\Drupal\search_api\Item\FieldInterface[] An array of additional fields that are available for this index, keyed by their field IDs. The field IDs should always start with "search_api_" (avoiding the special field IDs defined by \Drupal\search_api\Query\QueryInterface::sort()) to avoid conflicts with user-defined fields.

Overrides BackendPluginBase::getBackendDefinedFields

File

src/Plugin/search_api/backend/SearchApiElasticsearchBackend.php, line 1147

Class

SearchApiElasticsearchBackend
Elasticsearch Search API Backend definition.

Namespace

Drupal\elasticsearch_connector\Plugin\search_api\backend

Code

public function getBackendDefinedFields(IndexInterface $index) {
  $backend_defined_fields = [];
  foreach ($index
    ->getFields() as $field) {
    if ($field
      ->getType() == 'location') {

      // Add a distance pseudo field to integrate with the search_api_location module.
      // See search_api_location_views_views_data_alter() for details.
      $distance_field_name = $field
        ->getFieldIdentifier() . '__distance';
      $property_path_name = $field
        ->getPropertyPath() . '__distance';
      $distance_field = new Field($index, $distance_field_name);
      $distance_field
        ->setLabel($field
        ->getLabel() . ' (distance)');
      $distance_field
        ->setDataDefinition(DataDefinition::create('decimal'));
      $distance_field
        ->setType('decimal');
      $distance_field
        ->setDatasourceId($field
        ->getDatasourceId());
      $distance_field
        ->setPropertyPath($property_path_name);
      $backend_defined_fields[$distance_field_name] = $distance_field;
    }
  }
  return $backend_defined_fields;
}