protected function updateAnalyzers(Index $index, string $name)
    {
        $settings = $index->getSettings();
        $customAnalyzers = $this->getCustomAnalyzers();
        $missingAnalyzers = [];

        try {
            foreach ($customAnalyzers as $id => $analyzer) {
                if (!$settings->get("analysis.analyzer.{$id}")) {
                    $missingAnalyzers[$id] = $analyzer;
                }
            }
        } catch (\Throwable $e) {
            $this->logger->error(
                sprintf('Unable to read index [%s] and get analyzer settings.', $name),
                ['exception' => $e, 'index_name' => $name]
            );

            return;
        }

        if (empty($missingAnalyzers)) {
            $this->logger->info(
                sprintf(
                    'Index [%s] has all custom analyzers [%s].',
                    $name,
                    implode(',', array_keys($customAnalyzers))
                )
            );

            return;
        }

        $this->logger->warning(
            sprintf(
                'Closing index [%s] to add custom analyzers [%s].',
                $name,
                implode(',', array_keys($missingAnalyzers))
            )
        );

        try {
            $index->close();
            $index->setSettings(['analysis' => ['analyzer' => $customAnalyzers]]);
            $index->open();
        } catch (\Throwable $e) {
            $this->logger->error(
                sprintf('Unable to close index [%s] and update analyzer settings.', $name),
                ['exception' => $e, 'index_name' => $name]
            );

            return;
        }

        $this->logger->info(sprintf('Successfully added missing analyzers to index [%s]', $name));
    }
protected function updateAnalyzers(Index $index, string $name)
    {
        $settings = $index->getSettings();
        $customAnalyzers = $this->getCustomAnalyzers();
        $missingAnalyzers = [];

        try {
            foreach ($customAnalyzers as $id => $analyzer) {
                if (!$settings->get("analysis.analyzer.{$id}")) {
                    $missingAnalyzers[$id] = $analyzer;
                }
            }
        } catch (\Throwable $e) {
            $this->logger->error(
                sprintf('Unable to read index [%s] and get analyzer settings.', $name),
                ['exception' => $e, 'index_name' => $name]
            );

            return;
        }

        if (empty($missingAnalyzers)) {
            $this->logger->info(
                sprintf(
                    'Index [%s] has all custom analyzers [%s].',
                    $name,
                    implode(',', array_keys($customAnalyzers))
                )
            );

            return;
        }

        $this->logger->warning(
            sprintf(
                'Closing index [%s] to add custom analyzers [%s].',
                $name,
                implode(',', array_keys($missingAnalyzers))
            )
        );

        try {
            $index->close();
            $index->setSettings(['analysis' => ['analyzer' => $customAnalyzers]]);
            $index->open();
        } catch (\Throwable $e) {
            $this->logger->error(
                sprintf('Unable to close index [%s] and update analyzer settings.', $name),
                ['exception' => $e, 'index_name' => $name]
            );

            return;
        }

        $this->logger->info(sprintf('Successfully added missing analyzers to index [%s]', $name));
    }
protected function updateAnalyzers ( Index $ index , string $ name ) { $ settings = $ index -> getSettings ( ) ; $ customAnalyzers = $ this -> getCustomAnalyzers ( ) ; $ missingAnalyzers = [ ] ; try { foreach ( $ customAnalyzers as $ id => $ analyzer ) { if ( ! $ settings -> get ( "analysis.analyzer.{$id}" ) ) { $ missingAnalyzers [ $ id ] = $ analyzer ; } } } catch ( \ Throwable $ e ) { $ this -> logger -> error ( sprintf ( 'Unable to read index [%s] and get analyzer settings.' , $ name ) , [ 'exception' => $ e , 'index_name' => $ name ] ) ; return ; } if ( empty ( $ missingAnalyzers ) ) { $ this -> logger -> info ( sprintf ( 'Index [%s] has all custom analyzers [%s].' , $ name , implode ( ',' , array_keys ( $ customAnalyzers ) ) ) ) ; return ; } $ this -> logger -> warning ( sprintf ( 'Closing index [%s] to add custom analyzers [%s].' , $ name , implode ( ',' , array_keys ( $ missingAnalyzers ) ) ) ) ; try { $ index -> close ( ) ; $ index -> setSettings ( [ 'analysis' => [ 'analyzer' => $ customAnalyzers ] ] ) ; $ index -> open ( ) ; } catch ( \ Throwable $ e ) { $ this -> logger -> error ( sprintf ( 'Unable to close index [%s] and update analyzer settings.' , $ name ) , [ 'exception' => $ e , 'index_name' => $ name ] ) ; return ; } $ this -> logger -> info ( sprintf ( 'Successfully added missing analyzers to index [%s]' , $ name ) ) ; }
Checks if an existing index is missing any custom analyzers
and if it is, updates settings to include them.

@param Index  $index
@param string $name
Checks if an existing index is missing any custom analyzers and if it is updates settings to include them .