/** * @return \Elastica\Type\Mapping */ public function getElasticaMapping() { $mapping = new Mapping(); $mapping->setProperties($this->getElasticaFields()); $mapping->setParam('date_detection', false); return $mapping; }
/** * Add a mapping for the location of the photograph */ public function updateElasticsearchMapping(\Elastica\Type\Mapping $mapping) { // get the properties of the individual fields as an array $properties = $mapping->getProperties(); // enable tags to be faceted $properties['RawValue'] = array('type' => 'string', 'index' => 'not_analyzed'); // set the new properties on the mapping $mapping->setProperties($properties); return $mapping; }
protected function _getIndexForTest() { $index = $this->_createIndex(); $mapping = new Mapping(); $mapping->setProperties(array('comments' => array('type' => 'nested', 'properties' => array('name' => array('type' => 'string'), 'body' => array('type' => 'string'))))); $type = $index->getType('test'); $type->setMapping($mapping); $type->addDocuments(array(new Document(1, array('comments' => array(array('name' => 'bob', 'body' => 'this is bobs comment'), array('name' => 'john', 'body' => 'this is johns comment')), 'tags' => array('foo', 'bar'))), new Document(2, array('comments' => array(array('name' => 'bob', 'body' => 'this is another comment from bob'), array('name' => 'susan', 'body' => 'this is susans comment')), 'tags' => array('foo', 'baz'))))); $index->refresh(); return $index; }
protected function _getIndexForTest() { $index = $this->_createIndex('elastica_test_filter_nested'); $type = $index->getType('user'); $mapping = new Mapping(); $mapping->setProperties(array('firstname' => array('type' => 'string', 'store' => 'yes'), 'lastname' => array('type' => 'string'), 'hobbies' => array('type' => 'nested', 'include_in_parent' => true, 'properties' => array('hobby' => array('type' => 'string'))))); $type->setMapping($mapping); $response = $type->addDocuments(array(new Document(1, array('firstname' => 'Nicolas', 'lastname' => 'Ruflin', 'hobbies' => array(array('hobby' => 'opensource')))), new Document(2, array('firstname' => 'Nicolas', 'lastname' => 'Ippolito', 'hobbies' => array(array('hobby' => 'opensource'), array('hobby' => 'guitar')))))); $index->refresh(); return $index; }
public function postInstall(RecordInterface $record) { $client = new Client(array('host' => $this->registry['search.host'], 'port' => $this->registry['search.port'])); $index = $client->getIndex('amun'); $index->create(); $type = $index->getType('page'); $mapping = new Mapping(); $mapping->setType($type); $mapping->setProperties(array('id' => array('type' => 'string', 'include_in_all' => false), 'userId' => array('type' => 'integer', 'include_in_all' => false), 'path' => array('type' => 'string', 'include_in_all' => true), 'title' => array('type' => 'string', 'include_in_all' => true), 'content' => array('type' => 'string', 'include_in_all' => true), 'date' => array('type' => 'date', 'include_in_all' => false))); $mapping->send(); }
protected function setUp() { parent::setUp(); $this->_index = $this->_createIndex("geohash_grid"); $mapping = new Mapping(); $mapping->setProperties(array("location" => array("type" => "geo_point"))); $type = $this->_index->getType("test"); $type->setMapping($mapping); $docs = array(new Document("1", array("location" => array("lat" => 32.849437, "lon" => -117.271732))), new Document("2", array("location" => array("lat" => 32.79832, "lon" => -117.246648))), new Document("3", array("location" => array("lat" => 37.782439, "lon" => -122.39256)))); $type->addDocuments($docs); $this->_index->refresh(); }
protected function setUp() { parent::setUp(); $this->_index = $this->_createIndex("nested"); $mapping = new Mapping(); $mapping->setProperties(array("resellers" => array("type" => "nested", "properties" => array("name" => array("type" => "string"), "price" => array("type" => "double"))))); $type = $this->_index->getType("test"); $type->setMapping($mapping); $docs = array(new Document("1", array("resellers" => array("name" => "spacely sprockets", "price" => 5.55))), new Document("1", array("resellers" => array("name" => "cogswell cogs", "price" => 4.98)))); $type->addDocuments($docs); $this->_index->refresh(); }
protected function setUp() { parent::setUp(); $this->_index = $this->_createIndex("ip_range"); $mapping = new Mapping(); $mapping->setProperties(array("address" => array("type" => "ip"))); $type = $this->_index->getType("test"); $type->setMapping($mapping); $docs = array(new Document("1", array("address" => "192.168.1.100")), new Document("2", array("address" => "192.168.1.150")), new Document("3", array("address" => "192.168.1.200"))); $type->addDocuments($docs); $this->_index->refresh(); }
/** * @return Type */ private function getType() { if (null === $this->type) { $this->type = $this->getIndex()->getType($this->typeName); $mapping = new Type\Mapping(); $mapping->setType($this->type); $mapping->setTtl(['enabled' => true]); $mapping->setProperties([self::ID_FIELD => ['type' => 'string', 'include_in_all' => true], self::VALUE_FIELD => ['type' => 'string', 'include_in_all' => true]]); $mapping->send(); } return $this->type; }
protected function setUp() { parent::setUp(); $this->_index = $this->_createIndex("nested"); $mapping = new Mapping(); $mapping->setProperties(array("comments" => array("type" => "nested", "properties" => array("name" => array("type" => "string"), "body" => array("type" => "string"))))); $type = $this->_index->getType("test"); $type->setMapping($mapping); $docs = array(new Document("1", array("comments" => array(array("name" => "bob", "body" => "this is bobs comment"), array("name" => "john", "body" => "this is johns comment")), "tags" => array("foo", "bar"))), new Document("2", array("comments" => array(array("name" => "bob", "body" => "this is another comment from bob"), array("name" => "susan", "body" => "this is susans comment")), "tags" => array("foo", "baz")))); $type->addDocuments($docs); $this->_index->refresh(); }
protected function setUp() { parent::setUp(); $this->_index = $this->_createIndex("date_histogram"); $mapping = new Mapping(); $mapping->setProperties(array("created" => array("type" => "date"))); $type = $this->_index->getType("test"); $type->setMapping($mapping); $docs = array(new Document("1", array("created" => 1390962135000)), new Document("2", array("created" => 1390965735000)), new Document("3", array("created" => 1390954935000))); $type->addDocuments($docs); $this->_index->refresh(); }
/** * Creates a new Client of Elastica with the parameters $host and $port, And receive the index and type of stored data * * @param $user User Entity, $host string, $port string, $index string, $type string */ public function __construct(User $user, $host, $port, $index, $type) { $this->user = $user; $this->client = new Client(array('host' => $host, 'port' => $port)); $this->index = $this->client->getIndex($index); if (!$this->index->exists()) { $this->index->create(); $this->index->refresh(); } $this->type = $this->index->getType($type); $mapping = new Mapping(); $mapping->setType($this->type); $mapping->setProperties(array('name' => array('type' => 'string'), 'email' => array('type' => 'string'), 'password' => array('type' => 'string'))); }
/** * Add a mapping for the location of the photograph */ public function updateElasticsearchMapping(\Elastica\Type\Mapping $mapping) { // get the properties of the individual fields as an array $properties = $mapping->getProperties(); // add a location with geo point $precision1cm = array('format' => 'compressed', 'precision' => '1cm'); $properties['location'] = array('type' => 'geo_point', 'fielddata' => $precision1cm); $properties['ShutterSpeed'] = array('type' => 'string', 'index' => 'not_analyzed'); $properties['Aperture'] = array('type' => 'double'); // by default casted as a string, we want a date 2015-07-25 18:15:33 y-M-d H:m:s $properties['TakenAt'] = array('type' => 'date', 'format' => 'y-M-d H:m:s'); // set the new properties on the mapping $mapping->setProperties($properties); return $mapping; }
/** * Create the mapping for the type. * * @param \Cake\Datasource\ConnectionInterface $db The Elasticsearch connection * @return void */ public function create(ConnectionInterface $db) { if (empty($this->schema)) { return; } $index = $db->getIndex(); if (!$index->exists()) { $index->create(); } $type = $index->getType($this->table); $mapping = new ElasticaMapping(); $mapping->setType($type); $mapping->setProperties($this->schema); $mapping->send(); $this->created[] = $db->configName(); }
public function setUp() { $client = $this->_getClient(); $index = $client->getIndex('elastica_test_filter_nested_abstract_filter'); $index->create(array(), true); $type = $index->getType('user'); $mapping = new Mapping(); $mapping->setProperties(array('firstname' => array('type' => 'string', 'store' => 'yes'), '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 Document(1, array('firstname' => 'Nicolas', 'lastname' => 'Ruflin', 'hobbies' => array(array('hobby' => 'opensource')))); $docs[] = new Document(2, array('firstname' => 'Nicolas', 'lastname' => 'Ippolito', 'hobbies' => array(array('hobby' => 'opensource'), array('hobby' => 'guitar')))); $response = $type->addDocuments($docs); // Refresh index $index->refresh(); }
/** * 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; }
/** * Create Elastica Analysis. * * @param object $index Elastica Index * @param array $posttypes Array containing all post types * @param array $terms Array containing all terms * @return object $index Elastica Index * * @since 3.0.0 */ protected function analysis($index, $posttypes, $terms) { //Check integrity if (empty($index)) { return null; } //Check integrity if (empty($posttypes) && empty($terms)) { return null; } //Define properties $props = array('id' => array('type' => 'integer', 'include_in_all' => false), 'tags' => array('type' => 'string', 'index' => 'analyzed'), 'parent' => array('type' => 'integer', 'index' => 'analyzed'), 'title' => array('type' => 'string', 'index' => 'analyzed'), 'content' => array('type' => 'string', 'index' => 'analyzed'), 'excerpt' => array('type' => 'string', 'index' => 'analyzed'), 'author' => array('type' => 'integer', 'index' => 'analyzed'), 'date' => array('type' => 'date', 'format' => 'date_time_no_millis'), 'tags_suggest' => array('type' => 'completion', 'analyzer' => 'simple', 'search_analyzer' => 'simple', 'payloads' => false), '_boost' => array('type' => 'float', 'include_in_all' => false)); /** * Update props analysis. * * @param array $props * * @since 3.3.0 */ do_action('tto_plugin_search_analysis', $props); //Set analysis if (isset($posttypes) && !empty($posttypes)) { foreach ($posttypes as $k) { if (empty($k)) { continue; } $index->create(array('number_of_shards' => 4, 'number_of_replicas' => 1, 'analysis' => array('analyzer' => array('indexAnalyzer' => array('type' => 'custom', 'tokenizer' => 'standard', 'filter' => array('lowercase', 'asciifolding', 'filter_' . $k)), 'searchAnalyzer' => array('type' => 'custom', 'tokenizer' => 'standard', 'filter' => array('standard', 'lowercase', 'asciifolding', 'filter_' . $k))), 'filter' => array('filter_' . $k => array('type' => 'standard', 'language' => TTO_LOCAL, 'ignoreCase' => true)))), true); //Define new Type $type = $index->getType($k); //Define a new Elastica Mapper $mapping = new Mapping(); $mapping->setType($type); //$mapping->setParam('analyzer', 'indexAnalyzer'); //$mapping->setParam('search_analyzer', 'searchAnalyzer'); //Define boost field /*$mapping->setParam('_boost', array( 'name' => '_boost', 'null_value' => 1.0 ));*/ //Set mapping $mapping->setProperties($props); //Send mapping to type $mapping->send(); } } //Set analysis if (isset($terms) && !empty($terms)) { foreach ($terms as $t) { if (empty($t)) { continue; } $index->create(array('number_of_shards' => 4, 'number_of_replicas' => 1, 'analysis' => array('analyzer' => array('indexAnalyzer' => array('type' => 'custom', 'tokenizer' => 'standard', 'filter' => array('lowercase', 'asciifolding', 'filter_' . $t)), 'searchAnalyzer' => array('type' => 'custom', 'tokenizer' => 'standard', 'filter' => array('standard', 'lowercase', 'asciifolding', 'filter_' . $t))), 'filter' => array('filter_' . $t => array('type' => 'standard', 'language' => TTO_LOCAL, 'ignoreCase' => true)))), true); //Define new Type $type = $index->getType($t); //Define a new Elastica Mapper $mapping = new Mapping(); $mapping->setType($type); $mapping->setParam('index_analyzer', 'indexAnalyzer'); $mapping->setParam('search_analyzer', 'searchAnalyzer'); //Define boost field $mapping->setParam('_boost', array('name' => '_boost', 'null_value' => 1.0)); //Set mapping $mapping->setProperties($props); // Send mapping to type $mapping->send(); } } //Return index return $index; }
/** * Return default search fields mapping for node translations * * @param Index $index * @param string $lang * * @return Mapping */ protected function getMapping(Index $index, $lang = 'en') { $mapping = new Mapping(); $mapping->setType($index->getType($this->indexType . '_' . $lang)); $mapping->setParam('analyzer', 'index_analyzer_' . $lang); $mapping->setParam('_boost', array('name' => '_boost', 'null_value' => 1.0)); $mapping->setProperties($this->properties); return $mapping; }
/** * @dataProvider providerTransport */ public function testGetMapping(array $config, $transport) { $client = new Client($config); $index = $client->getIndex('test'); $index->create(array(), true); $type = $index->getType('mappingTest'); // Define mapping $mapping = new Mapping(); $mapping->setParam('_boost', array('name' => '_boost', 'null_value' => 1.0)); $mapping->setProperties(array('id' => array('type' => 'integer', 'include_in_all' => FALSE), 'user' => array('type' => 'object', 'properties' => array('name' => array('type' => 'string', 'include_in_all' => TRUE), 'fullName' => array('type' => 'string', 'include_in_all' => TRUE))), 'msg' => array('type' => 'string', 'include_in_all' => TRUE), 'tstamp' => array('type' => 'date', 'include_in_all' => FALSE), 'location' => array('type' => 'geo_point', 'include_in_all' => FALSE), '_boost' => array('type' => 'float', 'include_in_all' => FALSE))); $type->setMapping($mapping); $index->refresh(); $times = array(); for ($i = 0; $i < $this->_max; $i++) { $response = $type->request('_mapping', Request::GET); $times[] = $response->getQueryTime(); } self::logResults('get mapping', $transport, $times); }
/** * 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 Mapping($this->getType()); $mapping->setProperties($this->_mapping); $mapping->setSource(array('enabled' => $this->_source)); $mapping->send(); }
protected function createMapping() { dump(__METHOD__); $type = $this->getClient()->getIndex($this->getIndexName())->getType($this->getTypeName()); $mapping = new Mapping(); $mapping->setType($type); $mappingProperties = (new MappingFactory())->make(); $mapping->setProperties($mappingProperties); $response = $mapping->send(); // dump($response); $this->assertNotTrue($response->hasError()); return $mappingProperties; }
<?php /** * Created by PhpStorm. * User: Jiang Yu * Date: 2016/04/22 * Time: 16:11. */ use Elastica\Type\Mapping; use ESGateway\DataModel\FieldMapping\MappingFactory; require __DIR__ . '/../../bootstrap.php'; $createMappingHandler = function (\Elastica\Type $type) { $mapping = new Mapping(); $mapping->setType($type); $mappingProperties = (new MappingFactory())->make(); $mapping->setProperties($mappingProperties); $response = $mapping->send(); dump('create default mapping'); dump($response); }; $options = getopt('', ['gv:', 'index:', 'createIndex:']); $client = new \Elastica\Client(['host' => ELASTIC_SEARCH_HOST, 'port' => ELASTIC_SEARCH_PORT]); if (isset($options['createIndex'])) { $indexName = trim($options['createIndex']); assert(is_string($indexName) && strlen($indexName) > 0 && strpos($indexName, 'farm_v') === 0, 'bad index name'); $index = $client->getIndex($indexName); $exists = $index->exists(); if (!$exists) { $response = $index->create(); dump('create index'); dump($response);
/** * @param Type $type * @param string $analyzer * @return Mapping */ private function getTypeMapping(Type $type, $analyzer) { $mapping = new Mapping($type); $mapping->setParam('analyzer', $analyzer); $mapping->setProperties(['text' => ['type' => 'string', 'index' => 'analyzed', 'analyzer' => $analyzer], 'episode' => ['type' => 'object', 'properties' => ['id' => ['type' => 'integer'], 'number' => ['type' => 'integer'], 'name' => ['type' => 'string', 'index' => 'analyzed', 'analyzer' => $analyzer], 'slug' => ['type' => 'string', 'index' => 'not_analyzed']]], 'season' => ['type' => 'object', 'parameters' => ['id' => ['type' => 'integer'], 'number' => ['type' => 'integer']]], 'character' => ['type' => 'object', 'properties' => ['name' => ['type' => 'string', 'index' => 'not_analyzed'], 'slug' => ['type' => 'string', 'index' => 'not_analyzed']]], 'line' => ['type' => 'integer']]); return $mapping; }
/** * {@inheritdoc} */ public function store(array $data, array $keys, $namespace) { $type = $this->index->getType($namespace); // Build mapping $mapping = new Mapping(); $mapping->setType($type); $mapping->setProperties(array('hash' => array('type' => 'string', 'include_in_all' => true), 'keys' => array('type' => 'string', 'include_in_all' => true))); $mapping->send(); // Build document $document = new Document('', array_merge($data, array('hash' => $this->getHash($data, $keys), 'keys' => $keys))); $type->addDocument($document); $this->index->refresh(); }
/** * @group functional */ public function testUpdateDocumentWithoutSource() { $index = $this->_createIndex(); $type = $index->getType('elastica_type'); $mapping = new Mapping(); $mapping->setProperties(array('name' => array('type' => 'string', 'store' => 'yes'), 'counter' => array('type' => 'integer', 'store' => 'no'))); $mapping->disableSource(); $type->setMapping($mapping); $newDocument = new Document(); $newDocument->setAutoPopulate(); $newDocument->set('name', 'Batman'); $newDocument->set('counter', 1); $type->addDocument($newDocument); $script = new Script('ctx._source.counter += count; ctx._source.name = name'); $script->setId($newDocument->getId()); $script->setParam('count', 2); $script->setParam('name', 'robin'); $script->setUpsert($newDocument); try { $type->updateDocument($script); $this->fail('Update request should fail because source is disabled. Fields param is not set'); } catch (ResponseException $e) { $error = $e->getResponse()->getError(); $this->assertContains('document_source_missing_exception', $error['type']); } $newDocument->setFieldsSource(); try { $type->updateDocument($newDocument); $this->fail('Update request should fail because source is disabled. Fields param is set to _source'); } catch (ResponseException $e) { $error = $e->getResponse()->getError(); $this->assertContains('document_source_missing_exception', $error['type']); } }
/** * Creates a mapping object * * @throws \Elastica\Exception\InvalidException If invalid type * * @param array|\Elastica\Type\Mapping $mapping Mapping object or properties array * @return self */ public static function create($mapping) { if (is_array($mapping)) { $mappingObject = new Mapping(); $mappingObject->setProperties($mapping); } else { $mappingObject = $mapping; } if (!$mappingObject instanceof Mapping) { throw new InvalidException('Invalid object type'); } return $mappingObject; }
/** * Define all known mappings */ protected function createMappings(\Elastica\Index $index) { foreach ($this->getIndexedClasses() as $class) { /** @var $sng Searchable */ $sng = singleton($class); $type = $sng->getElasticaType(); if (isset($this->mappings[$type])) { // captured later continue; } $mapping = $sng->getElasticaMapping(); $mapping->setType($index->getType($type)); $mapping->send(); } if ($this->mappings) { foreach ($this->mappings as $type => $fields) { $mapping = new Mapping(); $mapping->setProperties($fields); $mapping->setParam('date_detection', false); $mapping->setType($index->getType($type)); $mapping->send(); } } }
/** * @return \Elastica\Type\Mapping */ public function getElasticaMapping() { $mapping = new Mapping(); $mapping->setProperties($this->getElasticaFields()); return $mapping; }
/** * @param array $mappingArray * @param Elastica\Type $type */ protected function setMapping(array $mappingArray, Elastica\Type $type) { try { $mapping = new Elastica\Type\Mapping(); $mapping->setType($type); $mapping->setProperties($mappingArray); $mapping->send(); } catch (\Exception $e) { $this->outputLine('Could not add mapping for type ' . $type->getName() . '. Message was ' . $e->getMessage()); } }
/** * @param Index $elasticaIndex */ protected function setMapping(Index $elasticaIndex) { foreach ($this->configuration->getTypes() as $typeName) { $mapping = new Mapping(); $type = $elasticaIndex->getType($typeName); $mapping->setType($type); // Set properties if ($properties = $this->configuration->getMappingProperties($type)) { $mapping->setProperties($properties); } // Set params if any if ($mappingParams = $this->configuration->getMappingParams($type)) { foreach ($mappingParams as $mappingParam => $mappingParamValue) { $mapping->setParam($mappingParam, $mappingParamValue); } } if ($mappingParams || $properties) { $mapping->send(); } } }