安装 ElasticSearch Scout Engine 包
composer require tamayo/laravel-scout-elastic
安装这个包的时候,顺便就会装好 Laravel Scout,发布一下资源
php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"
如果报错了
报错信息如下:
Problem 1
- Installation request for tamayo/laravel-scout-elastic ^4.0 -> satisfiable by tamayo/laravel-scout-elastic[4.0.0].
- tamayo/laravel-scout-elastic 4.0.0 requires laravel/scout ^5.0 -> satisfiable by laravel/scout[5.0.x-dev, v5.0.0, v5.0.1, v5.0.2, v5.0.3] but these conflict with your requirements or minimum-stability.
析报错信息,大概原因就是 要安装的tamayo/laravel-scout-elastic 4.0版本 可满足的laravel/scout版本只有5.0.x-dev, v5.0.0, v5.0.1, v5.0.2, v5.0.3
当前小编安装的laravel/scout为最新版本6.1
看来就是版本互相兼容的问题啦,那就只能对laravel/scout进行降级了
composer require laravel/scout ^5.0.3
5.在app.php中添加对应的Provider
Laravel\Scout\ScoutServiceProvider::class, ScoutEngines\Elasticsearch\ElasticsearchProvider::class,
6.在count.php中配置参数
‘driver‘ => env(‘SCOUT_DRIVER‘, ‘elasticsearch‘),
‘elasticsearch‘ => [
‘index‘ => env(‘ELASTICSEARCH_INDEX‘, ‘laravel5‘),
‘hosts‘ => [
env(‘ELASTICSEARCH_HOST‘, ‘http://127.0.0.1:9200‘),
],
],
7.创建ylaravel的索引和模板
composer require guzzlehttp/guzzle
<?php
namespace App\Console\Commands;
use GuzzleHttp\Client;
use Illuminate\Console\Command;
class ESInit extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = ‘es:init‘;
/**
* The console command description.
*
* @var string
*/
protected $description = ‘init laravel es for post‘;
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$client = new Client();
// 创建模版
$url = config(‘scout.elasticsearch.hosts‘)[0] . ‘/_template/tmp‘;
$client->put($url, [
‘json‘ => [
‘template‘ => config(‘scout.elasticsearch.index‘),
‘settings‘ => [
‘number_of_shards‘ => 1
],
‘mappings‘ => [
‘_default_‘ => [
‘_all‘ => [
‘enabled‘ => true
],
‘dynamic_templates‘ => [
[
‘strings‘ => [
‘match_mapping_type‘ => ‘string‘,
‘mapping‘ => [
‘type‘ => ‘text‘,
‘analyzer‘ => ‘ik_smart‘,
‘ignore_above‘ => 256,
‘fields‘ => [
‘keyword‘ => [
‘type‘ => ‘keyword‘
]
]
]
]
]
]
]
]
]
]);
$this->info("========创建模板成功=======");
$url = config(‘scout.elasticsearch.hosts‘)[0] . ‘/‘ . config(‘scout.elasticsearch.index‘);
$client->put($url, [
‘json‘ => [
‘settings‘ => [
‘refresh_interval‘ => ‘5s‘,
‘number_of_shards‘ => 1,
‘number_of_replicas‘ => 0,
],
‘mappings‘ => [
‘_default_‘ => [
‘_all‘ => [
‘enabled‘ => false
]
]
]
]
]);
$this->info("========创建索引成功=======");
}
}
我的项目中我使用文章数据表来搜索,因此需要修改post.php,也就是posts数据对于的数据模型
use Searchable;
//定义索引里面的type
public function searchableAs()
{
return "post";
}
//定义有哪些字段需要搜索
public function toSearchableArray()
{
return [
‘title‘=>$this->title,
‘content‘=>$this->content,
];
}
使用php artisan命令导入数据
php artisan scout:import "\App\Post"
导入成功之后我们在浏览器地址输栏入:127.0.0.1:9200/laravel54/post/23(laravel54是elasticsearch驱动定义的项目名,post对象的是我项目的post数据模型,23是某条数据的ID )
原文:https://www.cnblogs.com/php-no-2/p/10497347.html