/** * @return void */ public function indexAction() { $client = $this->clientFactory->create(); $index = $client->findIndex('timelogdata'); $query = '{ "size": 0, "aggregations": { "invoiced_over_time": { "date_histogram" : { "field" : "date", "interval" : "month", "time_zone": "Europe/Copenhagen" }, "aggregations": { "sum_amount": { "sum": { "field": "invAmount" } } } } } }'; $response = $index->request('GET', '/_search', array(), $query); $temp = $response->getTreatedContent(); $data = array(); foreach ($temp['aggregations']['invoiced_over_time']['buckets'] as $bucket) { $data[] = array('date' => date('Y-m', $bucket['key'] / 1000), 'value' => $bucket['sum_amount']['value']); } $this->view->assign('data', $data); }
/** * Import json file into ElasticSearch * * @param string $datafile The filename to import */ public function importTimelogDataFileCommand($datafile) { $jsonData = file_get_contents($datafile); $client = $this->clientFactory->create(); $indexName = 'timelogdata'; $workUnitIndex = $client->findIndex($indexName); if (!$workUnitIndex->exists()) { $workUnitIndex->create(); } $workUnitIndex->request('POST', '_flush'); $workUnitType = new WorkUnitType($workUnitIndex, 'WorkUnit'); $workUnitMapping = new WorkUnitMapping($workUnitType); $workUnitMapping->setPropertyByPath('customerName', array('type' => 'string', 'index' => 'not_analyzed')); $workUnitMapping->setPropertyByPath('employeeInitials', array('type' => 'string', 'index' => 'not_analyzed')); $workUnitMapping->setPropertyByPath('employeeName', array('type' => 'string', 'index' => 'not_analyzed')); $workUnitMapping->setPropertyByPath('projectName', array('type' => 'string', 'index' => 'not_analyzed')); $workUnitMapping->apply(); $lines = explode(PHP_EOL, $jsonData); $linesCount = count($lines); $this->output->progressStart($linesCount); foreach ($lines as $line) { $data = json_decode($line, TRUE); $this->output->progressAdvance(); if ($data !== NULL) { try { $document = new \Flowpack\ElasticSearch\Domain\Model\Document($workUnitType, $data); $document->store(); } catch (\Exception $e) { print 'Error submitting entry to ES. Data: ' . $line . PHP_EOL; } } } $this->output->progressFinish(); }
/** * final because else it could seriously damage the Index in the unlikely case there's already an index named typo3_ElasticSearch_FunctionalTests */ public final function setUp() { parent::setUp(); $this->clientFactory = $this->objectManager->get('Flowpack\\ElasticSearch\\Domain\\Factory\\ClientFactory'); $client = $this->clientFactory->create(); $this->testingIndex = $client->findIndex('typo3_elasticsearch_functionaltests'); if ($this->testingIndex->exists()) { throw new \Exception('The index "typo3_elasticsearch_functionaltests" already existed, aborting.', 1338967487); } else { $this->testingIndex->create(); $this->removeIndexOnTearDown = TRUE; } $this->additionalSetUp(); }
/** * This command will adjust the backend's mapping to the mapping the entity status prescribes. * * @param string $clientName The client name for the configuration. Defaults to the default client configured. * @return void */ public function convergeCommand($clientName = null) { $client = $this->clientFactory->create($clientName); $entityMappingCollection = $this->entityMappingBuilder->buildMappingInformation(); $this->backendMappingBuilder->setClient($client); $backendMappingCollection = $this->backendMappingBuilder->buildMappingInformation(); $additiveMappings = $entityMappingCollection->diffAgainstCollection($backendMappingCollection); /** @var $mapping \Flowpack\ElasticSearch\Domain\Model\Mapping */ foreach ($additiveMappings as $mapping) { $index = $mapping->getType()->getIndex(); $index->setClient($client); if (!$index->exists()) { $this->outputFormatted('Index <b>%s</b> does not exist', array($index->getName())); continue; } $this->outputLine('Attempt to apply properties to %s/%s: %s... ', array($index->getName(), $mapping->getType()->getName(), print_r($mapping->getProperties(), true))); $response = $mapping->apply(); if ($response->getStatusCode() === 200) { $this->outputFormatted('<b>OK</b>'); } else { $this->outputFormatted('<b>NOT OK</b>, response code was %d, response body was: %s', array($response->getStatusCode(), $response->getOriginalResponse()->getContent()), 4); } } if (0 === $additiveMappings->count()) { $this->outputLine('No mappings were to be applied.'); } }
/** * Shows the status of the current mapping * * @param string $object Class name of a domain object. If given, will only work on this single object * @param boolean $conductUpdate Set to TRUE to conduct the required corrections * @param string $clientName The client name to use */ public function statusCommand($object = null, $conductUpdate = false, $clientName = null) { $result = new ErrorResult(); $client = $this->clientFactory->create($clientName); $classesAndAnnotations = $this->indexInformer->getClassesAndAnnotations(); if ($object !== null) { if (!isset($classesAndAnnotations[$object])) { $this->outputFormatted("Error: Object '<b>%s</b>' is not configured correctly, check the Indexable annotation.", array($object)); $this->quit(1); } $classesAndAnnotations = array($object => $classesAndAnnotations[$object]); } array_walk($classesAndAnnotations, function (Indexable $annotation, $className) use($result, $client, $conductUpdate) { $this->outputFormatted("Object [33m%s[0m", array($className), 4); $this->outputFormatted("Index <b>%s</b> Type <b>%s</b>", array($annotation->indexName, $annotation->typeName), 8); $count = $client->findIndex($annotation->indexName)->findType($annotation->typeName)->count(); if ($count === null) { $result->forProperty($className)->addError(new Error('ElasticSearch was unable to retrieve a count for the type "%s" at index "%s". Probably these don\' exist.', 1340289921, array($annotation->typeName, $annotation->indexName))); } $this->outputFormatted("Documents in Search: <b>%s</b>", array($count !== null ? $count : "[41mError[0m"), 8); try { $count = $this->persistenceManager->createQueryForType($className)->count(); } catch (\Exception $exception) { $count = null; $result->forProperty($className)->addError(new Error('The persistence backend was unable to retrieve a count for the type "%s". The exception message was "%s".', 1340290088, array($className, $exception->getMessage()))); } $this->outputFormatted("Documents in Persistence: <b>%s</b>", array($count !== null ? $count : "[41mError[0m"), 8); if (!$result->forProperty($className)->hasErrors()) { $states = $this->getModificationsNeededStatesAndIdentifiers($client, $className); if ($conductUpdate) { $inserted = 0; $updated = 0; foreach ($states[ObjectIndexer::ACTION_TYPE_CREATE] as $identifier) { try { $this->objectIndexer->indexObject($this->persistenceManager->getObjectByIdentifier($identifier, $className)); $inserted++; } catch (\Exception $exception) { $result->forProperty($className)->addError(new Error('An error occurred while trying to add an object to the ElasticSearch backend. The exception message was "%s".', 1340356330, array($exception->getMessage()))); } } foreach ($states[ObjectIndexer::ACTION_TYPE_UPDATE] as $identifier) { try { $this->objectIndexer->indexObject($this->persistenceManager->getObjectByIdentifier($identifier, $className)); $updated++; } catch (\Exception $exception) { $result->forProperty($className)->addError(new Error('An error occurred while trying to update an object to the ElasticSearch backend. The exception message was "%s".', 1340358590, array($exception->getMessage()))); } } $this->outputFormatted("Objects inserted: <b>%s</b>", array($inserted), 8); $this->outputFormatted("Objects updated: <b>%s</b>", array($updated), 8); } else { $this->outputFormatted("Modifications needed: <b>create</b> %d, <b>update</b> %d", array(count($states[ObjectIndexer::ACTION_TYPE_CREATE]), count($states[ObjectIndexer::ACTION_TYPE_UPDATE])), 8); } } }); if ($result->hasErrors()) { $this->outputLine(); $this->outputLine('The following errors occurred:'); /** @var $error \TYPO3\Flow\Error\Error */ foreach ($result->getFlattenedErrors() as $className => $errors) { foreach ($errors as $error) { $this->outputLine(); $this->outputFormatted("<b>[41mError[0m</b> for [33m%s[0m:", array($className), 8); $this->outputFormatted((string) $error, array(), 4); } } } }
/** * Create a client * * @return Client */ public function create() { return $this->clientFactory->create(null, ElasticSearchClient::class); }
/** * Create a client * * @return \Flowpack\ElasticSearch\Domain\Model\Client */ public function create() { return $this->clientFactory->create(NULL, 'Flowpack\\ElasticSearch\\ContentRepositoryAdaptor\\ElasticSearchClient'); }