You are here

protected function SearchApiElasticsearchElastica::buildSearchQuery in Search API Elasticsearch 7

Helper function build search query().

2 calls to SearchApiElasticsearchElastica::buildSearchQuery()
SearchApiElasticsearchElastica::search in modules/elastica/includes/SearchApiElasticsearchElastica.inc
Overrides search().
SearchApiElasticsearchElastica::searchMultiple in modules/elastica/includes/SearchApiElasticsearchElastica.inc
Implements SearchApiMultiServiceInterface::searchMultiple().

File

modules/elastica/includes/SearchApiElasticsearchElastica.inc, line 633
Provides Elastica client for Search API Elasticsearch.

Class

SearchApiElasticsearchElastica
Search API Elasticsearch Elastica service class.

Code

protected function buildSearchQuery(SearchApiQueryInterface $query) {

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

  // Main query.
  $elastica_query = new Elastica\Query();
  $elastica_query
    ->setFrom($query_options['query_offset']);
  $elastica_query
    ->setSize($query_options['query_limit']);

  // Search API Location support.
  if (!empty($query_options['spatials'])) {
    foreach ($query_options['spatials'] as $i => $spatial) {
      if (empty($spatial['field']) || empty($spatial['lat']) || empty($spatial['lon'])) {
        continue;
      }
      $field = $spatial['field'];
      $point = array(
        'lat' => (double) $spatial['lat'],
        'lon' => (double) $spatial['lon'],
      );
      $radius = isset($spatial['radius']) ? (double) $spatial['radius'] : NULL;
      $query_options['query_search_filter'] = new Elastica\Filter\GeoDistance($field, $point, $radius);
    }
  }

  // Build the query.
  if (!empty($query_options['query_search_string']) && !empty($query_options['query_search_filter'])) {
    $elastica_query
      ->setQuery(new Elastica\Query\Filtered($query_options['query_search_string'], $query_options['query_search_filter']));
  }
  elseif (!empty($query_options['query_search_string'])) {
    $elastica_query
      ->setQuery($query_options['query_search_string']);
  }
  elseif (!empty($query_options['query_search_filter'])) {
    $elastica_query
      ->setPostFilter($query_options['query_search_filter']);
  }

  // Sort.
  if (!empty($query_options['sort'])) {
    $elastica_query
      ->setSort($query_options['sort']);
  }
  return $elastica_query;
}