You are here

public function SearchApiSolrTest::testNgramResult in Search API Solr 8.2

Same name and namespace in other branches
  1. 8 tests/src/Kernel/SearchApiSolrTest.php \Drupal\Tests\search_api_solr\Kernel\SearchApiSolrTest::testNgramResult()

Tests ngram search result.

File

tests/src/Kernel/SearchApiSolrTest.php, line 900

Class

SearchApiSolrTest
Tests index and search capabilities using the Solr search backend.

Namespace

Drupal\Tests\search_api_solr\Kernel

Code

public function testNgramResult() {
  $this
    ->addTestEntity(1, [
    'name' => 'Test Article 1',
    'body' => 'The test article number 1 about cats, dogs and trees.',
    'type' => 'article',
    'category' => 'dogs and trees',
  ]);

  // Add another node with body length equal to the limit.
  $this
    ->addTestEntity(2, [
    'name' => 'Test Article 1',
    'body' => 'The test article number 2 about a tree.',
    'type' => 'article',
    'category' => 'trees',
  ]);
  $this
    ->indexItems($this->indexId);
  $results = $this
    ->buildSearch([
    'tre',
  ], [], [
    'category_ngram',
  ])
    ->execute();
  $this
    ->assertResults([
    1,
    2,
  ], $results, 'Ngram text "tre".');
  $results = $this
    ->buildSearch([], [], [])
    ->addCondition('category_ngram_string', 'tre')
    ->execute();
  $this
    ->assertResults([
    2,
  ], $results, 'Ngram string "tre".');
  $results = $this
    ->buildSearch([
    'Dog',
  ], [], [
    'category_ngram',
  ])
    ->execute();
  $this
    ->assertResults([
    1,
  ], $results, 'Ngram text "Dog".');
  $results = $this
    ->buildSearch([], [], [])
    ->addCondition('category_ngram_string', 'Dog')
    ->execute();
  $this
    ->assertResults([
    1,
  ], $results, 'Ngram string "Dog".');
}