Panelusuran dhasar ing Laravel karo Elasticsearch

Panelusuran dhasar Laravel minangka Elasticsearch fitur dhasar nalika nggabungake Elasticsearch menyang proyek sampeyan Laravel. Kanggo nindakake telusuran dhasar, tindakake langkah iki:

Langkah 1: Nggawe Model lan Nemtokake Katrangan sing Bisa Ditelusuri

Pisanan, nggawe model in Laravel lan nemtokake deskripsi sing bisa ditelusuri kanggo iki model. Katrangan sing bisa digoleki minangka larik sing ngemot kolom sing pengin digoleki Elasticsearch.

Contone, ing Product  model, sampeyan pengin nelusuri adhedhasar name lan description  lapangan.

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 2: Telusuri Data

Sawise nemtokake katrangan sing bisa digoleki ing model, sampeyan bisa nggunakake search() cara kanggo nindakake panelusuran data ing Elasticsearch.

$keyword = "Laravel";  
  
$results = Product::search($keyword)->get();  

Cara kasebut search($keyword) bakal nggoleki cathetan sing ngemot tembung kunci " Laravel " ing kolom name lan. description Product model

Langkah 3: Tampilan Asil

Sawise nindakake telusuran, sampeyan bisa nggunakake asil kanggo nampilake informasi menyang pangguna.

foreach($results as $result) {  
    echo $result->name. ": ". $result->description;  
    // Display product information or other search data  
}  

Iki ngidini sampeyan nampilake asil panelusuran dhasar saka Elasticsearch ing aplikasi sampeyan Laravel.