You are here

public function SearchMatchTest::_testQueries in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/search/tests/src/Kernel/SearchMatchTest.php \Drupal\Tests\search\Kernel\SearchMatchTest::_testQueries()

Run predefine queries looking for indexed terms.

1 call to SearchMatchTest::_testQueries()
SearchMatchTest::testMatching in core/modules/search/tests/src/Kernel/SearchMatchTest.php
Tests search indexing.

File

core/modules/search/tests/src/Kernel/SearchMatchTest.php, line 107

Class

SearchMatchTest
Indexes content and queries it.

Namespace

Drupal\Tests\search\Kernel

Code

public function _testQueries() {

  // Note: OR queries that include short words in OR groups are only accepted
  // if the ORed terms are ANDed with at least one long word in the rest of
  // the query. Examples:
  // -  enim dolore OR ut = enim (dolore OR ut) = (enim dolor) OR (enim ut)
  // is good, and
  // -  dolore OR ut = (dolore) OR (ut)
  // is bad. This is a design limitation to avoid full table scans.
  $queries = [
    // Simple AND queries.
    'ipsum' => [
      1,
    ],
    'enim' => [
      4,
      5,
      6,
    ],
    'xxxxx' => [],
    'enim minim' => [
      5,
      6,
    ],
    'enim xxxxx' => [],
    'dolore eu' => [
      7,
    ],
    'dolore xx' => [],
    'ut minim' => [
      5,
    ],
    'xx minim' => [],
    'enim veniam am minim ut' => [
      5,
    ],
    // Simple OR and AND/OR queries.
    'dolore OR ipsum' => [
      1,
      2,
      7,
    ],
    'dolore OR xxxxx' => [
      2,
      7,
    ],
    'dolore OR ipsum OR enim' => [
      1,
      2,
      4,
      5,
      6,
      7,
    ],
    'ipsum OR dolore sit OR cillum' => [
      2,
      7,
    ],
    'minim dolore OR ipsum' => [
      7,
    ],
    'dolore OR ipsum veniam' => [
      7,
    ],
    'minim dolore OR ipsum OR enim' => [
      5,
      6,
      7,
    ],
    'dolore xx OR yy' => [],
    'xxxxx dolore OR ipsum' => [],
    // Sequence of OR queries.
    'minim' => [
      5,
      6,
      7,
    ],
    'minim OR xxxx' => [
      5,
      6,
      7,
    ],
    'minim OR xxxx OR minim' => [
      5,
      6,
      7,
    ],
    'minim OR xxxx minim' => [
      5,
      6,
      7,
    ],
    'minim OR xxxx minim OR yyyy' => [
      5,
      6,
      7,
    ],
    'minim OR xxxx minim OR cillum' => [
      6,
      7,
      5,
    ],
    'minim OR xxxx minim OR xxxx' => [
      5,
      6,
      7,
    ],
    // Negative queries.
    'dolore -sit' => [
      7,
    ],
    'dolore -eu' => [
      2,
    ],
    'dolore -xxxxx' => [
      2,
      7,
    ],
    'dolore -xx' => [
      2,
      7,
    ],
    // Phrase queries.
    '"dolore sit"' => [
      2,
    ],
    '"sit dolore"' => [],
    '"am minim veniam es"' => [
      6,
      7,
    ],
    '"minim am veniam es"' => [],
    // Mixed queries.
    '"am minim veniam es" OR dolore' => [
      2,
      6,
      7,
    ],
    '"minim am veniam es" OR "dolore sit"' => [
      2,
    ],
    '"minim am veniam es" OR "sit dolore"' => [],
    '"am minim veniam es" -eu' => [
      6,
    ],
    '"am minim veniam" -"cillum dolore"' => [
      5,
      6,
    ],
    '"am minim veniam" -"dolore cillum"' => [
      5,
      6,
      7,
    ],
    'xxxxx "minim am veniam es" OR dolore' => [],
    'xx "minim am veniam es" OR dolore' => [],
  ];
  $connection = Database::getConnection();
  foreach ($queries as $query => $results) {
    $result = $connection
      ->select('search_index', 'i')
      ->extend(SearchQuery::class)
      ->searchExpression($query, static::SEARCH_TYPE)
      ->execute();
    $set = $result ? $result
      ->fetchAll() : [];
    $this
      ->_testQueryMatching($query, $set, $results);
    $this
      ->_testQueryScores($query, $set, $results);
  }

  // These queries are run against the second index type, SEARCH_TYPE_2.
  $queries = [
    // Simple AND queries.
    'ipsum' => [],
    'enim' => [],
    'enim minim' => [],
    'dear' => [
      8,
    ],
    'germany' => [
      11,
      12,
    ],
  ];
  foreach ($queries as $query => $results) {
    $result = $connection
      ->select('search_index', 'i')
      ->extend(SearchQuery::class)
      ->searchExpression($query, static::SEARCH_TYPE_2)
      ->execute();
    $set = $result ? $result
      ->fetchAll() : [];
    $this
      ->_testQueryMatching($query, $set, $results);
    $this
      ->_testQueryScores($query, $set, $results);
  }

  // These queries are run against the third index type, SEARCH_TYPE_JPN.
  $queries = [
    // Simple AND queries.
    '呂波耳' => [
      13,
    ],
    '以呂波耳' => [
      13,
    ],
    'ほへと ヌルヲ' => [
      13,
    ],
    'とちリ' => [],
    'ドルーパル' => [
      14,
    ],
    'パルが大' => [
      14,
    ],
    'コーヒー' => [
      15,
    ],
    'ヒーキ' => [],
  ];
  foreach ($queries as $query => $results) {
    $result = $connection
      ->select('search_index', 'i')
      ->extend(SearchQuery::class)
      ->searchExpression($query, static::SEARCH_TYPE_JPN)
      ->execute();
    $set = $result ? $result
      ->fetchAll() : [];
    $this
      ->_testQueryMatching($query, $set, $results);
    $this
      ->_testQueryScores($query, $set, $results);
  }
}