PHP code example of wannanbigpig / laravel-scout-elastic

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

    

wannanbigpig / laravel-scout-elastic example snippets


// config/scout.php


return [
    // ...
    
    'driver' => env('SCOUT_DRIVER', 'elasticsearch'),
    
    // ...
    
    /*
    |--------------------------------------------------------------------------
    | Elasticsearch Configuration
    |--------------------------------------------------------------------------
    |
    | Here you may configure your Elasticsearch settings.
    |
    */

    'elasticsearch' => [
        'hosts' => [env('ELASTICSEARCH_HOST', 'http://127.0.0.1:9200')],
        // 'auth' => [
        //     'username' => 'elastic',
        //     'password' => 'password copied during Elasticsearch start',
        // ],
        // index_ is followed by the index name. If you do not need to customize the index analyzer, skip the following Settings
        'index_article' => [
            'settings' => [
                'number_of_shards' => 5,
                'number_of_replicas' => 1,
            ],
            'mappings' => [
                "properties" => [
                    "title" => [
                        "type" => "text",
                        "analyzer" => "ik_max_word",
                        "search_analyzer" => "ik_smart",
                        "fields" => ["keyword" => ["type" => "keyword", "ignore_above" => 256]],
                    ],
                ],
            ],
        ],
    ],
];

use App\Models\Article;

// $condition = "test";
// ... or
// $condition = [
//     "title" => "test",
//     "abstract" => "test"
// ];
// ... or
$keyword = "test";
$source = [1,2];
$startTime = '2023-05-01T00:00:00.000+0800';
$endTime = '2023-05-20T00:00:00.000+0800';
$condition = [
    "_customize_body" => 1,
    "query" => [
        "bool" => [
            "should" => [
                [
                    "match" => [
                        "title" => ["query" => $keyword, 'boost' => 5],
                    ],
                ],
                [
                    "match" => [
                        "abstract" => ["query" => $keyword, 'boost' => 3],
                    ],
                ],
            ],
            "must" => [
                [
                    "terms" => ["source" => $source],
                ],
                [
                    "range" => [
                        "created_at" => [
                            'gte' => $startTime,
                            'lte' => $endTime,
                        ],
                    ],
                ],
            ],
        ],
    ],
];

$data = Article::search($condition)
        ->orderBy('_score', 'desc')
        ->paginate(10);
shell
php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"
shell
// create index
php artisan scout:index article

// delete index
php artisan scout:delete-index article

// batch update data to es
php artisan scout:import "App\Models\Article"