You are here

protected function SearchBuilder::getSearchQueryOptions in Elasticsearch Connector 8.5

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.2 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 to return associative array with query options.

Return value

array Associative array with the following keys:

  • query_offset: Pager offset.
  • query_limit: Number of items to return in the query.
  • query_search_string: Main full text query.
  • query_search_filter: Filters.
  • sort: Sort options.
  • mlt: More like this search options.
1 call to SearchBuilder::getSearchQueryOptions()
SearchBuilder::build in src/ElasticSearch/Parameters/Builder/SearchBuilder.php
Build up the body of the request to the Elasticsearch _search endpoint.

File

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

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();

  // Search API does not provide metadata for some special fields but might
  // try to query for them. Thus add the metadata so we allow for querying
  // them.
  if (empty($index_fields['search_api_datasource'])) {
    $index_fields['search_api_datasource'] = \Drupal::getContainer()
      ->get('search_api.fields_helper')
      ->createField($this->index, 'search_api_datasource', [
      'type' => 'string',
    ]);
  }

  // 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->query
      ->getFulltextFields();
    if ($query_full_text_fields) {

      // Make sure the fields exists within the indexed fields.
      $query_full_text_fields = array_intersect($this->index
        ->getFulltextFields(), $query_full_text_fields);
    }
    else {
      $query_full_text_fields = $this->index
        ->getFulltextFields();
    }
    $query_fields = [];
    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.
    $lucene = $this
      ->flattenKeys($keys, $parse_mode, $this->index
      ->getServerInstance()
      ->getBackend()
      ->getFuzziness());
    $search_string = $lucene
      ->__toString();
    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;
    }
  }
  $sort = NULL;

  // Sort.
  try {
    $sort = $this
      ->getSortSearchQuery();
  } catch (ElasticsearchException $e) {
    watchdog_exception('Elasticsearch Search API', $e);
    drupal_set_message($e
      ->getMessage(), 'error');
  }
  $languages = $this->query
    ->getLanguages();
  if ($languages !== NULL) {
    $this->query
      ->getConditionGroup()
      ->addCondition('_language', $languages, 'IN');
  }

  // Filters.
  try {
    $parsed_query_filters = $this
      ->getQueryFilters($this->query
      ->getConditionGroup(), $index_fields);
    if (!empty($parsed_query_filters)) {
      $query_search_filter = $parsed_query_filters;
    }
  } 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'];
  }
  $elasticSearchQuery = [
    'query_offset' => $query_offset,
    'query_limit' => $query_limit,
    'query_search_string' => $query_search_string,
    'query_search_filter' => $query_search_filter,
    'sort' => $sort,
    'mlt' => $mlt,
  ];

  // Allow other modules to alter index config before we create it.
  $indexName = IndexFactory::getIndexName($this->index);
  $dispatcher = \Drupal::service('event_dispatcher');
  $prepareSearchQueryEvent = new PrepareSearchQueryEvent($elasticSearchQuery, $indexName);
  $event = $dispatcher
    ->dispatch(PrepareSearchQueryEvent::PREPARE_QUERY, $prepareSearchQueryEvent);
  $elasticSearchQuery = $event
    ->getElasticSearchQuery();
  return $elasticSearchQuery;
}