/** * @return Index */ private function getIndex() { if (null === $this->index) { $this->index = $this->client->getIndex($this->indexName); if (!$this->index->exists()) { $this->index->create(); } } return $this->index; }
/** * @actionInfo(name='Бэкап данных') */ public function backupAction() { $this->folderName = __DIR__ . '/backup/'; if (!$this->elasticaIndex->exists()) { $this->log->error('Индекс для бэкапа отсутствует: {indexName}', ['indexName' => $this->indexName]); return; } $this->checkFileName(); $this->checkBackupFolder(); $this->log->info('Всё ок, бекапим {indexName} в {fileName}', ['indexName' => $this->indexName, 'fileName' => $this->fileName]); $this->log->info('Параметры бэкапа: sizePerShard={sizePerShard}', ['sizePerShard' => $this->sizePerShard]); $scanAndScroll = $this->getScanAndScroll(); foreach ($scanAndScroll as $resultSet) { $buffer = []; /* @var \Elastica\ResultSet $resultSet */ $results = $resultSet->getResults(); foreach ($results as $result) { $item = []; $item['_id'] = $result->getId(); $item['_source'] = $result->getSource(); $buffer[] = json_encode($item, JSON_UNESCAPED_UNICODE); } $fileBody = implode(PHP_EOL, $buffer); if (file_put_contents($this->fileName, $fileBody, FILE_APPEND)) { $countDocuments = count($results); $this->log->info('Сохранили {countDocuments} записей', ['countDocuments' => $countDocuments]); } else { $this->log->error('Ошибка записи данных'); die; } } }
/** * @return Status */ public function validate() { if ($this->startOver) { $this->outputIndented("Blowing away index to start over..."); $this->createIndex(true); $this->output("ok\n"); return Status::newGood(); } if (!$this->index->exists()) { $this->outputIndented("Creating index..."); $this->createIndex(false); $this->output("ok\n"); return Status::newGood(); } $this->outputIndented("Index exists so validating...\n"); return Status::newGood(); }
/** * Initialize the ElasticSearch client, and setup settings for the client. * * @return void */ public function initialize() { spl_autoload_register(array($this, 'autoLoad')); $this->_connectionOptions = array('url' => $this->modx->getOption('sisea.elastic.hostname', null, 'http://127.0.0.1') . ':' . $this->modx->getOption('sisea.elastic.port', null, 9200) . '/'); try { $this->client = new \Elastica\Client($this->_connectionOptions); $this->index = $this->client->getIndex(strtolower($this->modx->getOption('sisea.elastic.index', null, 'siplesearchindex'))); if (!$this->index->exists()) { $indexSetup = $this->modx->getObject('modSnippet', array('name' => 'SimpleSearchElasticIndexSetup')); if ($indexSetup) { $indexOptions = $this->modx->fromJSON($this->modx->runSnippet('SimpleSearchElasticIndexSetup')); } else { $indexOptions = $this->modx->fromJSON($this->modx->runSnippet('SimpleSearchElasticIndexSetup_default')); } $this->index->create($indexOptions, true); } } catch (Exception $e) { $this->modx->log(xPDO::LOG_LEVEL_ERROR, 'Error connecting to ElasticSearch server: ' . $e->getMessage()); } }
protected function setUp() { $typeName = Cache::TYPE_NAME; $this->type = $this->prophesize('\\Elastica\\Type'); $this->type->request(Argument::any(), Argument::cetera())->willReturn(true); $this->type->getName()->willReturn($typeName); $this->index = $this->prophesize('\\Elastica\\Index'); $this->index->getType($typeName)->willReturn($this->type->reveal()); $this->index->exists()->willReturn(true); $nsDoc = new Document('DoctrineNamespaceCacheKey[]', [Cache::VALUE_FIELD => serialize($this->namespaceId)]); $this->type->getIndex()->willReturn($this->index->reveal()); $this->type->getDocument("DoctrineNamespaceCacheKey[]")->willReturn($nsDoc); $this->client = $this->prophesize('\\Elastica\\Client'); $this->client->getIndex($this->indexName)->willReturn($this->index->reveal()); $this->cache = new Cache($this->client->reveal(), ['index' => $this->indexName]); }
/** * Удаление индекса */ public function deleteIndex() { if ($this->index->exists()) { $this->index->delete(); } }
protected function setUp() { /** @var ElasticaService elasticaService */ $elasticaService = $this->getContainer()->get("revinate_search.elasticsearch_service"); $this->elasticaClient = $elasticaService->getInstance(); $this->index = new \Elastica\Index($this->elasticaClient, View::INDEX_NAME); if (!$this->index->exists()) { $this->index->create(array("index.number_of_replicas" => "0", "index.number_of_shards" => "1")); $this->type = new \Elastica\Type($this->index, View::INDEX_TYPE); $mappingJson = json_decode(file_get_contents(__DIR__ . "/../data/es/mapping.json"), true); $mapping = new \Elastica\Type\Mapping($this->type, $mappingJson['properties']); $this->type->setMapping($mapping); } else { $this->type = new \Elastica\Type($this->index, View::INDEX_TYPE); } $this->timeSeriesIndex = new \Elastica\Index($this->elasticaClient, StatusLog::INDEX_NAME . self::$timeSeriesTestDateSuffix); if (!$this->timeSeriesIndex->exists()) { $this->timeSeriesIndex->create(array("index.number_of_replicas" => "0", "index.number_of_shards" => "1")); $this->timeSeriesType = new \Elastica\Type($this->timeSeriesIndex, StatusLog::INDEX_TYPE); $mappingJson = json_decode(file_get_contents(__DIR__ . "/../data/es/statusLogMapping.json"), true); $mapping = new \Elastica\Type\Mapping($this->timeSeriesType, $mappingJson['properties']); $this->timeSeriesType->setMapping($mapping); } else { $this->timeSeriesType = new \Elastica\Type($this->timeSeriesIndex, StatusLog::INDEX_TYPE); } }