PHP code example of crasmedia / laravel-scout-elastic

1. Go to this page and download the library: Download crasmedia/laravel-scout-elastic 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/ */

    

crasmedia / laravel-scout-elastic example snippets


// config/app.php
'providers' => [
    ...
    Laravel\Scout\ScoutServiceProvider::class,
    ...
    ScoutEngines\Elasticsearch\ElasticsearchProvider::class,
],

// config/scout.php
// Set your driver to elasticsearch
    'driver' => env('SCOUT_DRIVER', 'elasticsearch'),

...
    'elasticsearch' => [
        'index' => env('ELASTICSEARCH_INDEX', 'laravel'),
        'hosts' => [
            env('ELASTICSEARCH_HOST', 'http://localhost'),
        ],
    ],
...

$index = config('scout.elasticsearch.index');

// Check if index exists
if($model->searchableUsing()->exists($index))
{
    // Delete index if exists
    $model->searchableUsing()->deleteIndex($index);
}

// Create new index
$model->searchableUsing()->createIndex($index);

// Put custom mapping
$mappings = $model->mapping();
$model->searchableUsing()->putMapping($index, $model->getTable(), $mappings);

public function mapping()
{
    $mapping = [
        'title' => [
            'type' => 'keyword'
        ]
    ];

    return $mapping;
}

protected $boosts = [
    'title' => 2
];

Model::search('')->orderBy('title', 'asc')->get()