You are here

protected function SearchApiDbService::prepareKeys in Search API Database Search 7

Removes nested expressions and phrase groupings from the search keys.

Used as a helper method in createDbQuery() and createFilterCondition().

Parameters

array|string|null $keys: The keys which should be preprocessed.

bool $tokenizer_active: (optional) TRUE if we can rely on the "Tokenizer" processor already having preprocessed the keywords.

Return value

array|string|null The preprocessed keys.

2 calls to SearchApiDbService::prepareKeys()
SearchApiDbService::createDbQuery in ./service.inc
Creates a database query for a search.
SearchApiDbService::createFilterCondition in ./service.inc
Creates a database query condition for a given search filter.

File

./service.inc, line 1354
Contains SearchApiDbService.

Class

SearchApiDbService
Indexes and searches items using the database.

Code

protected function prepareKeys($keys, $tokenizer_active = FALSE) {
  if (is_scalar($keys)) {
    $keys = $this
      ->splitKeys($keys, $tokenizer_active);
    return is_array($keys) ? $this
      ->eliminateDuplicates($keys) : $keys;
  }
  elseif (!$keys) {
    return NULL;
  }
  $keys = $this
    ->splitKeys($keys, $tokenizer_active);
  $keys = $this
    ->eliminateDuplicates($keys);
  $conj = $keys['#conjunction'];
  $neg = !empty($keys['#negation']);
  foreach ($keys as $i => &$nested) {
    if (is_array($nested)) {
      $nested = $this
        ->prepareKeys($nested, $tokenizer_active);
      if (is_array($nested) && $neg == !empty($nested['#negation'])) {
        if ($nested['#conjunction'] == $conj) {
          unset($nested['#conjunction'], $nested['#negation']);
          foreach ($nested as $renested) {
            $keys[] = $renested;
          }
          unset($keys[$i]);
        }
      }
    }
  }
  $keys = array_filter($keys);
  if (($count = count($keys)) <= 2) {
    if ($count < 2 || isset($keys['#negation'])) {
      $keys = NULL;
    }
    else {
      unset($keys['#conjunction']);
      $keys = reset($keys);
    }
  }
  return $keys;
}