You are here

public function NumberFieldBoost::preprocessIndexItems in Search API 8

Preprocesses search items for indexing.

Parameters

\Drupal\search_api\Item\ItemInterface[] $items: An array of items to be preprocessed for indexing.

Overrides ProcessorPluginBase::preprocessIndexItems

File

src/Plugin/search_api/processor/NumberFieldBoost.php, line 112

Class

NumberFieldBoost
Adds a boost based on a number field value.

Namespace

Drupal\search_api\Plugin\search_api\processor

Code

public function preprocessIndexItems(array $items) {
  $boosts = $this->configuration['boosts'];

  /** @var \Drupal\search_api\Item\ItemInterface $item */
  foreach ($items as $item) {
    foreach ($boosts as $field_id => $settings) {
      if ($field = $item
        ->getField($field_id)) {
        if ($values = $field
          ->getValues()) {
          switch ($settings['aggregation']) {
            case 'min':
              $value = min($values);
              break;
            case 'avg':
              $value = array_sum($values) / count($values);
              break;
            case 'sum':
              $value = array_sum($values);
              break;
            case 'mul':
              $value = array_product($values);
              break;
            case 'first':
              $value = reset($values);
              break;
            case 'max':
            default:
              $value = max($values);
              break;
          }
          if ($value) {
            $item
              ->setBoost($item
              ->getBoost() * (double) $value * (double) $settings['boost_factor']);
          }
        }
      }
    }
  }
}