You are here

public function ElasticsearchViewsQuery::build in Elasticsearch Connector 8.2

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.5 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 274

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();
  if (isset($this->params['q']) && !empty($this->params['q'])) {

    // If we have more than one field we make a multi match query.
    if (count($this->params['fields']) > 1) {
      $params['query']['multi_match'] = array(
        'query' => $this->params['q'],
        'fields' => array_values($this->params['fulltext_fields']),
      );
    }
    else {
      $params['query']['match'] = array(
        reset($this->params['fulltext_fields']) => array(
          'query' => $this->params['q'],
          'operator' => $this->params['fulltext_operator'],
        ),
      );
    }
  }
  $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['fields'] = array();
  if (isset($this->params['fields'])) {
    $params['fields'] = array_merge($params['fields'], $this->params['fields']);
  }
  $where = $this->where;
  $params['filter'] = $this
    ->buildFilterArray($where);

  // Elastic complains when there is an empty filter array.
  if (empty($params['filter'])) {
    unset($params['filter']);
  }

  // If a filter and query is set, combine them into a filtered query.
  if (isset($params['filter']) && isset($params['query'])) {
    $temp = $params['query'];
    unset($params['query']);
    $params['query']['filtered'] = array(
      'query' => $temp,
      'filter' => $params['filter'],
    );
    unset($params['filter']);
  }

  // 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);
}