Implementation method of Laravel using scout to integrate elasticsearch to do full text search

  • 2021-11-10 09:02:37
  • OfStack

This paper introduces the implementation method of Laravel using scout to integrate elasticsearch to do full-text search, and shares it with you as follows:

Install the required components


composer require tamayo/laravel-scout-elastic
composer require laravel/scout 

If an error occurs in composer require laravel/scout


Using version ^6.1 for laravel/scout
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.

 Problem 1
  - tamayo/laravel-scout-elastic 4.0.0 requires laravel/scout ^5.0 -> satisfiable by laravel/scout[5.0.x-dev].
  - tamayo/laravel-scout-elastic 4.0.0 requires laravel/scout ^5.0 -> satisfiable by laravel/scout[5.0.x-dev].
  - tamayo/laravel-scout-elastic 4.0.0 requires laravel/scout ^5.0 -> satisfiable by laravel/scout[5.0.x-dev].
  - Conclusion: don't install laravel/scout 5.0.x-dev
  - Installation request for tamayo/laravel-scout-elastic ^4.0 -> satisfiable by tamayo/laravel-scout-elastic[4.0.0].


Installation failed, reverting ./composer.json to its original content.

Then use the command


composer require laravel/scout ^5.0

Modify the configuration file under 1 (config/app. php) to add the following two provider


'providers' => [ 
    //es search  Add the following  
    Laravel\Scout\ScoutServiceProvider::class, 
    ScoutEngines\Elasticsearch\ElasticsearchProvider::class, 
]

Add complete, execute command, generate config file


php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"

Modification of config/scout. php


  'driver' => env('SCOUT_DRIVER', 'elasticsearch'),

  'elasticsearch' => [
    'index' => env('ELASTICSEARCH_INDEX', ' Yours Index Name '),
    'hosts' => [
      env('ELASTICSEARCH_HOST', ''),
    ],
  ],

Configure the account number of ES at. env: password @ connection


ELASTICSEARCH_HOST=elastic: Password @ Your domain name .com:9200

Create a command line file to generate mapping to app/Console/Commands


<?php
namespace App\Console\Commands;
use GuzzleHttp\Client;
use Illuminate\Console\Command;

class ESInit extends Command {

  protected $signature = 'es:init';

  protected $description = 'init laravel es for news';

  public function __construct() { parent::__construct(); }

  public function handle() { // Create template
    $client = new Client(['auth'=>['elastic', 'Wangcai5388']]);
    $url = config('scout.elasticsearch.hosts')[0] . '/_template/news';
    $params = [
      'json' => [
        'template' => config('scout.elasticsearch.index'),
        'settings' => [
          'number_of_shards' => 5
        ],
        'mappings' => [
          '_default_' => [
            'dynamic_templates' => [
              [
                'strings' => [
                  'match_mapping_type' => 'string',
                  'mapping' => [
                    'type' => 'text',
                    'analyzer' => 'ik_smart',
                    'ignore_above' => 256,
                    'fields' => [
                      'keyword' => [
                        'type' => 'keyword'
                      ]
                    ]
                  ]
                ]
              ]
            ]
          ]
        ]
      ]
    ];
    $client->put($url, $params);

    //  Create index
    $url = config('scout.elasticsearch.hosts')[0] . '/' . config('scout.elasticsearch.index');

    $params = [
      'json' => [
        'settings' => [
          'refresh_interval' => '5s',
          'number_of_shards' => 5,
          'number_of_replicas' => 0
        ],
        'mappings' => [
          '_default_' => [
            '_all' => [
              'enabled' => false
            ]
          ]
        ]
      ]
    ];
    $client->put($url, $params);

  }
}

Register this command in kernel


<?php

namespace App\Console;

use App\Console\Commands\ESInit;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
  /**
   * The Artisan commands provided by your application.
   *
   * @var array
   */
  protected $commands = [
    ESInit::class
  ];

Execute this command to generate mapping


php artisan es:init

Modify model to support full-text search


Using version ^6.1 for laravel/scout
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.

 Problem 1
  - tamayo/laravel-scout-elastic 4.0.0 requires laravel/scout ^5.0 -> satisfiable by laravel/scout[5.0.x-dev].
  - tamayo/laravel-scout-elastic 4.0.0 requires laravel/scout ^5.0 -> satisfiable by laravel/scout[5.0.x-dev].
  - tamayo/laravel-scout-elastic 4.0.0 requires laravel/scout ^5.0 -> satisfiable by laravel/scout[5.0.x-dev].
  - Conclusion: don't install laravel/scout 5.0.x-dev
  - Installation request for tamayo/laravel-scout-elastic ^4.0 -> satisfiable by tamayo/laravel-scout-elastic[4.0.0].


Installation failed, reverting ./composer.json to its original content.
0

Import full-text index information


Using version ^6.1 for laravel/scout
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.

 Problem 1
  - tamayo/laravel-scout-elastic 4.0.0 requires laravel/scout ^5.0 -> satisfiable by laravel/scout[5.0.x-dev].
  - tamayo/laravel-scout-elastic 4.0.0 requires laravel/scout ^5.0 -> satisfiable by laravel/scout[5.0.x-dev].
  - tamayo/laravel-scout-elastic 4.0.0 requires laravel/scout ^5.0 -> satisfiable by laravel/scout[5.0.x-dev].
  - Conclusion: don't install laravel/scout 5.0.x-dev
  - Installation request for tamayo/laravel-scout-elastic ^4.0 -> satisfiable by tamayo/laravel-scout-elastic[4.0.0].


Installation failed, reverting ./composer.json to its original content.
1

Testing Simple Full-Text Indexing


Using version ^6.1 for laravel/scout
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.

 Problem 1
  - tamayo/laravel-scout-elastic 4.0.0 requires laravel/scout ^5.0 -> satisfiable by laravel/scout[5.0.x-dev].
  - tamayo/laravel-scout-elastic 4.0.0 requires laravel/scout ^5.0 -> satisfiable by laravel/scout[5.0.x-dev].
  - tamayo/laravel-scout-elastic 4.0.0 requires laravel/scout ^5.0 -> satisfiable by laravel/scout[5.0.x-dev].
  - Conclusion: don't install laravel/scout 5.0.x-dev
  - Installation request for tamayo/laravel-scout-elastic ^4.0 -> satisfiable by tamayo/laravel-scout-elastic[4.0.0].


Installation failed, reverting ./composer.json to its original content.
2

Related articles: