PHP code example of lordfm / plastic

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

    

lordfm / plastic example snippets


LoRDFM\Plastic\PlasticServiceProvider::class

use LoRDFM\Plastic\Searchable;

class Book extends Model
{
    use Searchable;
}

public $searchable = ['id', 'name', 'body', 'tags', 'images'];

public function buildDocument()
{
    return [
        'id' => $this->id,
        'tags' => $this->tags
    ];
}

public $documentType = 'custom_type';

public $documentIndex = 'custom_index';

$book = Book::first()->document()->save();

$book = Book::first()->document()->update();

$book = Book::first()->document()->delete();

Plastic::persist()->bulkSave(Tag::find(1)->books);

$authors = Author::where('age','>',25)->get();

Plastic::persist()->bulkDelete($authors);

$result = Book::search()->match('title','pulp')->get();

// Returns a collection of Book Models
$books = $result->hits();

// Returns the total number of matched documents
$result->totalHits();

// Returns the highest query score
$result->maxScore();

//Returns the time needed to execute the query
$result->took();

$dsl = Book::search()->match('title','pulp')->toDSL();

$books = Book::search()
    ->multiMatch(['title', 'description'], 'ham on rye', ['fuzziness' => 'AUTO'])
    ->sortBy('date')
    ->paginate();

$books->result();

User::search()
    ->must()
        ->term('name','kimchy')
    ->mustNot()
        ->range('age',['from'=>10,'to'=>20])
    ->should()
        ->match('bio','developer')
        ->match('bio','elastic')
    ->filter()
        ->term('tag','tech')
    ->get();

$contain = 'foo';

Post::search()
    ->multiMatch(['title', 'body'], $contain)
    ->nested('tags', function (SearchBuilder $builder) use ($contain) {
        $builder->match('tags.name', $contain);
    })->get();

$result = Book::search()->index('special-books')->match('title','pulp')->get();

$result = User::search()
    ->match('bio', 'elastic')
    ->aggregate(function (AggregationBuilder $builder) {
        $builder->average('average_age', 'age');
    })->get();

$aggregations = $result->aggregations();

Plastic::suggest()->completion('tag_suggest', 'photo')->get();

//this be handy if you have a custom index for your model
Tag::suggest()->term('tag_term','admin')->get();

use LoRDFM\Plastic\Map\Blueprint;
use LoRDFM\Plastic\Mappings\Mapping;

class AppTag extends Mapping
{
    /**
     * Full name of the model that should be mapped
     *
     * @var string
     */
    protected $model = App\Tag::class;

    /**
     * Run the mapping.
     *
     * @return void
     */
    public function map()
    {
        Map::create($this->getModelType(), function (Blueprint $map) {
            $map->string('name')->store('true')->index('analyzed');

            // instead of the fluent syntax we can use the second method argument to fill the attributes
            $map->completion('suggestion', ['analyzer' => 'simple', 'search_analyzer' => 'simple']);
        },$this->getModelIndex());
    }
}

$client = Plastic::getClient();

//index delete
$client->indices()->delete(['index'=> Plastic::getDefaultIndex()]);
//index create
$client->indices()->create(['index' => Plastic::getDefaultIndex()]);
bash
php artisan vendor:publish
bash
php artisan mapping:run
bash
php artisan mapping:rerun
bash
php artisan plastic:populate [--mappings][--index=...][--database=...]