PHP code example of ogini / oginisearch-laravel-scout
1. Go to this page and download the library: Download ogini/oginisearch-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/ */
ogini / oginisearch-laravel-scout example snippets
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Laravel\Scout\Searchable;
class Article extends Model
{
use Searchable;
/**
* Get the indexable data array for the model.
*/
public function toSearchableArray(): array
{
return [
'title' => $this->title,
'content' => $this->content,
'author' => $this->author->name,
'published_at' => $this->published_at,
];
}
/**
* Get the index name for the model.
*/
public function searchableAs(): string
{
return 'articles';
}
}
// Configure stopwords
Ogini::configureStopwords('articles', ['the', 'a', 'an', 'and', 'or'], 'en');
// Get current stopwords
$stopwords = Ogini::getStopwords('articles');
// Update stopwords
Ogini::updateStopwords('articles', ['the', 'a', 'an']);
// Reset to default
Ogini::resetStopwords('articles', 'en');
use OginiScoutDriver\Facades\AsyncOgini;
// Index documents asynchronously
$promise = AsyncOgini::indexDocumentAsync('articles', [
'title' => 'Async Article',
'content' => 'This article was indexed asynchronously',
]);
// Bulk index with callback
AsyncOgini::bulkIndexDocumentsAsync('articles', $documents,
function ($result) {
Log::info('Bulk indexing completed', $result);
},
function ($error) {
Log::error('Bulk indexing failed', ['error' => $error]);
}
);
// Search asynchronously
$searchPromise = AsyncOgini::searchAsync('articles', [
'query' => ['match' => ['title' => 'Laravel']],
]);
// Wait for all pending operations
$results = AsyncOgini::waitForAll();
// Enable queue integration
AsyncOgini::setQueueEnabled(true);
// Now operations will be queued instead of executed immediately
$jobId = AsyncOgini::indexDocumentAsync('articles', $document);
use OginiScoutDriver\Events\IndexingCompleted;
use OginiScoutDriver\Events\SearchCompleted;
// In your EventServiceProvider
protected $listen = [
IndexingCompleted::class => [
YourIndexingCompletedListener::class,
],
SearchCompleted::class => [
YourSearchCompletedListener::class,
],
];
namespace App\Listeners;
use OginiScoutDriver\Events\IndexingCompleted;
class LogIndexingSuccess
{
public function handle(IndexingCompleted $event): void
{
\Log::info('Document indexed successfully', [
'job_id' => $event->getJobId(),
'index_name' => $event->getIndexName(),
'document_id' => $event->getDocumentId(),
'is_bulk' => $event->isBulk(),
]);
}
}
// The package automatically optimizes queries based on configuration:
// - Removes short terms (< min_term_length)
// - Applies complexity scoring
// - Optimizes wildcard usage
// - Boosts phrase matches and exact matches
// Query results are automatically cached based on configuration
// Cache keys are generated from query parameters and settings
// TTL values are configurable for different operation types
// HTTP connections are pooled and reused for better performance
// Pool size and timeout settings are configurable
// Large operations are automatically batched
// Batch size and retry logic are configurable
use OginiUpdateChecker;
// Check if updates are available
if (OginiUpdateChecker::hasUpdate()) {
$updateInfo = OginiUpdateChecker::getUpdateInfo();
if ($updateInfo['security_update']) {
Log::warning('Security update available', $updateInfo);
}
if ($updateInfo['breaking_changes']) {
Log::info('Major version update available', $updateInfo);
}
}
// Get current and latest versions
$current = OginiUpdateChecker::getCurrentVersion();
$latest = OginiUpdateChecker::getLatestVersion();
// Clear update cache
OginiUpdateChecker::clearCache();