private function deleteIndex() { try { $this->client->indices()->delete(['index' => self::INDEX_NAME]); } catch (\Exception $e) { // Do nothing. } }
/** * Create the synonyms index for a store id. * * @param integer $storeId Store id. * @param string[] $synonyms Raw synonyms list. * @param string[] $expansions Raw expansions list. * * @return void */ public function reindex($storeId, $synonyms, $expansions) { $indexIdentifier = ThesaurusIndex::INDEX_IDENTIER; $indexName = $this->indexSettingsHelper->createIndexNameFromIdentifier($indexIdentifier, $storeId); $indexAlias = $this->indexSettingsHelper->getIndexAliasFromIdentifier($indexIdentifier, $storeId); $indexSettings = ['settings' => $this->getIndexSettings($synonyms, $expansions)]; $this->client->indices()->create(['index' => $indexName, 'body' => $indexSettings]); $this->indexManager->proceedIndexInstall($indexName, $indexAlias); $this->cacheHelper->cleanIndexCache(ThesaurusIndex::INDEX_IDENTIER, $storeId); }
public function createIndex($index, array $params = []) { try { $this->clientTo->indices()->delete(['index' => $index]); } catch (Missing404Exception $e) { } if ($params) { $this->clientTo->indices()->create(['index' => $index, 'body' => $params]); } }
/** * Create the index. */ public function createIndex() { // Try to delete the index if it already exists. try { $this->client->indices()->delete(['index' => env('ES_INDEX')]); } catch (\Exception $e) { } // Create the mapping $params = ['index' => env('ES_INDEX'), 'body' => ['settings' => ['number_of_shards' => 1, 'number_of_replicas' => 0, 'analysis' => ['filter' => ['autocomplete_filter' => ['type' => 'ngram', 'min_gram' => 3, 'max_gram' => 20]], 'analyzer' => ['autocomplete' => ['type' => 'custom', 'tokenizer' => 'standard', 'filter' => ['lowercase', 'autocomplete_filter']]]]], 'mappings' => [$this->type => ['properties' => ['created_at' => ['type' => 'string', 'analyzer' => 'standard'], 'updated_at' => ['type' => 'string', 'analyzer' => 'standard'], 'filePath' => ['type' => 'string', 'analyzer' => 'standard'], 'filePath' => ['type' => 'string', 'analyzer' => 'standard'], 'fileName' => ['type' => 'string', 'analyzer' => 'standard'], 'fileUrl' => ['type' => 'string', 'analyzer' => 'standard'], 'scanId' => ['type' => 'string', 'analyzer' => 'standard'], 'mediaTypeId' => ['type' => 'string', 'analyzer' => 'standard'], 'media_typeId' => ['type' => 'string', 'analyzer' => 'standard'], 'mediaTypeName' => ['type' => 'string', 'analyzer' => 'standard'], 'scanCreated_at' => ['type' => 'string', 'analyzer' => 'standard'], 'scanUpdated_at' => ['type' => 'string', 'analyzer' => 'standard'], 'scanScanId' => ['type' => 'string', 'analyzer' => 'standard'], 'scanScanQuality' => ['type' => 'string', 'analyzer' => 'standard'], 'scanFileDirectory' => ['type' => 'string', 'analyzer' => 'standard'], 'scanLocation' => ['type' => 'string', 'analyzer' => 'standard'], 'scanScanTime' => ['type' => 'string', 'analyzer' => 'standard'], 'scanVoltage' => ['type' => 'string', 'analyzer' => 'standard'], 'scanVoxelSize' => ['type' => 'string', 'analyzer' => 'standard'], 'scanImageCount' => ['type' => 'integer'], 'scanCurrent' => ['type' => 'string', 'analyzer' => 'standard'], 'scanSequence' => ['type' => 'integer'], 'mediaTypeName' => ['type' => 'string', 'analyzer' => 'standard'], 'mediaTypeName' => ['type' => 'string', 'analyzer' => 'standard'], 'scanSpecimenId' => ['type' => 'integer'], 'scanMuseumId' => ['type' => 'integer'], 'scanAuthorId' => ['type' => 'integer', 'analyzer' => 'standard'], 'scanAnimalGroupId' => ['type' => 'integer'], 'scanSpecimenCreated_at' => ['type' => 'string', 'analyzer' => 'standard'], 'scanSpecimenUpdated_at' => ['type' => 'string', 'analyzer' => 'standard'], 'scanSpecimenSpecimenNumber' => ['type' => 'integer'], 'scanSpecimenSpeciesCommonName' => ['type' => 'string', 'analyzer' => 'autocomplete'], 'scanSpecimenSpeciesScientificName' => ['type' => 'string', 'analyzer' => 'autocomplete'], 'scanMuseumName' => ['type' => 'string', 'analyzer' => 'standard'], 'scanAuthorName' => ['type' => 'string', 'analyzer' => 'standard'], 'scanAnimal_grouId' => ['type' => 'integer'], 'scanAnimal_groupName' => ['type' => 'string', 'analyzer' => 'standard']]]]]]; $this->client->indices()->create($params); }
/** * Puts mapping into elasticsearch client. * * @param array $types Specific types to put. * @param bool $ignoreConflicts Ignore elasticsearch merge conflicts. */ public function updateMapping(array $types = [], $ignoreConflicts = true) { $this->isReadOnly('Mapping update'); $params['index'] = $this->getIndexName(); if (empty($types)) { $map = $this->getConfig()['mappings']; foreach ($map as $bundle) { if (strpos($bundle, ':')) { $types[] = $bundle; } else { $bundleMappings = $this->getMetadataCollector()->getMappings([$bundle]); foreach ($bundleMappings as $document) { $types[] = $document['bundle'] . ':' . $document['class']; } } } } foreach ($types as $type) { try { $mapping = $this->getMetadataCollector()->getClientMapping([$type]); $type = $this->getMetadataCollector()->getDocumentType($type); $params['type'] = $type; $params['body'] = $mapping; $params['ignore_conflicts'] = $ignoreConflicts; $this->client->indices()->putMapping(array_filter($params)); } catch (\Exception $e) { throw new \LogicException('Only the documents[] can be passed to the type update command. ' . 'Maybe you added only a bundle. Please check if a document is mapped in the manager.'); } } }
public function createIndex($settings = null, $mappings = null) { // Assemble new index name $nextIndexNumber = 1; $lastIndexName = $this->getLastIndexName(); if (null != $lastIndexName) { $lastIndexNameArr = explode('_', $lastIndexName); $nextIndexNumber = array_pop($lastIndexNameArr) + 1; } $nextIndexName = $this->getIndexName() . '_' . date('Y') . '_' . $nextIndexNumber; // Create new index $params['index'] = $nextIndexName; if (null != $settings) { $params['body']['settings'] = $settings; } if (null != $mappings) { $params['body']['mappings'] = $mappings; } $this->client->indices()->create($params); // Update aliases $params = []; $params['body'] = ['actions' => [['add' => ['index' => $nextIndexName, 'alias' => $this->getIndexNameCurrent()]], ['add' => ['index' => $nextIndexName, 'alias' => $this->getIndexName()]]]]; if (null != $lastIndexName) { array_unshift($params['body']['actions'], ['remove' => ['index' => $lastIndexName, 'alias' => $this->getIndexNameCurrent()]]); } $this->client->indices()->updateAliases($params); return $nextIndexName; }
/** * Count document into the index and then multiply it by the request cutoff frequency * to compute an absolute cutoff frequency limit (max numbre of doc). * * @param RequestInterface $request The spellcheck request. * * @return int */ private function getCutoffrequencyLimit(RequestInterface $request) { $indexStatsResponse = $this->client->indices()->stats(['index' => $request->getIndex()]); $indexStats = current($indexStatsResponse['indices']); $totalIndexedDocs = $indexStats['total']['docs']['count']; return $request->getCutoffFrequency() * $totalIndexedDocs; }
/** * Puts mapping into elasticsearch client. * * @param array $types Specific types to put. * * @return array */ public function updateMapping(array $types = []) { $this->isReadOnly('Mapping update'); $params['index'] = $this->getIndexName(); $params['types'] = array_keys($types); $params['body'] = $types; return $this->client->indices()->putMapping($params); }
/** * @test */ public function callClearWithNonExistingIndexIsOk() { $client = $this->createClient(); $serializer = $this->createSerializer(); $storage = $this->createStorage($client, $serializer); self::$client->indices()->delete(['index' => self::$index]); $storage->clear(); }
/** * Returns all of the mappings for the specified indices, and types. * * @param array $indices The indices to get mappings from. Defaults to all indices. * @param array $types The types to get mappings for. Defaults to all types. * * @return array like [ * 'indexName' => [ * 'mappings' => [ * 'typeName' => [ * 'properties' => [ * 'fieldName' => [ * 'type' => (string).. * ], * ], * ], * ], * ]... * ]; */ public function getMappings(array $indices = [], array $types = []) { //If no indices or types are specified return mappings for all indices and types. if (empty($indices) && empty($types)) { return $this->client()->indices()->getMapping(); } return $this->client->indices()->getMapping(['index' => $indices, 'type' => $types]); }
/** * Handle index creation command * * @param Client $client * @param string $index */ public function handle(Client $client, $index, $type) { $config = $this->configurationRepository->get($index); if ($this->isInvalid($config, $type)) { throw new \InvalidArgumentException(); } $client->indices()->putMapping(['index' => $index, 'type' => $type, 'body' => [$type => $config['mappings'][$type]]]); }
public function getIndices() { $indices = $this->client->indices()->get(['index' => $this->indexPrefix . '_syrup*']); if (!empty($indices)) { return array_keys($indices); } return []; }
/** * Handle index creation command * * @param Client $client * @param string $index */ public function handle(Client $client, $index) { $config = $this->configurationRepository->get($index); if (null === $config) { throw new \InvalidArgumentException(); } $client->indices()->create(['index' => $index, 'body' => $config]); }
/** * Handle index creation command * * @param Client $client * @param string $index */ public function handle($client, $index) { $config = $this->configurationRepository->get($index); if (null === $client || null === $config) { throw new \InvalidArgumentException(); } $client->indices()->putSettings(['index' => $index, 'body' => ['settings' => $this->extractSettings($config)]]); }
private function setSetting($name, $value) { $index = $this->options->getIndexName(); $params = array(); $params['index'] = $index; $params['body'][$name] = $value; $response = $this->client->indices()->putSettings($params); return igorw\get_in($response, ['acknowledged']); }
/** * Execute the console command. */ public function handle() { if (!$this->option('yes') && !$this->shouldContinue()) { $this->line('Cancelled.'); return; } if (!$this->elasticsearch->indices()->exists(['index' => $this->indexName()])) { $this->warn("There is no index called `{$this->indexName()}`, stopping."); return; } $this->line("Deleting the `{$this->indexName()}` index..."); try { $this->elasticsearch->indices()->delete(['index' => $this->indexName()]); $this->info("Deleted the `{$this->indexName()}` index."); } catch (\Throwable $err) { $this->error("Failed to delete index `{$this->indexName()}`: " . $err->getMessage()); } }
protected function checkIndex() { $params = ['index' => $this->config->getIndex()]; $indices = $this->client->indices(); if (!$indices->exists($params)) { $indices->create($params); sleep(1); } }
/** * @param $configuration * @param $params */ public function initializeIndex($configuration, $params) { // $params = ['index' => $configuration['index']]; // $response = $this->client->indices()->delete($params); $params['index'] = $configuration['index']; if ($this->client->indices()->exists($params) == false) { $params = ['index' => $configuration['index'], 'body' => [$configuration['type'] => ['_source' => ['enabled' => true], "_timestamp" => ["enabled" => true], 'properties' => ['id' => ['type' => 'integer'], 'pageUrl' => ['type' => 'string', 'analyzer' => 'standard'], 'L' => ['type' => 'integer'], 'pageTitle' => ['type' => 'string'], 'content' => ['type' => 'string'], "indexedAt" => ["type" => "date"]]]]]; $this->client->indices()->create($params); } // $this->client->indices()->putMapping($params); }
private function clearCache(\Elasticsearch\Client $client, OutputInterface $output) { $output->writeln('<info>Clearing filter cache (scheduled to clear in 60s)</info>'); $size = Response::fetchFilterCache($client->indices()->stats()); $times = 60; $client->indices()->clearCache(['filter' => true]); $progress = new ProgressHelper(); $progress->setFormat(ProgressHelper::FORMAT_QUIET_NOMAX); $progress->start($output, $times); while (Response::fetchFilterCache($client->indices()->stats()) == $size) { sleep(1); $progress->advance(); $times--; if ($times <= 0) { break; } } $progress->finish(); $output->writeln('<info>Done cache clear.</info>'); }
/** * List of all mappings in specified index * @return array */ public function listIndiceMappings($indice) { $return = array(); $stats = $this->client->indices()->getMapping(array('index' => $indice)); if (!empty($stats[$indice]) && !empty($stats[$indice]['mappings'])) { foreach (array_keys($stats[$indice]['mappings']) as $mapping) { $return[] = array('id' => $mapping); } } return $return; }
/** * Ensure the mapping is correct * @return void */ protected function ensureMapping() { $params = ['index' => $this->index, 'type' => $this->getType()]; $mapping = $this->elasticsearchClient->indices()->getMapping($params); $mapping = isset($mapping[$this->index]) ? $mapping[$this->index]['mappings'] : []; $mapping = isset($mapping[$this->getType()]) ? $mapping[$this->getType()] : []; $mergedMapping = array_replace_recursive($mapping, $this->getMapping()); if ($mergedMapping != $mapping) { $params['body'] = [$this->getType() => $mergedMapping]; $this->elasticsearchClient->indices()->putMapping($params); } }
private function getLatestVersionOfIndex($indexLogicalName) { $max = 0; foreach ($this->elasticsearch->indices()->getAliases() as $index => $aliases) { if (preg_match("/^{$indexLogicalName}_v([0-9]+)\$/", $index, $match)) { $v = intval($match[1]); if ($v > $max) { $max = $v; } } } return $max; }
/** * Removes unused indices */ public function cleanupIndices() { $prefix = $this->indexFactory->getPrefix(); $aliases = $this->client->indices()->getAliases(); foreach ($aliases as $index => $indexAliases) { if (strpos($index, $prefix) !== 0) { continue; } if (empty($indexAliases['aliases'])) { $this->client->indices()->delete(['index' => $index]); } } }
/** * Generates all possible synonym rewrites for a store and text query. * * @param integer $storeId Store id. * @param string $queryText Text query. * @param string $type Substitution type (synonym or expansion). * * @return array */ private function getSynonymRewrites($storeId, $queryText, $type) { $indexName = $this->getIndexAlias($storeId); $analysis = $this->client->indices()->analyze(['index' => $indexName, 'text' => $queryText, 'analyzer' => $type]); $synonymByPositions = []; foreach ($analysis['tokens'] as $token) { if ($token['type'] == 'SYNONYM') { $positionKey = sprintf('%s_%s', $token['start_offset'], $token['end_offset']); $synonymByPositions[$positionKey][] = $token; } } return $this->combineSynonyms($queryText, $synonymByPositions); }
/** * Indexes all posts in the forum in ES */ public function indexAll() { $deleteParams = ['index' => $this->config->get('index', 'phosphorum')]; try { $this->client->indices()->delete($deleteParams); } catch (Missing404Exception $e) { $this->logger->info('The index does not exist yet. Skip deleting...'); } catch (\Exception $e) { $this->logger->error("Indexer: {$e->getMessage()}. {$e->getFile()}:{$e->getLine()}"); } foreach (Posts::find('deleted != ' . Posts::IS_DELETED) as $post) { $this->doIndex($post); } }
/** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function createIndexMapping() { // //Bo sung them hash tag neu can $mappingPost = array("_source" => array('enabled' => true), 'properties' => array('post_id' => array('type' => 'long'), 'description' => array('type' => 'string', 'analyzer' => 'nGram_analyzer'))); $mappingBoard = array("_source" => array('enabled' => true), 'properties' => array('board_id' => array('type' => 'long'), 'title' => array('type' => 'string', 'analyzer' => 'whitespace'))); $mappingUser = array("_source" => array('enabled' => true), 'properties' => array('user_id' => array('type' => 'long'), 'username' => array('type' => 'string', 'analyzer' => 'nGram_analyzer'), 'avatar_link' => array('type' => 'string'))); $analysis = array("analysis" => array("filter" => array("nGram_filter" => array("type" => "edge_ngram", "min_gram" => 1, "max_gram" => 30, "token_chars" => ["letter", "digit", "punctuation", "symbol"])), "analyzer" => array("nGram_analyzer" => array('type' => 'custom', "tokenizer" => 'standard', "filter" => ["lowercase", "nGram_filter"])))); $client = new Client(); $indexParams['index'] = INDEX_NAME; $indexParams['body']['settings']['index'] = $analysis; // Index Settings $indexParams['body']['mappings']['post'] = $mappingPost; $indexParams['body']['mappings']['board'] = $mappingBoard; $indexParams['body']['mappings']['user'] = $mappingUser; $result = $client->indices()->create($indexParams); return $result; }
/** * {@inheritDoc} */ public function proceedIndexInstall($indexName, $indexAlias) { $aliasActions = [['add' => ['index' => $indexName, 'alias' => $indexAlias]]]; $deletedIndices = []; try { $oldIndices = $this->client->indices()->getMapping(['index' => $indexAlias]); } catch (\Elasticsearch\Common\Exceptions\Missing404Exception $e) { $oldIndices = []; } foreach (array_keys($oldIndices) as $oldIndexName) { if ($oldIndexName != $indexName) { $deletedIndices[] = $oldIndexName; $aliasActions[] = ['remove' => ['index' => $oldIndexName, 'alias' => $indexAlias]]; } } $this->client->indices()->updateAliases(['body' => ['actions' => $aliasActions]]); foreach ($deletedIndices as $deletedIndex) { $this->client->indices()->delete(['index' => $deletedIndex]); } }
public function testDeleteIndex() { $response = $this->client->indices()->delete(['index' => 'my_index']); $this->assertEquals(1, $response['acknowledged']); }
/** * Indexes all posts in the forum in ES */ public function indexAll() { $client = new Client(); try { $deleteParams['index'] = 'phosphorum'; $client->indices()->delete($deleteParams); } catch (\Exception $e) { // the index does not exist yet } foreach (Posts::find('deleted != 1') as $post) { $this->doIndex($client, $post); } }
protected function classTearDown() { $indexParams['index'] = 'benchmarking_index'; $this->setupClient->indices()->delete($indexParams); }