You are here

protected function SearchApiElasticsearchQuery::build in Search API Elasticsearch 7.2

Build Elastica query for Elasticsearch.

Throws

Exception

1 call to SearchApiElasticsearchQuery::build()
SearchApiElasticsearchQuery::search in includes/SearchApiElasticsearchQuery.inc
Execute a search.

File

includes/SearchApiElasticsearchQuery.inc, line 56

Class

SearchApiElasticsearchQuery
@file

Code

protected function build() {
  $this->query = new Query();
  $offset = empty($this->query_options['offset']) ? 0 : $this->query_options['offset'];
  $limit = empty($this->query_options['limit']) ? 10 : $this->query_options['limit'];
  $keys = $this->search_api_query
    ->getKeys();
  if (!empty($keys)) {
    if (is_string($keys)) {
      $keys = array(
        $keys,
      );
    }
    $full_text_fields = $this->search_api_query
      ->getFields();
    $string = $this
      ->flattenKeys($keys, $this->query_options['parse mode']);
    if (!empty($string)) {
      $query_string = new QueryString($string);
      $query_string
        ->setFields(array_values($full_text_fields));
    }
  }
  $sort = $this
    ->getSortQuery();
  $parsed_filters = $this
    ->parseFilter($this->search_api_query
    ->getFilter());
  if (!empty($parsed_filters)) {
    $search_filter = $parsed_filters[0];
  }
  $this->query
    ->setFrom($offset);
  $this->query
    ->setSize($limit);
  if (isset($query_string) && isset($search_filter)) {
    $bool_query = new BoolQuery();
    $bool_query
      ->addMust($query_string);
    $bool_query
      ->addFilter($search_filter);
    $this->query
      ->setQuery($bool_query);
  }
  elseif (isset($query_string)) {
    $this->query
      ->setQuery($query_string);
  }
  elseif (isset($search_filter)) {
    $this->query
      ->setPostFilter($search_filter);
  }
  if (!empty($sort)) {
    $this->query
      ->setSort($sort);
  }
  $this
    ->addAggregations();
}