You are here

public function NumberFieldBoostTest::testNumberFieldBoost in Search API 8

Tests number field boost queries.

Throws

\Drupal\search_api\SearchApiException

File

tests/src/Kernel/Processor/NumberFieldBoostTest.php, line 127

Class

NumberFieldBoostTest
Tests the "Number field boost" processor.

Namespace

Drupal\Tests\search_api\Kernel\Processor

Code

public function testNumberFieldBoost() {
  $this
    ->indexItems();
  $this
    ->assertArrayHasKey('number_field_boost', $this->index
    ->getProcessors(), 'Number field boost processor is added.');
  $processor = $this->index
    ->getProcessor('number_field_boost');
  $result = $this
    ->getSearchResults();
  $this
    ->assertEquals([
    'entity:node/3:en',
    'entity:node/2:en',
    'entity:node/1:en',
  ], array_keys($result
    ->getResultItems()));
  $configuration = [
    'boosts' => [
      'field_boost' => [
        'boost_factor' => 1.0,
        'aggregation' => 'max',
      ],
    ],
  ];
  $processor
    ->setConfiguration($configuration);
  $this->index
    ->addProcessor($processor);
  $this->index
    ->save();
  $this
    ->indexItems();
  $result = $this
    ->getSearchResults();
  $this
    ->assertEquals([
    'entity:node/2:en',
    'entity:node/1:en',
    'entity:node/3:en',
  ], array_keys($result
    ->getResultItems()));
  $configuration = [
    'boosts' => [
      'field_boost' => [
        'boost_factor' => 1.0,
        'aggregation' => 'min',
      ],
    ],
  ];
  $processor
    ->setConfiguration($configuration);
  $this->index
    ->addProcessor($processor);
  $this->index
    ->save();
  $this
    ->indexItems();
  $result = $this
    ->getSearchResults();
  $this
    ->assertEquals([
    'entity:node/1:en',
    'entity:node/2:en',
    'entity:node/3:en',
  ], array_keys($result
    ->getResultItems()));
  $configuration = [
    'boosts' => [
      'field_boost' => [
        'boost_factor' => 1.0,
        'aggregation' => 'avg',
      ],
    ],
  ];
  $processor
    ->setConfiguration($configuration);
  $this->index
    ->addProcessor($processor);
  $this->index
    ->save();
  $this
    ->indexItems();
  $result = $this
    ->getSearchResults();
  $this
    ->assertEquals([
    'entity:node/1:en',
    'entity:node/2:en',
    'entity:node/3:en',
  ], array_keys($result
    ->getResultItems()));
  $configuration = [
    'boosts' => [
      'field_boost' => [
        'boost_factor' => 1.0,
        'aggregation' => 'sum',
      ],
    ],
  ];
  $processor
    ->setConfiguration($configuration);
  $this->index
    ->addProcessor($processor);
  $this->index
    ->save();
  $this
    ->indexItems();
  $result = $this
    ->getSearchResults();
  $this
    ->assertEquals([
    'entity:node/2:en',
    'entity:node/1:en',
    'entity:node/3:en',
  ], array_keys($result
    ->getResultItems()));
  $configuration = [
    'boosts' => [
      'field_boost' => [
        'boost_factor' => 1.0,
        'aggregation' => 'first',
      ],
    ],
  ];
  $processor
    ->setConfiguration($configuration);
  $this->index
    ->addProcessor($processor);
  $this->index
    ->save();
  $this
    ->indexItems();
  $result = $this
    ->getSearchResults();
  $this
    ->assertEquals([
    'entity:node/1:en',
    'entity:node/2:en',
    'entity:node/3:en',
  ], array_keys($result
    ->getResultItems()));
}