Commit d0ddc698 authored by Thomas Seidl's avatar Thomas Seidl
Browse files

Issue #3397017 by drunken monkey, gaddman: Fixed MySQL error in edge cases when indexing bigrams.

parent 45a96186
Loading
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
Search API 1.x, dev (xxxx-xx-xx):
---------------------------------
- #3397017 by drunken monkey, gaddman: Fixed MySQL error in edge cases when
  indexing bigrams.
- #3394738 by drunken monkey: Fixed fatal error after upgrade to 1.30.
- #3308184 by drunken monkey, admirlju: Fixed handling of incomplete field
  definitions when generating Views data.
+1 −4
Original line number Diff line number Diff line
@@ -47,10 +47,7 @@ class MySql extends GenericDatabase implements LocationAwareDatabaseInterface {
    // As MySQL removes trailing whitespace when computing primary keys, we need
    // to do the same or pseudo-duplicates could cause an exception ("Integrity
    // constraint violation: Duplicate entry") during indexing.
    if ($type !== 'text') {
      $value = rtrim($value);
    }
    return $value;
    return rtrim($value);
  }

  /**
+3 −2
Original line number Diff line number Diff line
@@ -1383,8 +1383,9 @@ class Database extends BackendPluginBase implements AutocompleteBackendInterface

      if ($this->configuration['phrase'] === 'bigram') {
        // Now add a bigram for this word and the last. In case this is the
        // first word, there is no bigram to add.
        if ($prev_word === NULL) {
        // first word, or the bigram wouldn't fit into the maximum token length,
        // there is no bigram to add.
        if ($prev_word === NULL || mb_strlen($prev_word) + 1 >= static::TOKEN_LENGTH_MAX) {
          continue;
        }

+29 −0
Original line number Diff line number Diff line
@@ -153,6 +153,7 @@ class BackendTest extends BackendTestBase {
    $this->regressionTest3225675();
    $this->regressionTest3258802();
    $this->regressionTest3227268();
    $this->regressionTest3397017();
  }

  /**
@@ -1046,6 +1047,34 @@ class BackendTest extends BackendTestBase {
    $this->assertEquals('utf8mb4_bin', $collations['word']);
  }

  /**
   * Tests that bigram indexing doesn't choke on 49-characters words.
   *
   * @see https://www.drupal.org/node/3397017
   */
  protected function regressionTest3397017(): void {
    // Index all items before adding a new one, so we can better predict the
    // expected count.
    $this->indexItems($this->indexId);

    $entity_id = count($this->entities) + 1;
    // @see \Drupal\search_api_db\Plugin\search_api\backend\Database::TOKEN_LENGTH_MAX
    $long_word = str_repeat('a', 49);
    $entity = $this->addTestEntity($entity_id, [
      'type' => 'article',
      'body' => "foo $long_word bar baz",
    ]);

    $count = $this->indexItems($this->indexId);
    $this->assertEquals(1, $count);
    $results = $this->buildSearch($long_word)
      ->execute();
    $this->assertResults([$entity_id], $results, 'String filter with trailing space');

    $entity->delete();
    unset($this->entities[$entity_id]);
  }

  /**
   * {@inheritdoc}
   */