You are here

public function ElasticsearchViewsQuery::build in Elasticsearch Connector 8.5

Same name and namespace in other branches
  1. 8.7 modules/elasticsearch_connector_views/src/Plugin/views/query/ElasticsearchViewsQuery.php \Drupal\elasticsearch_connector_views\Plugin\views\query\ElasticsearchViewsQuery::build()
  2. 8.2 modules/elasticsearch_connector_views/src/Plugin/views/query/ElasticsearchViewsQuery.php \Drupal\elasticsearch_connector_views\Plugin\views\query\ElasticsearchViewsQuery::build()
  3. 8.6 modules/elasticsearch_connector_views/src/Plugin/views/query/ElasticsearchViewsQuery.php \Drupal\elasticsearch_connector_views\Plugin\views\query\ElasticsearchViewsQuery::build()

Builds the necessary info to execute the query.

Parameters

view $view: The view which is executed.

Overrides QueryPluginBase::build

File

modules/elasticsearch_connector_views/src/Plugin/views/query/ElasticsearchViewsQuery.php, line 301

Class

ElasticsearchViewsQuery
Defines a Views query class for searching on Search API indexes.

Namespace

Drupal\elasticsearch_connector_views\Plugin\views\query

Code

public function build(ViewExecutable $view) {
  $this->view = $view;

  // Store the view in the object to be able to use it later.
  $this->view = $view;
  $view
    ->initPager();

  // Let the pager modify the query to add limits.
  $view->pager
    ->query();
  if ($this
    ->shouldAbort()) {
    return;
  }

  // Set aliases of the fields.
  foreach ($view->field as $field_name => &$field) {
    $field->field_alias = $field_name;
    $field->aliases['entity_type'] = 'entity_type';
  }

  // Add fields to the query so they will be shown in solr document.
  $this->params['fields'] = array_keys($view->field);
  $this->params['fields'][] = '_source';
  $params = array();
  $params['size'] = $view->pager
    ->getItemsPerPage();
  $params['from'] = $view->pager
    ->getCurrentPage() * $view->pager
    ->getItemsPerPage();

  // If we display all items without pager remove the size limit to return
  // all documents from elasticsearch.
  if ($params['size'] == 0) {
    unset($params['size']);
  }

  // Add fields.
  // We are specifying which fields to be visible!
  $params['_source'] = array();
  if (isset($this->params['fields'])) {
    $params['_source'] = array_merge($params['_source'], $this->params['fields']);
  }
  if (!empty($this->where['conditions'])) {
    $params['query'] = [
      'bool' => [
        'must' => [
          'match' => $this->where['conditions'],
        ],
      ],
    ];
  }

  // Add sorting.
  if (!empty($this->sort_fields)) {
    $params['sort'] = $this
      ->buildSortArray();
  }
  $this->query_params = $params;

  // Export parameters for preview.
  $view->build_info['query'] = var_export($params, TRUE);
}