/** * 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(); }
public function setUp() { $client = new Elastica_Client(); $index = $client->getIndex('elastica_test_filter_nested'); $index->create(array(), true); $type = $index->getType('user'); $mapping = new Elastica_Type_Mapping(); $mapping->setProperties( array( 'firstname' => array('type' => 'string', 'store' => 'yes'), // default is store => no expected 'lastname' => array('type' => 'string'), 'hobbies' => array( 'type' => 'nested', 'include_in_parent' => true, 'properties' => array('hobby' => array('type' => 'string')) ) ) ); $type->setMapping($mapping); // Adds a list of documents with _bulk upload to the index $docs = array(); $docs[] = new Elastica_Document(1, array( 'firstname' => 'Nicolas', 'lastname' => 'Ruflin', 'hobbies' => array( array('hobby' => 'opensource') ) ) ); $docs[] = new Elastica_Document(2, array( 'firstname' => 'Nicolas', 'lastname' => 'Ippolito', 'hobbies' => array( array('hobby' => 'opensource'), array('hobby' => 'guitar'), ) ) ); $response = $type->addDocuments($docs); // Refresh index $index->refresh(); }
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(); } } }
/** * Creates a mapping object * * @param array|Elastica_Type_Mapping $mapping Mapping object or properties array * @return Elastica_Type_Mapping Mapping object * @throws Elastica_Exception_Invalid If invalid type */ public static function create($mapping) { if (is_array($mapping)) { $mappingObject = new Elastica_Type_Mapping(); $mappingObject->setProperties($mapping); } else { $mappingObject = $mapping; } if (!$mappingObject instanceof Elastica_Type_Mapping) { throw new Elastica_Exception_Invalid('Invalid object type'); } return $mappingObject; }
/** * 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; }