Instal lan Konfigurasi Elasticsearch ing Laravel

Kanggo nginstal lan ngatur Elasticsearch ing Laravel, tindakake langkah iki:

Langkah 1: Instal Elasticsearch

Pisanan, sampeyan kudu nginstal Elasticsearch ing server utawa nggunakake Elasticsearch layanan maya kaya Elastic Cloud. Ngunjungi Elasticsearch situs web resmi kanggo ndownload versi sing cocog lan tindakake pandhuan instalasi.

Langkah 2: Instal Elasticsearch Package kanggo Laravel

Sabanjure, instal Elasticsearch paket kanggo Laravel. Ana macem-macem paket sing ndhukung Elasticsearch ing Laravel, nanging siji paket populer yaiku " Laravel Scout ". Kanggo nginstal Laravel Scout, bukak terminal lan jalanake printah ing ngisor iki:

composer require laravel/scout

Langkah 3: Konfigurasi Elasticsearch ing Laravel

Sawise nginstal Laravel Scout, sampeyan kudu ngatur supaya bisa digunakake Elasticsearch minangka mesin telusur standar. Bukak file .env Laravel lan tambahake parameter konfigurasi ing ngisor iki:

SCOUT_DRIVER=elasticsearch  
SCOUT_ELASTICSEARCH_HOSTS=http://localhost:9200  

Ngendi SCOUT_DRIVER nemtokake mesin telusur sing Laravel Scout nggunakake lan SCOUT_ELASTICSEARCH_HOSTS nemtokake Elasticsearch URL sing bakal disambungake Scout.

Langkah 4: Run Migration

Sabanjure, bukak migration kanggo nggawe tabel "ditelusuri" kanggo model sing pengin digoleki Elasticsearch. Gunakake printah ing ngisor iki:

php artisan migrate

Langkah 5: Nemtokake Model lan Temtokake Katrangan sing Bisa Digoleki

Pungkasan, ing model sing pengin digoleki, tambahake Searchable sifat kasebut lan nemtokake deskripsi sing bisa ditelusuri kanggo saben model. Tuladhane:

use Laravel\Scout\Searchable;  
  
class Product extends Model  
{  
    use Searchable;  
  
    public function toSearchableArray()  
    {  
        return [  
            'id' => $this->id,  
            'name' => $this->name,  
            'description' => $this->description,  
            // Add other searchable fields if needed  
        ];  
    }  
}  

Langkah 6: Sinkronisasi Data karo Elasticsearch

Sawise ngatur lan nemtokake model sing bisa ditelusuri, jalanake printah kanggo nyinkronake data saka database menyang Elasticsearch:

php artisan scout:import "App\Models\Product"

Sawise rampung, Elasticsearch wis digabungake menyang Laravel, lan sampeyan bisa miwiti nggunakake fitur telusuran ing aplikasi sampeyan.