toArray() public method

Converts the mapping to an array.
public toArray ( ) : array
return array Mapping as array
Example #1
0
 public function testEnableTtl()
 {
     $client = $this->_getClient();
     $index = $client->getIndex('test');
     $index->create(array(), true);
     $type = $index->getType('test');
     $mapping = new Mapping($type, array());
     $mapping->enableTtl();
     $data = $mapping->toArray();
     $this->assertTrue($data[$type->getName()]['_ttl']['enabled']);
 }
 /**
  * Creates the elastic search mapping for the provided table, or just prints it out
  * to the screen if the `dry-run` option is provided.
  *
  * @param string $table The table name to inspect and create a mapping for
  * @return bool
  */
 public function main($table)
 {
     $table = TableRegistry::get($table);
     $schema = $table->schema();
     $mapping = ['@timestamp' => ['type' => 'date', 'format' => 'basic_t_time_no_millis||dateOptionalTime||basic_date_time||ordinal_date_time_no_millis||yyyy-MM-dd HH:mm:ss'], 'transaction' => ['type' => 'string', 'index' => 'not_analyzed'], 'type' => ['type' => 'string', 'index' => 'not_analyzed'], 'primary_key' => ['type' => 'string', 'index' => 'not_analyzed'], 'source' => ['type' => 'string', 'index' => 'not_analyzed'], 'parent_source' => ['type' => 'string', 'index' => 'not_analyzed'], 'original' => ['properties' => []], 'changed' => ['properties' => []], 'meta' => ['properties' => ['ip' => ['type' => 'string', 'index' => 'not_analyzed'], 'url' => ['type' => 'string', 'index' => 'not_analyzed'], 'user' => ['type' => 'string', 'index' => 'not_analyzed'], 'app_name' => ['type' => 'string', 'index' => 'not_analyzed']]]];
     $properties = [];
     foreach ($schema->columns() as $column) {
         $properties[$column] = $this->mapType($schema, $column);
     }
     if ($table->hasBehavior('AuditLog')) {
         $whitelist = (array) $table->behaviors()->AuditLog->config('whitelist');
         $blacklist = (array) $table->behaviors()->AuditLog->config('blacklist');
         $properties = empty($whitelist) ? $properties : array_intersect_key($properties, array_flip($whitelist));
         $properties = array_diff_key($properties, array_flip($blacklist));
     }
     $mapping['original']['properties'] = $mapping['changed']['properties'] = $properties;
     $client = ConnectionManager::get('auditlog_elastic');
     $index = $client->getIndex(sprintf($client->getConfig('index'), '-' . gmdate('Y.m.d')));
     $type = $index->getType($table->table());
     $elasticMapping = new ElasticaMapping();
     $elasticMapping->setType($type);
     $elasticMapping->setProperties($mapping);
     if ($this->params['dry-run']) {
         $this->out(json_encode($elasticMapping->toArray(), JSON_PRETTY_PRINT));
         return true;
     }
     if ($this->params['use-templates']) {
         $template = ['template' => sprintf($client->getConfig('index'), '*'), 'mappings' => $elasticMapping->toArray()];
         $response = $client->request('_template/template_' . $type->getName(), Request::PUT, $template);
         $this->out('<success>Successfully created the mapping template</success>');
         return $response->isOk();
     }
     if (!$index->exists()) {
         $index->create();
     }
     $elasticMapping->send();
     $this->out('<success>Successfully created the mapping</success>');
     return true;
 }
 /**
  * @return Status
  */
 public function validate()
 {
     $this->outputIndented("Validating mappings...");
     if ($this->optimizeIndexForExperimentalHighlighter && !in_array('experimental highlighter', $this->availablePlugins)) {
         $this->output("impossible!\n");
         return Status::newFatal(new RawMessage("wgCirrusSearchOptimizeIndexForExperimentalHighlighter is set to true but the " . "'experimental highlighter' plugin is not installed on all hosts."));
     }
     $requiredMappings = $this->mappingConfig;
     if (!$this->checkMapping($requiredMappings)) {
         /** @var Mapping[] $actions */
         $actions = array();
         // TODO Conflict resolution here might leave old portions of mappings
         foreach ($this->types as $typeName => $type) {
             $action = new Mapping($type);
             foreach ($requiredMappings[$typeName] as $key => $value) {
                 $action->setParam($key, $value);
             }
             $actions[] = $action;
         }
         try {
             // @todo Use $action->send(array('master_timeout' => ...))
             // after updating to version of Elastica library that supports it.
             // See https://github.com/ruflin/Elastica/pull/1004
             foreach ($actions as $action) {
                 $action->getType()->request('_mapping', Request::PUT, $action->toArray(), array('master_timeout' => $this->masterTimeout));
             }
             $this->output("corrected\n");
         } catch (ExceptionInterface $e) {
             $this->output("failed!\n");
             $message = ElasticsearchIntermediary::extractMessage($e);
             return Status::newFatal(new RawMessage("Couldn't update mappings.  Here is elasticsearch's error message: {$message}\n"));
         }
     }
     return Status::newGood();
 }