Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
33.33% covered (danger)
33.33%
1 / 3
CRAP
84.62% covered (warning)
84.62%
22 / 26
IndexCreator
0.00% covered (danger)
0.00%
0 / 1
33.33% covered (danger)
33.33%
1 / 3
8.23
84.62% covered (warning)
84.62%
22 / 26
 __construct
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
4 / 4
 createIndex
0.00% covered (danger)
0.00%
0 / 1
3.14
75.00% covered (warning)
75.00%
6 / 8
 buildArgs
0.00% covered (danger)
0.00%
0 / 1
4.05
85.71% covered (warning)
85.71%
12 / 14
<?php
namespace CirrusSearch\Maintenance;
use Elastica\Index;
use Status;
class IndexCreator {
    /**
     * @var Index
     */
    private $index;
    /**
     * @var array
     */
    private $analysisConfig;
    /**
     * @var array|null
     */
    private $similarityConfig;
    /**
     * @var array
     */
    private $mapping;
    /**
     * @param Index $index
     * @param array $analysisConfig
     * @param array|null $similarityConfig
     */
    public function __construct( Index $index, array $analysisConfig, array $similarityConfig = null ) {
        $this->index = $index;
        $this->analysisConfig = $analysisConfig;
        $this->similarityConfig = $similarityConfig;
    }
    /**
     * @param bool $rebuild
     * @param int $maxShardsPerNode
     * @param int $shardCount
     * @param string $replicaCount
     * @param int $refreshInterval
     * @param array $mergeSettings
     * @param bool $searchAllFields
     * @param array $extraSettings
     *
     * @return Status
     */
    public function createIndex(
        $rebuild,
        $maxShardsPerNode,
        $shardCount,
        $replicaCount,
        $refreshInterval,
        array $mergeSettings,
        $searchAllFields,
        array $extraSettings
    ) {
        $args = $this->buildArgs(
            $maxShardsPerNode,
            $shardCount,
            $replicaCount,
            $refreshInterval,
            $mergeSettings,
            $searchAllFields,
            $extraSettings
        );
        try {
            $response = $this->index->create( $args, $rebuild );
            if ( $response->hasError() === true ) {
                return Status::newFatal( $response->getError() );
            }
        } catch ( \Elastica\Exception\InvalidException | \Elastica\Exception\ResponseException $ex ) {
            return Status::newFatal( $ex->getMessage() );
        }
        return Status::newGood();
    }
    /**
     * @param int $maxShardsPerNode
     * @param int $shardCount
     * @param string $replicaCount
     * @param int $refreshInterval
     * @param array $mergeSettings
     * @param bool $searchAllFields
     * @param array $extraSettings
     *
     * @return array
     */
    private function buildArgs(
        $maxShardsPerNode,
        $shardCount,
        $replicaCount,
        $refreshInterval,
        array $mergeSettings,
        $searchAllFields,
        array $extraSettings
    ) {
        $indexSettings = [
            'number_of_shards' => $shardCount,
            'auto_expand_replicas' => $replicaCount,
            'refresh_interval' => $refreshInterval . 's',
            'analysis' => $this->analysisConfig,
            'routing' => [
                'allocation.total_shards_per_node' => $maxShardsPerNode,
            ]
        ];
        if ( $mergeSettings ) {
            $indexSettings['merge.policy'] = $mergeSettings;
        }
        $similarity = $this->similarityConfig;
        if ( $similarity ) {
            $indexSettings['similarity'] = $similarity;
        }
        if ( $searchAllFields ) {
            // Use our weighted all field as the default rather than _all which is disabled.
            $indexSettings['query.default_field'] = 'all';
        }
        // ideally we should merge $extraSettings to $indexSettings
        // but existing config might declare keys like "index.mapping.total_fields.limit"
        // which would not work under the 'index' key.
        $settings = [ 'index' => $indexSettings ] + $extraSettings;
        return [ 'settings' => $settings ];
    }
}