PHP code example of avadim / manticore-laravel-scout
1. Go to this page and download the library: Download avadim/manticore-laravel-scout 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/ */
use Illuminate\Database\Eloquent\Model;
use Laravel\Scout\Searchable;
class Post extends Model
{
use Searchable;
public function toSearchableArray(): array
{
return [
'title' => $this->title,
'body' => $this->body,
'author_id' => (int)$this->author_id,
'created_at' => $this->created_at->getTimestamp(),
];
}
}
Post::search('manticore') // WHERE MATCH('manticore')
->where('author_id', 7) // AND author_id = 7
->where('rating', '>', 4) // AND rating > 4
->whereIn('category_id', [1, 2, 3]) // AND category_id IN (1, 2, 3)
->whereNotIn('status', [0]) // AND status NOT IN (0)
->orderBy('created_at', 'desc') // ORDER BY created_at DESC
->take(50) // LIMIT 50
->get();
$post->scoutMetadata()['_score'];
Post::search('manticore')->options([
'fields' => ['title'], // search these full-text fields only
'escape' => false, // pass the query language through, see below
'highlight' => true, // or ['options' => [...], 'fields' => [...]]
'ranker' => 'sph04', // OPTION ranker=sph04
'field_weights' => ['title' => 10], // OPTION field_weights=(title=10)
'cutoff' => 1000, // OPTION cutoff=1000
])->get();
$post->scoutMetadata()['_highlight'];
use avadim\Manticore\Laravel\Database\ManticoreQueryBuilder as Query;
Post::search('manticore', function (Query $query, string $phrase) {
return $query->where('author_id', 7)->facet('author_id');
})->get();
use Laravel\Scout\EngineManager;
$engine = app(EngineManager::class)->engine('manticore');
$engine->transaction(function () { /* ... */ });
$engine->getSchemaBuilder()->getColumns('posts');
Post::search('manticore')->get();
$engine->getMeta()->toArray(); // the meta of the search that has just run
class Embedder
{
public function __invoke(string $phrase, $model): array
{
return $this->vectors->of($phrase);
}
}
use avadim\Manticore\Laravel\Database\ManticoreBlueprint;
public function manticoreSchema(): callable
{
return function (ManticoreBlueprint $table) {
$table->text('title');
$table->integer('author_id');
$table->floatVector('embedding', 1536, 'cosine');
};
}
'embedding' => $this->embedding, // an array of floats
Post::search('a fruit that keeps the doctor away')->semantic()->get();
Post::search('apple')->semantic(0.8)->get();
Post::search('apple')->hybrid(1, 2)->get();
$post->scoutMetadata()['_knn_dist']; // the distance, 0 being the vector itself
$post->scoutMetadata()['_similarity']; // 1 - the distance, i.e. 0 to 1 for a cosine index
$post->scoutMetadata()['_hybrid_score']; // of a hybrid search, what it was ranked by