public function testGetIdNoSource() { // Creates a new index 'xodoa' and a type 'user' inside this index $indexName = 'xodoa'; $typeName = 'user'; $client = $this->_getClient(); $index = $client->getIndex($indexName); $index->create(array(), true); $type = $index->getType($typeName); $mapping = new Mapping($type); $mapping->disableSource(); $mapping->send(); // Adds 1 document to the index $docId = 3; $doc1 = new Document($docId, array('username' => 'hans')); $type->addDocument($doc1); // Refreshes index $index->refresh(); $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()); }
protected function _getTestIndex() { $index = $this->_createIndex('has_child_test'); $parentType = $index->getType('parent'); $childType = $index->getType('child'); $childMapping = new Mapping($childType); $childMapping->setParent('parent'); $childMapping->send(); $altType = $index->getType('alt'); $altDoc = new Document('alt1', array('name' => 'altname')); $altType->addDocument($altDoc); $parent1 = new Document('parent1', array('id' => 'parent1', 'user' => 'parent1', 'email' => '*****@*****.**')); $parentType->addDocument($parent1); $parent2 = new Document('parent2', array('id' => 'parent2', 'user' => 'parent2', 'email' => '*****@*****.**')); $parentType->addDocument($parent2); $child1 = new Document('child1', array('id' => 'child1', 'user' => 'child1', 'email' => '*****@*****.**')); $child1->setParent('parent1'); $childType->addDocument($child1); $child2 = new Document('child2', array('id' => 'child2', 'user' => 'child2', 'email' => '*****@*****.**')); $child2->setParent('parent2'); $childType->addDocument($child2); $child3 = new Document('child3', array('id' => 'child3', 'user' => 'child3', 'email' => '*****@*****.**', 'alt' => array(array('name' => 'testname')))); $child3->setParent('parent2'); $childType->addDocument($child3); $index->refresh(); return $index; }
/** * @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(); }
/** * @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 { foreach ($actions as $action) { $action->send(); } $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(); }
/** * @return \Elastica\Type\Mapping */ public function getElasticaMapping() { $mapping = new Mapping(); $mapping->setProperties($this->getElasticaFields()); $mapping->setParam('date_detection', false); return $mapping; }
/** * @group functional */ public function testSearch() { $client = $this->_getClient(); $index = new Index($client, 'test'); $index->create(array(), true); $index->getSettings()->setNumberOfReplicas(0); //$index->getSettings()->setNumberOfShards(1); $type = new Type($index, 'helloworldmlt'); $mapping = new Mapping($type, array('email' => array('store' => 'yes', 'type' => 'string', 'index' => 'analyzed'), 'content' => array('store' => 'yes', 'type' => 'string', 'index' => 'analyzed'))); $mapping->setSource(array('enabled' => false)); $type->setMapping($mapping); $doc = new Document(1000, array('email' => '*****@*****.**', 'content' => 'This is a sample post. Hello World Fuzzy Like This!')); $type->addDocument($doc); $doc = new Document(1001, array('email' => '*****@*****.**', 'content' => 'This is a fake nospam email address for gmail')); $type->addDocument($doc); // Refresh index $index->refresh(); $mltQuery = new MoreLikeThis(); $mltQuery->setLike('fake gmail sample'); $mltQuery->setFields(array('email', 'content')); $mltQuery->setMaxQueryTerms(3); $mltQuery->setMinDocFrequency(1); $mltQuery->setMinTermFrequency(1); $query = new Query(); $query->setQuery($mltQuery); $resultSet = $type->search($query); $resultSet->getResponse()->getData(); $this->assertEquals(2, $resultSet->count()); }
/** * @group functional */ public function testHasParent() { $index = $this->_createIndex(); $shopType = $index->getType('shop'); $productType = $index->getType('product'); $mapping = new Mapping(); $mapping->setParent('shop'); $productType->setMapping($mapping); $shopType->addDocuments(array(new Document('zurich', array('brand' => 'google')), new Document('london', array('brand' => 'apple')))); $doc1 = new Document(1, array('device' => 'chromebook')); $doc1->setParent('zurich'); $doc2 = new Document(2, array('device' => 'macmini')); $doc2->setParent('london'); $productType->addDocument($doc1); $productType->addDocument($doc2); $index->refresh(); // All documents $parentQuery = new HasParent(new MatchAll(), $shopType->getName()); $search = new Search($index->getClient()); $results = $search->search($parentQuery); $this->assertEquals(2, $results->count()); $match = new Match(); $match->setField('brand', 'google'); $parentQuery = new HasParent($match, $shopType->getName()); $search = new Search($index->getClient()); $results = $search->search($parentQuery); $this->assertEquals(1, $results->count()); $result = $results->current(); $data = $result->getData(); $this->assertEquals($data['device'], 'chromebook'); }
/** * @param \Elastica\Index $index * @param string $mappingName * @param array $mappingData * * @return void */ protected function sendMapping(Index $index, $mappingName, array $mappingData) { $type = $index->getType($mappingName); $this->messenger->info(sprintf('Send mapping type "%s" (index: "%s")', $mappingName, $index->getName())); $mapping = new Mapping($type); foreach ($mappingData as $key => $value) { $mapping->setParam($key, $value); } $mapping->send(); }
/** * 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(); }
public function testParentMapping() { $index = $this->_createIndex(); $parenttype = new Type($index, 'parenttype'); $parentmapping = new Mapping($parenttype, array('name' => array('type' => 'string', 'store' => 'yes'))); $parenttype->setMapping($parentmapping); $childtype = new Type($index, 'childtype'); $childmapping = new Mapping($childtype, array('name' => array('type' => 'string', 'store' => 'yes'))); $childmapping->setParam('_parent', array('type' => 'parenttype')); $childtype->setMapping($childmapping); }
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(); }
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("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(); }
/** * @param Schema $schema * @param string $defaultAnalyzer * @return Mapping */ public function create(Schema $schema, $defaultAnalyzer = null) { $this->defaultAnalyzer = $defaultAnalyzer; $rootObject = new \stdClass(); $rootObject->dynamic_templates = []; $mapping = new Mapping(null, $this->mapSchema($schema, $rootObject)); foreach (get_object_vars($rootObject) as $k => $v) { if (!empty($v)) { $mapping->setParam($k, $v); } } return $mapping; }
/** * 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(); }
public function testIndexMappingForParent() { $type = $this->getMockElasticaType(); $this->indexConfigsByName['parent']['index']->expects($this->once())->method('getType')->with('a')->will($this->returnValue($type)); $type->expects($this->once())->method('delete'); $mapping = Mapping::create($this->indexConfigsByName['parent']['config']['mappings']['a']['properties']); $mapping->setParam('_parent', array('type' => 'b')); $type->expects($this->once())->method('setMapping')->with($mapping); $resetter = new Resetter($this->indexConfigsByName); $resetter->resetIndexType('parent', 'a'); }
public function testSearch() { $client = $this->_getClient(); $index = new Index($client, 'test'); $index->create(array(), true); $index->getSettings()->setNumberOfReplicas(0); //$index->getSettings()->setNumberOfShards(1); $type = new Type($index, 'helloworldfuzzy'); $mapping = new Mapping($type, array('email' => array('store' => 'yes', 'type' => 'string', 'index' => 'analyzed'), 'content' => array('store' => 'yes', 'type' => 'string', 'index' => 'analyzed'))); $mapping->setSource(array('enabled' => false)); $type->setMapping($mapping); $doc = new Document(1000, array('email' => '*****@*****.**', 'content' => 'This is a sample post. Hello World Fuzzy Like This!')); $type->addDocument($doc); // Refresh index $index->refresh(); $fltQuery = new FuzzyLikeThis(); $fltQuery->setLikeText("sample gmail"); $fltQuery->addFields(array("email", "content")); $fltQuery->setMinSimilarity(0.3); $fltQuery->setMaxQueryTerms(3); $resultSet = $type->search($fltQuery); $this->assertEquals(1, $resultSet->count()); }
/** * create type mapping object * * @param array $indexConfig * @return Mapping */ protected function createMapping($indexConfig) { $mapping = Mapping::create($indexConfig['properties']); $mappingSpecialFields = array('_uid', '_id', '_source', '_all', '_analyzer', '_boost', '_routing', '_index', '_size', '_timestamp', '_ttl', 'dynamic_templates'); foreach ($mappingSpecialFields as $specialField) { if (isset($indexConfig[$specialField])) { $mapping->setParam($specialField, $indexConfig[$specialField]); } } if (isset($indexConfig['_parent'])) { $mapping->setParam('_parent', array('type' => $indexConfig['_parent']['type'])); } return $mapping; }
/** * 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; }
/** * @group functional */ public function testGetters() { $index = $this->_createIndex(); $type = $index->getType('test'); $properties = array('firstname' => array('type' => 'string', 'store' => true), 'lastname' => array('type' => 'string')); $mapping = new Mapping($type, $properties); $all = array('enabled' => true, 'store' => true); $mapping->setParam('_all', $all); $get_all = $mapping->getParam('_all'); $this->assertEquals($get_all, $all); $this->assertNull($mapping->getParam('_boost', $all)); $this->assertEquals($properties, $mapping->getProperties()); $index->delete(); }
/** * Deletes and recreates a mapping type for the named index * * @param string $indexName * @param string $typeName * @throws \InvalidArgumentException if no index or type mapping exists for the given names * @throws ResponseException */ public function resetIndexType($indexName, $typeName) { $typeConfig = $this->configManager->getTypeConfiguration($indexName, $typeName); $type = $this->indexManager->getIndex($indexName)->getType($typeName); try { $type->delete(); } catch (ResponseException $e) { if (strpos($e->getMessage(), 'TypeMissingException') === false) { throw $e; } } $mapping = new Mapping(); foreach ($this->mappingBuilder->buildTypeMapping($typeConfig) as $name => $field) { $mapping->setParam($name, $field); } $type->setMapping($mapping); }