Shigar kuma Sanya Elasticsearch a ciki Laravel

Don shigar da saita Elasticsearch ciki Laravel, bi waɗannan matakan:

Mataki 1: Shigar Elasticsearch

Da fari dai, kuna buƙatar shigarwa Elasticsearch akan sabar ku ko amfani da Elasticsearch sabis na girgije kamar Elastic Cloud. Ziyarci Elasticsearch gidan yanar gizon hukuma don saukar da sigar da ta dace kuma ku bi umarnin shigarwa.

Mataki 2: Shigar Elasticsearch Package don Laravel

Na gaba, shigar da Elasticsearch kunshin don Laravel. Akwai fakiti daban-daban waɗanda ke goyan bayan Elasticsearch a ciki Laravel, amma fakiti ɗaya sanannen shine " Laravel Scout ". Don shigarwa Laravel Scout, buɗe terminal kuma gudanar da umarni mai zuwa:

composer require laravel/scout

Mataki na 3: Sanya Elasticsearch ciki Laravel

Bayan installing Laravel Scout, kana bukatar ka saita shi don amfani Elasticsearch da matsayin tsoho search engine. Bude fayil ɗin .env Laravel na kuma ƙara sigogi masu zuwa:

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

Inda SCOUT_DRIVER ya bayyana injin binciken da ke Laravel Scout amfani da kuma SCOUT_ELASTICSEARCH_HOSTS ƙayyade Elasticsearch URL ɗin da Scout zai haɗa zuwa.

Mataki na 4: Gudu Migration

Na gaba, gudanar da migration don ƙirƙirar tebur "za'a iya bincika" don samfuran da kuke son bincika a ciki Elasticsearch. Yi amfani da umarni mai zuwa:

php artisan migrate

Mataki 5: Ƙayyade Model kuma Sanya Bayanin Bincike

A ƙarshe, a cikin ƙirar da kuke son bincika, ƙara Searchable yanayin kuma ayyana bayanin da ake nema don kowane ƙirar. Misali:

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  
        ];  
    }  
}  

Mataki 6: Aiki tare da Data Elasticsearch

Bayan daidaitawa da ma'anar ƙirar ƙira, gudanar da umarni don daidaita bayanai daga bayananku zuwa Elasticsearch:

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

Da zarar an gama, Elasticsearch an haɗa shi cikin Laravel, kuma zaku iya fara amfani da fasalin bincikensa a cikin aikace-aikacenku.