You are here

protected function SearchBuilder::flattenKeys in Elasticsearch Connector 8.2

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

Return a full text search query.

TODO: better handling of parse modes.

Parameters

array $keys:

string $parse_mode:

array $full_text_fields:

Return value

string

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

File

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

Class

SearchBuilder
Class SearchBuilder.

Namespace

Drupal\elasticsearch_connector\ElasticSearch\Parameters\Builder

Code

protected function flattenKeys(array $keys, $parse_mode = '', $full_text_fields = []) {
  $conjunction = isset($keys['#conjunction']) ? $keys['#conjunction'] : 'AND';
  $negation = !empty($keys['#negation']);
  $values = [];
  foreach ($keys as $key_nr => $key) {

    // We cannot use \Drupal\Core\Render\Element::children() anymore because
    // $keys is not a valid render array.
    if ($key_nr[0] === '#' || !$key) {
      continue;
    }
    if (is_array($key)) {
      $values[] = $this
        ->flattenKeys($key);
    }
    elseif (is_string($key)) {

      // If parse mode is not "direct": quote the keyword.
      if ($parse_mode
        ->getPluginId() !== 'direct') {
        $key = '"' . $key . '"';
      }
      $values[] = $key;
    }
  }
  if (!empty($values)) {
    return ($negation === TRUE ? 'NOT ' : '') . '(' . implode(" {$conjunction} ", $values) . ')';
  }
  else {
    return '';
  }
}