PHP code example of opensolr / laravel-scout-opensolr

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

    

opensolr / laravel-scout-opensolr example snippets


use Laravel\Scout\Searchable;

class Post extends Model
{
    use Searchable;

    public function toSearchableArray(): array
    {
        return [
            'title' => $this->title,
            'body' => $this->body,
            'category' => $this->category,
        ];
    }
}

Post::search('how do keyword and semantic search combine?')->get();
Post::search('budget dining')->where('category', 'restaurants')->paginate(15);

// config/scout-opensolr.php
return [
    // ...
    'hybrid' => env('OPENSOLR_HYBRID', true),  // false = pure semantic
    'alpha'  => env('OPENSOLR_ALPHA', 0.5),    // 0 = all semantic … 1 = all lexical
];

use Opensolr\ScoutOpensolr\OpensolrClient;

$client = new OpensolrClient(config('scout-opensolr.email'), config('scout-opensolr.api_key'));

// What the picture reads as (labels, OCR text, barcodes) — no search yet.
// Accepts a file path or raw image bytes:
$read = $client->imageToWords('my-index', storage_path('app/photo.jpg'));
// ['text' => 'red running shoe', 'mode' => 'clip',
//  'labels' => ['running shoe', 'sneaker'], 'codes' => ['0123456789012']]

// Feed those words into the ordinary hybrid search:
$body = $client->embedAndSearch('my-index', $read['text'], 5, ['search_mode' => 'union']);
$docs = $body['response']['docs'];

use Laravel\Scout\EngineManager;

$engine = app(EngineManager::class)->engine('opensolr');

$engine->checkSchema();            // what the index can do, and why not
$engine->applySchema();            // install the Opensolr vector schema
$engine->applySchema(force: true); // the same on an index that already holds documents

use Laravel\Scout\EngineManager;

$answer = app(EngineManager::class)->engine('opensolr')->aiAnswer(
    'what does the refund policy say?',
    filterQuery: null,
    ragDocs: 4,       // how many hybrid hits feed the LLM (default 4)
    ragWords: 1500,   // words of text taken from each hit (default 1500)
    // instruction: 'Answer in German, cite the exact titles you used', // optional
);

$answer = app(EngineManager::class)->engine('opensolr')->aiAnswer(
    'what does the refund policy say?',
    tuning: [
        'search_mode' => 'keywords_

$client->hybridSearch($index, $query, freshBias: true);
$client->aiAnswer($index, $question, tuning: ['fresh_bias' => 1]);