1. Go to this page and download the library: Download jolicode/elastically library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
jolicode / elastically example snippets
// Your own DTO, or one generated by Jane (see below)
class Beer
{
public string $foo;
public string $bar;
}
use JoliCode\Elastically\Factory;
use JoliCode\Elastically\Model\Document;
// Factory object with Elastica options + new Elastically options in the same array
$factory = new Factory([
// Where to find the mappings
Factory::CONFIG_MAPPINGS_DIRECTORY => __DIR__.'/mappings',
// What objects to find in each index
Factory::CONFIG_INDEX_CLASS_MAPPING => [
'beers' => Beer::class,
],
]);
// Class to perform request, same as the Elastica Client
$client = $factory->buildClient();
// Class to build Indexes
$indexBuilder = $factory->buildIndexBuilder();
// Create the Index in Elasticsearch
$index = $indexBuilder->createIndex('beers');
// Set the proper aliases
$indexBuilder->markAsLive($index, 'beers');
// Class to index DTO(s) in an Index
$indexer = $factory->buildIndexer();
$dto = new Beer();
$dto->bar = 'American Pale Ale';
$dto->foo = 'Hops from Alsace, France';
// Add a document to the queue
$indexer->scheduleIndex('beers', new Document('123', $dto));
$indexer->flush();
// Set parameters on the Bulk
$indexer->setBulkRequestParams([
'pipeline' => 'covfefe',
'refresh' => 'wait_for'
]);
// Force index refresh if needed
$indexer->refresh('beers');
// Get the Document (new!)
$results = $client->getIndex('beers')->getDocument('123');
// Get the DTO (new!)
$results = $client->getIndex('beers')->getModel('123');
// Perform a search
$results = $client->getIndex('beers')->search('alsace');
// Get the Elastic Document
$results->getDocuments()[0];
// Get the Elastica compatible Result
$results->getResults()[0];
// Get the DTO 🎉 (new!)
$results->getResults()[0]->getModel();
// Create a new version of the Index "beers"
$index = $indexBuilder->createIndex('beers');
// Slow down the Refresh Interval of the new Index to speed up indexation
$indexBuilder->slowDownRefresh($index);
$indexBuilder->speedUpRefresh($index);
// Set proper aliases
$indexBuilder->markAsLive($index, 'beers');
// Clean the old indices (close the previous one and delete the older)
$indexBuilder->purgeOldIndices('beers');
// Mapping change? Just call migrate and enjoy a full reindex (use the Task API internally to avoid timeout)
$newIndex = $indexBuilder->migrate($index);
$indexBuilder->speedUpRefresh($newIndex);
$indexBuilder->markAsLive($newIndex, 'beers');
use JoliCode\Elastically\Messenger\IndexationRequest;
use JoliCode\Elastically\Messenger\IndexationRequestHandler;
$bus->dispatch(new IndexationRequest(Product::class, '1234567890'));
// Third argument is the operation, so for a "delete" add this argument:
// new IndexationRequest(Product::class, 'ref9999', IndexationRequestHandler::OP_DELETE);
$factory = new Factory([
Factory::CONFIG_SERIALIZER => $serializer,
]);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.