Bincike na asali Laravel tare da Elasticsearch

Bincike na asali Laravel tare da shi Elasticsearch ne muhimmin fasali lokacin haɗawa Elasticsearch cikin aikin ku Laravel. Don yin bincike na asali, bi waɗannan matakan:

Mataki 1: Ƙirƙiri Model kuma Ƙirƙiri Bayanin Neman Bincike

Da farko, ƙirƙiri a model ciki Laravel kuma ayyana bayanin abin da ake nema don wannan model. Bayanin da ake nema shine tsararru mai ɗauke da filayen da kuke son bincika a ciki Elasticsearch.

Misali, a cikin Product  samfurin, kuna son bincika bisa ga filaye name da description  filayen.

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 2: Bincika Bayanai

Bayan ayyana bayanin da ake iya nema a cikin model, zaku iya amfani da search() hanyar don yin binciken bayanai a cikin Elasticsearch.

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

Hanyar search($keyword) za ta nemo bayanan da ke ɗauke da kalmar " Laravel " a cikin name da description filayen Product model.

Mataki 3: Nuna Sakamako

Bayan yin binciken, zaku iya amfani da sakamakon don nuna bayanai ga mai amfani.

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

Wannan yana ba ku damar gabatar da ainihin sakamakon bincike daga Elasticsearch cikin Laravel aikace-aikacenku.