PHP code example of bidzm / mysticquent

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

    

bidzm / mysticquent example snippets


Mysticquent\MysticquentServiceProvider::class

use Mysticquent\Searchable;

class User extends Model
{
    use Searchable;
}

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

public $documentType = 'custom_type';

public $documentIndex = 'custom_index';

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

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

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

Mysticquent::document()->bulkSave(User::find(1)->books);

$users = User::where('age','>',25)->get();

Mysticquent::document()->bulkDelete($users);

$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 Sleimanx2\Plastic\Map\Blueprint;
use Sleimanx2\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