public function testGetIdNoSource()
 {
     // Creates a new index 'xodoa' and a type 'user' inside this index
     $indexName = 'xodoa';
     $typeName = 'user';
     $client = new Elastica_Client();
     $index = $client->getIndex($indexName);
     $index->create(array(), true);
     $type = $index->getType($typeName);
     $mapping = new Elastica_Type_Mapping($type);
     $mapping->disableSource();
     $mapping->send();
     // Adds 1 document to the index
     $docId = 3;
     $doc1 = new Elastica_Document($docId, array('username' => 'hans'));
     $type->addDocument($doc1);
     // Refreshes index
     $index->refresh();
     sleep(2);
     $resultSet = $type->search('hans');
     $this->assertEquals(1, $resultSet->count());
     $result = $resultSet->current();
     $this->assertEquals(array(), $result->getSource());
     $this->assertInstanceOf('Elastica_Result', $result);
     $this->assertEquals($indexName, $result->getIndex());
     $this->assertEquals($typeName, $result->getType());
     $this->assertEquals($docId, $result->getId());
     $this->assertGreaterThan(0, $result->getScore());
     $this->assertInternalType('array', $result->getData());
 }
Esempio n. 2
0
 /**
  * Creates the index and sets the mapping for this type
  *
  * @param bool $recreate OPTIONAL Recreates the index if true (default = false)
  */
 public function create($recreate = false)
 {
     $this->getIndex()->create($this->_indexParams, $recreate);
     $mapping = new Elastica_Type_Mapping($this->getType());
     $mapping->setProperties($this->_mapping);
     $mapping->setSource(array('enabled' => $this->_source));
     $mapping->send();
 }
 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();
         }
     }
 }
Esempio n. 4
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;
 }