PHP code example of yucadoo / elasticsearcher-fractal
1. Go to this page and download the library: Download yucadoo/elasticsearcher-fractal 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/ */
yucadoo / elasticsearcher-fractal example snippets
php
namespace App\ElasticSearcher;
use YucaDoo\ElasticSearcher\Managers\DocumentAdapter;
class EloquentDocumentAdapter implements DocumentAdapter {
/**
* Get Elasticsearch id for Eloquent model.
* @param \Illuminate\Database\Eloquent\Model $item Eloquent model.
* @return string|int Elasticsearch id.
*/
public function getId($item)
{
return $item->getKey();
}
/**
* Get index name for Eloquent model.
* @param \Illuminate\Database\Eloquent\Model $item Eloquent model.
* @return string Elasticsearch index name.
*/
function getIndexName($item): string
{
// Elasticsearch index has the same name as database table.
return $item->getTable();
}
}
php
namespace App\ElasticSearcher\Transformers;
use App\Models\User;
use League\Fractal\TransformerAbstract;
class UserElasticsearchTransformer extends TransformerAbstract
{
/**
* Transform user into Elasticsearch document.
*
* @param User $user User to be converted into Elasticsearch document.
* @return array Generated Elasticsearch document.
*/
public function transform(User $user)
{
return [
'id' => $user->id,
'name' => $user->name,
];
}
}
php
use App\ElasticSearcher\EloquentDocumentAdapter;
use App\ElasticSearcher\Transformers\PostElasticsearchTransformer;
use App\ElasticSearcher\Transformers\UserElasticsearchTransformer;
use ElasticSearcher\Environment;
use ElasticSearcher\ElasticSearcher;
use ElasticSearcher\Managers\DocumentsManager as WrappedDocumentManager;
use League\Fractal\Manager as FractalManager;
use Mouf\AliasContainer\AliasContainer;
use Psr\Container\ContainerInterface;
use YucaDoo\ElasticSearcher\Managers\DocumentManager;
use YucaDoo\SingletonContainer\SingletonContainer;
/**
* Resolve old document manager. This function is shown for completeness.
* @return WrappedDocumentManager Document manager handling raw documents.
*/
function createWrappedDocumentManager(): WrappedDocumentManager
{
$env = new Environment(
['hosts' => ['localhost:9200']]
);
$searcher = new ElasticSearcher($env);
return $searcher->documentsManager();
}
/**
* Resolve new document manager.
* @param ContainerInterface $container Container providing transformer instances.
* @return DocumentManager Document manager handling models instead of raw documents.
*/
function createNewDocumentManager(ContainerInterface $container): DocumentManager
{
// Wrap container with singleton container to cache resolved transformers.
$singletonContainer = new SingletonContainer($container);
// Map index names to transformers.
$transformerRepository = new AliasContainer($singletonContainer, [
'posts' => PostElasticsearchTransformer::class,
'users' => UserElasticsearchTransformer::class,
]);
// Compose document manager.
return new DocumentManager(
createWrappedDocumentManager(),
new FractalManager(),
new EloquentDocumentAdapter(),
$transformerRepository
);
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.