You are here

protected function SearchApiElasticsearchQuery::parseFilter in Search API Elasticsearch 7.2

Parse Search API filters.

Parameters

SearchApiQueryFilterInterface $query_filter:

string $field_type:

string $ignored_field:

Return value

array|null

Throws

Exception

2 calls to SearchApiElasticsearchQuery::parseFilter()
SearchApiElasticsearchQuery::build in includes/SearchApiElasticsearchQuery.inc
Build Elastica query for Elasticsearch.
SearchApiElasticsearchQuery::getAggregationFilter in includes/SearchApiElasticsearchQuery.inc
Helper method to get filters for aggregations.

File

includes/SearchApiElasticsearchQuery.inc, line 346

Class

SearchApiElasticsearchQuery
@file

Code

protected function parseFilter(SearchApiQueryFilterInterface $query_filter, $field_type = null, $ignored_field = null) {
  if (empty($query_filter) || empty($field_type)) {
    return null;
  }
  else {
    $conjunction = $query_filter
      ->getConjunction();
    $filters = [];
    foreach ($query_filter
      ->getFilters() as $filter_info) {
      $filter = null;

      // Simple filter [field_id, value, operator]
      if (is_array($filter_info)) {
        $filter_operator = str_replace('!=', '<>', $filter_info[2]);
        $filter_array = [
          'field_id' => $filter_info[0],
          'field_value' => $filter_info[1],
          'filter_operator' => $filter_operator,
        ];
        $this
          ->verifyFilterConfiguration($filter_array, $ignored_field);

        // Handle parse date filters.
        if ($field_type == 'date') {
          $filter_array['field_value'] = date(SEARCH_API_ELASTICSEARCH_DATE_FORMAT, $filter_array['field_value']);
        }
        $filter = $this
          ->getFilter($filter_array);
        if (!empty($filter)) {
          $filters[] = $filter;
        }
      }
      elseif ($filter_info instanceof SearchApiQueryFilterInterface) {
        $nested_filters = $this
          ->parseFilter($filter_info, $field_type);
        if (!empty($nested_filters)) {
          $filters = array_merge($filters, $nested_filters);
        }
      }
    }
    $this
      ->joinFilters($filters, $conjunction);
    return $filters;
  }
}