You are here

protected function SearchApiElasticsearchBackend::getSearchQueryOptions in Elasticsearch Connector 8

Helper function return associative array with query options.

1 call to SearchApiElasticsearchBackend::getSearchQueryOptions()
SearchApiElasticsearchBackend::buildSearchQuery in src/Plugin/search_api/backend/SearchApiElasticsearchBackend.php
Helper function build search query().

File

src/Plugin/search_api/backend/SearchApiElasticsearchBackend.php, line 1084
Contains the SearchApiElasticsearchBackend object.

Class

SearchApiElasticsearchBackend
Plugin annotation @SearchApiBackend( id = "elasticsearch", label = @Translation("Elasticsearch"), description = @Translation("Index items using an Elasticsearch server.") )

Namespace

Drupal\elasticsearch_connector\Plugin\search_api\backend

Code

protected function getSearchQueryOptions(QueryInterface $query) {

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

  // Index fields.
  $index_fields = $this
    ->getIndexFields($query);

  // 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 = $query
    ->getKeys();
  if (!empty($keys)) {
    if (is_string($keys)) {
      $keys = array(
        $keys,
      );
    }

    // Full text fields in which to perform the search.
    $query_full_text_fields = $query
      ->getFields();

    // Query string.
    $search_string = $this
      ->flattenKeys($keys, $query_options['parse mode']);
    if (!empty($search_string)) {
      $query_search_string = array(
        'query_string' => array(),
      );
      $query_search_string['query_string']['query'] = $search_string;
      $query_search_string['query_string']['fields'] = array_values($query_full_text_fields);
      $query_search_string['query_string']['analyzer'] = 'snowball';
    }
  }
  $sort = NULL;

  // Sort.
  try {

    // TODO: Why we are calling SolrSearchQuery?
    $sort = $this
      ->getSortSearchQuery($query);
  } catch (\Exception $e) {

    // watchdog_exception('Elasticsearch Search API', String::checkPlain($e->getMessage()), array(), WATCHDOG_ERROR);
    drupal_set_message($e
      ->getMessage(), 'error');
  }

  // Filters.
  $parsed_query_filters = $this
    ->parseFilter($query
    ->getFilter(), $index_fields);
  if (!empty($parsed_query_filters)) {
    $query_search_filter = $parsed_query_filters[0];
  }

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