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


use App\Models\User;

// Implementation of the functions createWrappedDocumentManager() and createNewDocumentManager() is discussed later.
// $originalDocumentManager = createWrappedDocumentManager();
$newDocumentManager = createNewDocumentManager($frameworkContainer);

$user = new User(['id' => 123, 'name' => 'Administrator']);

// Move transformation to fractal transformer
// $data = ['id' => $user->id, 'name' => $user->name];

// $originalDocumentManager->index('users', '_doc', $data);
$newDocumentManager->create($user);
//$originalDocumentManager->bulkIndex('users', '_doc', [$data, $data, $data]);
$newDocumentManager->bulkCreate([$user, $user, $user]);
//$originalDocumentManager->update('users', '_doc', 123, ['name' => 'Moderator']);
$user->name = 'Moderator';
$newDocumentManager->update($user);
//$originalDocumentManager->updateOrIndex('users', '_doc', 123, ['name' => 'Super User']);
$user->name = 'Super User';
$newDocumentManager->updateOrCreate($user);
//$originalDocumentManager->delete('users', '_doc', 123);
$newDocumentManager->delete($user);
//$originalDocumentManager->exists('users', '_doc', 123);
$newDocumentManager->exists($user);
//$originalDocumentManager->get('users', '_doc', 123);
$newDocumentManager->get($user);
 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
    );
}