PHP code example of tubber / model-indexer

1. Go to this page and download the library: Download tubber/model-indexer 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/ */

    

tubber / model-indexer example snippets


use Solarium\QueryType\Update\Query\Document;

class IndexableModel implements \Tubber\Indexer\Contracts\IndexableInterface
{

    /**
     * Get an array of documents that need to be indexed for this object
     *
     * @return array|Document[]
     */
    public function indexingDocuments(): array
    {
        return [
            new Document([
                'id' => 1,
                'name' => 'document 1'
            ]),
            new Document([
                'id' => 2,
                'name' => 'document 2'
            ]),
        ];
    }

    /**
     * Mark this object as indexed
     *
     * @return void
     */
    public function markAsIndexed(): void
    {
        return; // Perform some kind of action to indicate this object has been indexed
    }
    
    /**
     * Get the query to delete this object from the given core
     *
     * @param string $search_core
     * @return string
     */
    public function getDeleteQueryFor(string $search_core): string
    {
        return "id: 1";
    }
}

use Solarium\Client;
use Tubber\Indexer\Contracts\SolrConfigInterface;

class SearchConfig implements SolrConfigInterface
{

    /**
     * Get the client for this Solr interaction
     *
     * @return Client
     */
    public function getClient(): Client
    {
        $client = new Client();

        $client
            ->getEndpoint('your_solarium_endpoint_name')
            ->setCore('your_core_name');

        return $client;
    }

    /**
     * Reload the core that's provided in $this->getClient
     *
     * @return void
     */
    public function reloadCollection(): void
    {
        // You can implement your own way of reloading the collection
        // This method is call after indexing has finished
    }
}


/**@var array|Document[] $documents*/
$documents = [
    new IndexableModel()
];

ModelIndexer::forModels($documents, new SearchConfig)->perform();

use Tubber\Indexer\ModelRemover;

ModelRemover::forModels($documents, new SearchConfig)->perform();

/**
 * Set the collection of models that need to be indexed
 *
 * @param array $models
 * @param SolrConfigInterface $config
 * @return ModelIndexer
 * @throws NoCoreFoundException
 */
public static function forModels(array $models, SolrConfigInterface $config): ModelIndexer;

public function setBufferSize(int $buffer_size = 100): ModelIndexer;

public function setCommitWithin(int $commit_within = 10000): ModelIndexer;

ModelIndexer::forModels($models, new SearchConfig)
    ->setBufferSize(1500)
    ->setCommitWithin(5000)
    ->perform();

/**
 * Perform the indexing for the models in this class
 *
 * @throws \InvalidArgumentException
 */
public function perform(): void;