laravel 5.5 バージョン
composer require tamayo/laravel-scout-elastic
composer require laravel/scout
ログイン後にコピー
Composer require laravel/scout がエラーを報告する場合 composer require tamayo/laravel-scout-elastic 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.
ログイン後にコピー
次に、コマンド 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.
ログイン後にコピー
composer require laravel/scout ^5.0
ログイン後にコピー
を使用して設定ファイル (config/app) を変更します.php) を追加し、次の内容を追加します。 2 つのプロバイダー composer require laravel/scout ^5.0
ログイン後にコピー
'providers' => [
//es search 加上以下内容
Laravel\Scout\ScoutServiceProvider::class,
ScoutEngines\Elasticsearch\ElasticsearchProvider::class,
]
ログイン後にコピー
が追加され、コマンドを実行して構成ファイルを生成します。'providers' => [ //es search 加上以下内容 Laravel\Scout\ScoutServiceProvider::class, ScoutEngines\Elasticsearch\ElasticsearchProvider::class, ]
ログイン後にコピー
php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"
ログイン後にコピー
Modify config/scout.phpphp artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"
ログイン後にコピー
'driver' => env('SCOUT_DRIVER', 'elasticsearch'),
'elasticsearch' => [
'index' => env('ELASTICSEARCH_INDEX', '你的Index名字'),
'hosts' => [
env('ELASTICSEARCH_HOST', ''),
],
],
ログイン後にコピー
Configure .env の ES アカウント:password@connection 'driver' => env('SCOUT_DRIVER', 'elasticsearch'), 'elasticsearch' => [ 'index' => env('ELASTICSEARCH_INDEX', '你的Index名字'), 'hosts' => [ env('ELASTICSEARCH_HOST', ''), ], ],
ログイン後にコピー
ELASTICSEARCH_HOST=elastic:密码@你的域名.com:9200
ログイン後にコピー
コマンド ライン ファイルを作成してマッピングを生成し、app/Console/Commands に移動しますELASTICSEARCH_HOST=elastic:密码@你的域名.com:9200
ログイン後にコピー
<?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() { //创建template
$client = new Client(['auth'=>['elastic', 'yourPassword']]);
$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);
// 创建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);
}
}
ログイン後にコピー
このコマンドをカーネルに登録します<?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() { //创建template $client = new Client(['auth'=>['elastic', 'yourPassword']]); $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); // 创建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); } }
ログイン後にコピー
<?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
];
ログイン後にコピー
このコマンドを実行してマッピングを生成します<?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 ];
ログイン後にコピー
php artisan es:init
ログイン後にコピー
全文検索をサポートするようにモデルを変更しますphp artisan es:init
ログイン後にコピー
<?php
namespace App\ActivityNews\Model;
use App\Model\Category;
use App\Star\Model\Star;
use Illuminate\Database\Eloquent\Model;
use Laravel\Scout\Searchable;
class ActivityNews extends Model
{
use Searchable;
protected $table = 'activity_news';
protected $fillable = [
'type_id',
'category_id',
'title',
'sub_title',
'thumb',
'intro',
'star_id',
'start_at',
'end_at',
'content',
'video_url',
'status',
'is_open',
'is_top',
'rank',
];
public function star()
{
return $this->hasOne(Star::class, 'id', 'star_id');
}
public function category()
{
return $this->hasOne(Category::class, 'id', 'category_id');
}
public static function getActivityIdByName($name)
{
return self::select('id')
->where([
['status', '=', 1],
['type_id', '=', 2],
['title', 'like', '%' . $name . '%']
])->get()->pluck('id');
}
}
ログイン後にコピー
全文インデックス情報をインポートします<?php namespace App\ActivityNews\Model; use App\Model\Category; use App\Star\Model\Star; use Illuminate\Database\Eloquent\Model; use Laravel\Scout\Searchable; class ActivityNews extends Model { use Searchable; protected $table = 'activity_news'; protected $fillable = [ 'type_id', 'category_id', 'title', 'sub_title', 'thumb', 'intro', 'star_id', 'start_at', 'end_at', 'content', 'video_url', 'status', 'is_open', 'is_top', 'rank', ]; public function star() { return $this->hasOne(Star::class, 'id', 'star_id'); } public function category() { return $this->hasOne(Category::class, 'id', 'category_id'); } public static function getActivityIdByName($name) { return self::select('id') ->where([ ['status', '=', 1], ['type_id', '=', 2], ['title', 'like', '%' . $name . '%'] ])->get()->pluck('id'); } }
ログイン後にコピー
php artisan scout:import "App\ActivityNews\Model\ActivityNews"
ログイン後にコピー
単純な全文インデックスをテストしますphp artisan scout:import "App\ActivityNews\Model\ActivityNews"
ログイン後にコピー
php artisan tinker
>>> App\ActivityNews\Model\ActivityNews::search('略懂皮毛')->get();
ログイン後にコピー
php artisan tinker >>> App\ActivityNews\Model\ActivityNews::search('略懂皮毛')->get();
ログイン後にコピー