protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output)
 {
     $table = new Console\Helper\TableHelper();
     $indexArgument = $input->getArgument('index');
     $typeArgument = $input->getArgument('type');
     $dropIndexOption = $input->getOption('dropIndex');
     $dropTypeOption = $input->getOption('dropType');
     if ($indexArgument === 'all') {
         $indices = $this->indices;
     } else {
         $indices = explode(' ', $indexArgument);
     }
     $dropIndex = (bool) $dropIndexOption;
     $dropType = (bool) $dropTypeOption;
     if ($typeArgument === 'all') {
         $types = $this->types;
         $dropIndex = true;
     } else {
         $types = explode(' ', $typeArgument);
     }
     foreach ($indices as $indexName => $indexData) {
         dump($indexName, $indexData);
         exit;
         $index = $this->elastica->getIndex($indexName);
         if ($dropIndex === true) {
             try {
                 $output->writeln(sprintf('dropping index %s', $indexName));
                 //$response = $index->delete();
                 $output->writeln(sprintf('index %s dropped', $indexName));
             } catch (\Elastica_Exception_Response $e) {
                 $output->writeln(sprintf('index %s didnt exist', $indexName));
             }
             $output->writeln('');
             $output->writeln(sprintf('creating index %s', $indexName));
             $output->writeln('');
         }
         try {
             $index->create(['analysis' => ['analyzer' => ['default_index' => ['type' => 'custom', 'tokenizer' => 'lowercase', 'filter' => ['standard', 'lowercase', 'asciifolding', 'indexNGram', 'trim', 'unique', 'stopwordsFilter']], 'default_search' => ['type' => 'custom', 'tokenizer' => 'lowercase', 'filter' => ['standard', 'lowercase', 'asciifolding', 'searchNGram', 'trim', 'unique']]], 'filter' => ['indexNGram' => ["type" => "nGram", "min_gram" => 4, "max_gram" => 50], 'searchNGram' => ["type" => "nGram", "min_gram" => 4, "max_gram" => 50], 'stopwordsFilter' => ['type' => 'stop', 'stopwords' => $stopwords]]]], $dropIndex);
         } catch (\Elastica_Exception_Response $e) {
             $output->writeln(sprintf('<error>%s</error>', $e->getMessage()));
         }
         //}
         $typesToCreate = array_intersect($types, $this->indicesTypes[$indexName]);
         foreach ($typesToCreate as $typeName) {
             $output->writeln(sprintf('creating mapping for type: <info>%s</info> in index: <info>%s</info>', $typeName, $indexName));
             $output->writeln('');
             if (!isset($this->mappings[$typeName])) {
                 $output->writeln(sprintf('<error>Mappings for type: %s are not defined.</error>', $typeName));
                 $output->writeln('');
                 continue;
             }
             $elasticaType = $index->getType($typeName);
             if ($dropType === true) {
                 try {
                     $output->writeln(sprintf('deleting type <info>%s</info> from index: <info>%s</info>', $typeName, $indexName));
                     $elasticaType->delete();
                 } catch (\Elastica_Exception_Response $e) {
                     $output->writeln(sprintf('<error>%s</error>', $e->getMessage()));
                 }
                 $output->writeln('');
             }
             $mapping = new \Elastica_Type_Mapping();
             $mapping->setType($elasticaType);
             // Set mapping
             $mapping->setProperties($this->mappings[$typeName]);
             // Send mapping to type
             $mapping->send();
         }
     }
 }
Ejemplo n.º 2
0
 /**
  * Creates or updates Elasticsearch index.
  *
  * @link http://www.elasticsearch.org/guide/reference/mapping/core-types.html
  * @link http://www.elasticsearch.org/guide/reference/mapping/multi-field-type.html
  * @return JR_Search_Model_Resource_Engine_Elasticsearch_Client
  * @throws Exception
  */
 protected function _prepareIndex()
 {
     try {
         $indexSettings = $this->_getIndexSettings();
         $index = $this->getIndex($this->_index);
         if (!$index->exists()) {
             $indexSettings['number_of_shards'] = (int) $this->getConfig('number_of_shards');
             $index->create($indexSettings);
         } else {
             $index->setSettings($indexSettings);
         }
         $mapping = new Elastica_Type_Mapping();
         $mapping->setType($index->getType('product'));
         $mapping->setProperties($this->_getIndexProperties());
         $mapping->send();
     } catch (Exception $e) {
         Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
         throw $e;
     }
     return $this;
 }