Pimcore\Console\Command\SearchBackendReindexCommand::execute PHP Method

execute() protected method

protected execute ( Symfony\Component\Console\Input\InputInterface $input, Symfony\Component\Console\Output\OutputInterface $output )
$input Symfony\Component\Console\Input\InputInterface
$output Symfony\Component\Console\Output\OutputInterface
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        // clear all data
        $db = \Pimcore\Db::get();
        $db->query("TRUNCATE `search_backend_data`;");
        $elementsPerLoop = 100;
        $types = ["asset", "document", "object"];
        foreach ($types as $type) {
            $listClassName = "\\Pimcore\\Model\\" . ucfirst($type) . "\\Listing";
            $list = new $listClassName();
            if (method_exists($list, "setUnpublished")) {
                $list->setUnpublished(true);
            }
            $elementsTotal = $list->getTotalCount();
            for ($i = 0; $i < ceil($elementsTotal / $elementsPerLoop); $i++) {
                $list->setLimit($elementsPerLoop);
                $list->setOffset($i * $elementsPerLoop);
                $this->output->writeln("Processing " . $type . ": " . ($list->getOffset() + $elementsPerLoop) . "/" . $elementsTotal);
                $elements = $list->load();
                foreach ($elements as $element) {
                    try {
                        $searchEntry = Search\Backend\Data::getForElement($element);
                        if ($searchEntry instanceof Search\Backend\Data and $searchEntry->getId() instanceof Search\Backend\Data\Id) {
                            $searchEntry->setDataFromElement($element);
                        } else {
                            $searchEntry = new Search\Backend\Data($element);
                        }
                        $searchEntry->save();
                    } catch (\Exception $e) {
                        Logger::err($e);
                    }
                }
                \Pimcore::collectGarbage();
            }
        }
        $db->query("OPTIMIZE TABLE search_backend_data;");
    }
SearchBackendReindexCommand