PHP code example of boomhq / laravel-scout-elastic

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

    

boomhq / 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'),
        ],
//set If one index per model (use searchableAs Methode) (defaut false)
        'perModelIndex' => true,
    ],
...

    public function elasticsearchIndex()
    {
        return  [
                  "settings" => [
                        "analysis" => [
                           "analyzer" => [
                              "default" => [
                                 "tokenizer" => "my_tokenizer", 
                                 "filter" => [
                                    "lowercase" 
                                 ] 
                              ], 
                              "default_search" => [
                                       "tokenizer" => "my_tokenizer" 
                                    ] 
                           ], 
                           "tokenizer" => [
                                          "my_tokenizer" => [
                                             "type" => "edge_ngram", 
                                             "min_gram" => 3, 
                                             "max_gram" => 20, 
                                             "token_chars" => [
                                                "letter" 
                                             ], 
                                             "filter" => [
                                                   "lowercase", 
                                                   "asciifolding" 
                                                ] 
                                          ] 
                                       ] 
                        ], 
                        "max_ngram_diff" => "20" 
                     ] 
               ]; 
                
    }


 public function customScoutQuerySearching($terms): array
    {
        return [
            'query' => [
                'multi_match' => [
                    'query' => (string) ($terms),
                    'fields' => [
                        '*'
                    ],
                    'fuzziness' => 'AUTO',
                    'type' => 'most_fields'
                ]
            ],
        ];
    }