diff --git a/modules/search_api_db/src/Plugin/search_api/backend/Database.php b/modules/search_api_db/src/Plugin/search_api/backend/Database.php index e68bf5d1..219bf0c2 100644 --- a/modules/search_api_db/src/Plugin/search_api/backend/Database.php +++ b/modules/search_api_db/src/Plugin/search_api/backend/Database.php @@ -751,8 +751,6 @@ protected function findFreeColumn($table, $column) { * (optional) The type of table being created. Either "index" (for the * denormalized table for an entire index) or "field" (for field-specific * tables). - * - * @todo Write a test to ensure a field named "value" doesn't break this. */ protected function createFieldTable(FieldInterface $field = NULL, array $db, $type = 'field') { $new_table = !$this->database->schema()->tableExists($db['table']); @@ -1954,7 +1952,7 @@ protected function createKeysQuery($keys, array $fields, array $all_fields, Inde } if (!$match_parts) { - $db_query->condition('word', $words, 'IN'); + $db_query->condition('t.word', $words, 'IN'); } else { $db_or = new Condition('OR'); @@ -1992,7 +1990,9 @@ protected function createKeysQuery($keys, array $fields, array $all_fields, Inde $db_query->condition($db_or); } - $db_query->condition('field_name', array_map([__CLASS__, 'getTextFieldName'], array_keys($fields)), 'IN'); + $field_names = array_keys($fields); + $field_names = array_map([__CLASS__, 'getTextFieldName'], $field_names); + $db_query->condition('t.field_name', $field_names, 'IN'); } if ($nested) { @@ -2003,7 +2003,7 @@ protected function createKeysQuery($keys, array $fields, array $all_fields, Inde if (!$match_parts) { $word .= ' '; $var = ':word' . strlen($word); - $query->addExpression($var, 'word', [$var => $word]); + $query->addExpression($var, 't.word', [$var => $word]); } else { $i += $word_count; @@ -2694,11 +2694,11 @@ public function getAutocompleteSuggestions(QueryInterface $query, SearchInterfac } $field_query = $this->database->select($fields[$field]['table'], 't'); $field_query->fields('t', ['word', 'item_id']) - ->condition('field_name', $field) - ->condition('item_id', $all_results, 'IN'); + ->condition('t.field_name', $field) + ->condition('t.item_id', $all_results, 'IN'); if ($pass == 1) { - $field_query->condition('word', $incomplete_like, 'LIKE') - ->condition('word', $keys, 'NOT IN'); + $field_query->condition('t.word', $incomplete_like, 'LIKE') + ->condition('t.word', $keys, 'NOT IN'); } if (!isset($word_query)) { $word_query = $field_query; @@ -2711,10 +2711,10 @@ public function getAutocompleteSuggestions(QueryInterface $query, SearchInterfac return []; } $db_query = $this->database->select($word_query, 't'); - $db_query->addExpression('COUNT(DISTINCT item_id)', 'results'); + $db_query->addExpression('COUNT(DISTINCT t.item_id)', 'results'); $db_query->fields('t', ['word']) - ->groupBy('word') - ->having('COUNT(DISTINCT item_id) <= :max', [':max' => $max_occurrences]) + ->groupBy('t.word') + ->having('COUNT(DISTINCT t.item_id) <= :max', [':max' => $max_occurrences]) ->orderBy('results', 'DESC') ->range(0, $limit); $incomp_len = strlen($incomplete_key); diff --git a/modules/search_api_db/tests/src/Kernel/BackendTest.php b/modules/search_api_db/tests/src/Kernel/BackendTest.php index c22a7f1f..a8e5fab0 100644 --- a/modules/search_api_db/tests/src/Kernel/BackendTest.php +++ b/modules/search_api_db/tests/src/Kernel/BackendTest.php @@ -68,6 +68,30 @@ public function setUp() { ]); $this->installConfig(['search_api_test_db']); + + // Add additional fields to the search index that have the same ID as + // column names used by this backend, to see whether this leads to any + // conflicts. + $index = $this->getIndex(); + $fields_helper = \Drupal::getContainer()->get('search_api.fields_helper'); + $column_names = [ + 'item_id', + 'field_name', + 'word', + 'score', + 'value', + ]; + $field_info = [ + 'datasource_id' => 'entity:entity_test_mulrev_changed', + 'property_path' => 'type', + 'type' => 'string', + ]; + foreach ($column_names as $column_name) { + $field_info['label'] = "Test field $column_name"; + $field = $fields_helper->createField($index, $column_name, $field_info); + $index->addField($field); + } + $index->save(); } /** @@ -111,13 +135,18 @@ protected function checkServerBackend() { 'body', 'category', 'created', + 'field_name', 'id', + 'item_id', 'keywords', 'name', + 'score', 'search_api_datasource', 'search_api_language', 'type', + 'value', 'width', + 'word', ]; $actual_fields = array_keys($field_infos); sort($actual_fields);