You are here

protected function SearchBuilder::getQueryFilters in Elasticsearch Connector 8.6

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

Recursively parse Search API condition group.

Parameters

\Drupal\search_api\Query\ConditionGroupInterface $condition_group: The condition group object that holds all conditions that should be expressed as filters.

\Drupal\search_api\Item\FieldInterface[] $index_fields: An array of all indexed fields for the index, keyed by field identifier.

Return value

array Array of filter parameters to apply to query based on the given Search API condition group.

Throws

\Exception

1 call to SearchBuilder::getQueryFilters()
SearchBuilder::getSearchQueryOptions in src/ElasticSearch/Parameters/Builder/SearchBuilder.php
Helper function to return associative array with query options.

File

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

Class

SearchBuilder
Class SearchBuilder.

Namespace

Drupal\elasticsearch_connector\ElasticSearch\Parameters\Builder

Code

protected function getQueryFilters(ConditionGroupInterface $condition_group, array $index_fields) {
  $filters = [];
  $backend_fields = [
    '_language' => TRUE,
  ];
  if (!empty($condition_group)) {
    $conjunction = $condition_group
      ->getConjunction();
    foreach ($condition_group
      ->getConditions() as $condition) {
      $filter = NULL;

      // Simple filter [field_id, value, operator].
      if ($condition instanceof Condition) {
        if (!$condition
          ->getField() || !$condition
          ->getValue() || !$condition
          ->getOperator()) {

          // TODO: When using views the sort field is coming as a filter and
          // messing with this section.
          // throw new Exception(t('Incorrect filter criteria is using for searching!'));
        }
        $field_id = $condition
          ->getField();
        if (!isset($index_fields[$field_id]) && !isset($backend_fields[$field_id])) {

          // TODO: proper exception.
          throw new \Exception(t(':field_id Undefined field ! Incorrect filter criteria is using for searching!', [
            ':field_id' => $field_id,
          ]));
        }

        // Check operator.
        if (!$condition
          ->getOperator()) {

          // TODO: proper exception.
          throw new \Exception(t('Empty filter operator for :field_id field! Incorrect filter criteria is using for searching!', [
            ':field_id' => $field_id,
          ]));
        }

        // For some data type, we need to do conversions here.
        if (isset($index_fields[$field_id])) {
          $field = $index_fields[$field_id];
          switch ($field
            ->getType()) {
            case 'boolean':
              $condition
                ->setValue((bool) $condition
                ->getValue());
              break;
          }
        }

        // Check field.
        $filter = FilterFactory::filterFromCondition($condition);
        if (!empty($filter)) {
          $filters[] = $filter;
        }
      }
      elseif ($condition instanceof ConditionGroupInterface) {
        $nested_filters = $this
          ->getQueryFilters($condition, $index_fields);
        if (!empty($nested_filters)) {
          $filters[] = $nested_filters;
        }
      }
    }
    $filters = $this
      ->setFiltersConjunction($filters, $conjunction);
  }
  return $filters;
}