一、安装环境
1、elasticsearch需要java环境。所以先在java官网上下载java来安装java环境
2、下载elasticsearch在java环境中的运行包,该包实现搜索的索引等
下载地址:
https://github.com/medcl/elasticsearch-rtf
运行:
Mac/Linux:
cd elasticsearch/bin
./elasticsearch -d
sudo -u ops ES_JAVA_OPTS="-Xms2024m -Xmx2024m" ./bin/elasticsearch -d
Windows:
cd elasticsearch/bin
elasticsearch.bat
插件比较多,不用的插件可以删除
留下analysis-ik插件即可
是否启动可在logs/elasticsearch.log中查看启动的ip端口,默认9200和9300
或者在浏览器127.0.0.1:9200出现json可以说明安装成功
三、安装laravel使用的elasticsearch所需要的包
1、laravel搜索驱动(可以随时更换驱动)
可以根据laravel的文档安装修改
composer require laravel/scout
你需要将 ScoutServiceProvider 添加到你的 config/app.php 配置文件的 providers 数组中:
Laravel\Scout\ScoutServiceProvider::class,
注册好 Scout 的服务提供者之后,你可以使用 vendor:publish Artisan 命令生成 Scout 的配置文件。这个命令会在你的 config 目录下生成 scout.php 配置文件:
php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"
2、安装Laravel Scout Elasticsearch Driver
https://github.com/ErickTamayo/laravel-scout-elastic
下载:composer require tamayo/laravel-scout-elastic
增加:
'providers' => [
...
Laravel\Scout\ScoutServiceProvider::class,
...
ScoutEngines\Elasticsearch\ElasticsearchProvider::class,
],
四、修改配置scout.php
<?php
return [
'driver' => env('SCOUT_DRIVER', 'elasticsearch'),
'queue' => env('SCOUT_QUEUE', false),
'chunk' => [
'searchable' => 500,
'unsearchable' => 500,
],
'algolia' => [
'id' => env('ALGOLIA_APP_ID', ''),
'secret' => env('ALGOLIA_SECRET', ''),
],
//增加
'elasticsearch' => [
'index' => env('ELASTICSEARCH_INDEX', 'laravel'),
//这个laravel代表elasticsearch的索引,相当于你的数据库
'hosts' => [
env('ELASTICSEARCH_HOST', 'http://127.0.0.1:9200'),
],
//host后面是ip:port
],
];
五、Laravel自定义命令行
初始化ES
php artisan make:command ESint
安装GuzzleHttp
在Console中Commands中编写Esint
composer require guzzlehttp/guzzle
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use GuzzleHttp\Client;
class ESInit extends Command
{
//php artisan make:command ESInit
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'es:init';
/**
* The console command description.
*
* @var string
*/
protected $description = 'init es';
/**
* 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->delete($url);
$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->delete($url);
$client->put($url, [
'json' => [
'settings' => [
'refresh_interval' => '5s',//更新时间
'number_of_shards' => 1,
'number_of_replicas' => 0,
],
'mappings' => [
'_default_' => [
'_all' => [
'enabled' => false
]
]
]
]
]);
$this->info('#############创建索引成功#############');
}
}
Kernel 中注册
<?php
namespace App\Console;
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 = [
//挂载新的命令
\App\Console\Commands\ESInit::class
];
/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
// $schedule->command('inspire')
// ->hourly();
}
/**
* Register the Closure based commands for the application.
*
* @return void
*/
protected function commands()
{
require base_path('routes/console.php');
}
}
六、导入数据
在你需要搜索的模型里面加入
<?php
namespace App\Http\Model;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Laravel\Scout\Searchable;
class Article extends Model
{
//需要引入
use Searchable;
/**
* 得到该模型索引的名称。相当于表
*
* @return string
*/
public function searchableAs()
{
return 'article';
}
//定义哪些字段需要索引
public function toSearchableArray()
{
return [
'title' => $this->title,
'content' => $this->content,
];
}
}
导入数据:
php artisan scout:import "带上命名空间的模型名"
可以在浏览器输入127.0.0.1:9200/laravel/article/id来查看
七、渲染搜索页面
在search方法中
$query就是搜索的字段,如:
$query = request('query');
$articles = Article::search($query)->paginate(6);