You are here

protected function SearchBuilder::getSearchQueryOptions in Elasticsearch Connector 8.2

Same name and namespace in other branches
  1. 8.7 src/ElasticSearch/Parameters/Builder/SearchBuilder.php \Drupal\elasticsearch_connector\ElasticSearch\Parameters\Builder\SearchBuilder::getSearchQueryOptions()
  2. 8.5 src/ElasticSearch/Parameters/Builder/SearchBuilder.php \Drupal\elasticsearch_connector\ElasticSearch\Parameters\Builder\SearchBuilder::getSearchQueryOptions()
  3. 8.6 src/ElasticSearch/Parameters/Builder/SearchBuilder.php \Drupal\elasticsearch_connector\ElasticSearch\Parameters\Builder\SearchBuilder::getSearchQueryOptions()

Helper function return associative array with query options.

Return value

array

1 call to SearchBuilder::getSearchQueryOptions()
SearchBuilder::build in src/ElasticSearch/Parameters/Builder/SearchBuilder.php

File

src/ElasticSearch/Parameters/Builder/SearchBuilder.php, line 121

Class

SearchBuilder
Class SearchBuilder.

Namespace

Drupal\elasticsearch_connector\ElasticSearch\Parameters\Builder

Code

protected function getSearchQueryOptions() {

  // Query options.
  $query_options = $this->query
    ->getOptions();
  $parse_mode = $this->query
    ->getParseMode();

  // Index fields.
  $index_fields = $this->index
    ->getFields();

  // Range.
  $query_offset = empty($query_options['offset']) ? 0 : $query_options['offset'];
  $query_limit = empty($query_options['limit']) ? 10 : $query_options['limit'];

  // Query string.
  $query_search_string = NULL;

  // Query filter.
  $query_search_filter = NULL;

  // Full text search.
  $keys = $this->query
    ->getKeys();
  if (!empty($keys)) {
    if (is_string($keys)) {
      $keys = [
        $keys,
      ];
    }

    // Full text fields in which to perform the search.
    $query_full_text_fields = $this->index
      ->getFulltextFields();
    $query_fields = array();
    foreach ($query_full_text_fields as $full_text_field_name) {
      $full_text_field = $index_fields[$full_text_field_name];
      $query_fields[] = $full_text_field
        ->getFieldIdentifier() . '^' . $full_text_field
        ->getBoost();
    }

    // Query string.
    $search_string = $this
      ->flattenKeys($keys, $parse_mode);
    if (!empty($search_string)) {
      $query_search_string = [
        'query_string' => [],
      ];
      $query_search_string['query_string']['query'] = $search_string;
      $query_search_string['query_string']['fields'] = $query_fields;
      $query_search_string['query_string']['analyzer'] = 'snowball';
      $query_search_string['query_string']['default_operator'] = 'OR';
    }
  }
  $sort = NULL;

  // Sort.
  try {
    $sort = $this
      ->getSortSearchQuery();
  } catch (ElasticsearchException $e) {
    watchdog_exception('Elasticsearch Search API', $e);
    drupal_set_message($e
      ->getMessage(), 'error');
  }

  // Filters.
  try {
    $parsed_query_filters = $this
      ->getQueryFilters($this->query
      ->getConditionGroup(), $index_fields);
    if (!empty($parsed_query_filters)) {
      $query_search_filter = $parsed_query_filters[0];
    }
  } catch (ElasticsearchException $e) {
    watchdog_exception('Elasticsearch Search API', $e, Html::escape($e
      ->getMessage()));
    drupal_set_message(Html::escape($e
      ->getMessage()), 'error');
  }

  // More Like This.
  $mlt = [];
  if (isset($query_options['search_api_mlt'])) {
    $mlt = $query_options['search_api_mlt'];
  }
  return [
    'query_offset' => $query_offset,
    'query_limit' => $query_limit,
    'query_search_string' => $query_search_string,
    'query_search_filter' => $query_search_filter,
    'sort' => $sort,
    'mlt' => $mlt,
  ];
}