一.基本概念
- 索引(index) :这个索引就是mysql里数据库的名字,不是数据库里的字段索引,只是巧了,名字一样而已
- 类型(type) :就是你要搜索的表名
- 文档(document):就是表里面的一条记录
- 字段(field):一条记录有很多字段,你要以哪些字段为准,进行搜索
- 模板(template):就是一些搜索配置,用哪些分析器,该模板应用到那个索引
PS:
1. 注意,es的搜索还是以http请求为基础,其实每次搜索,就是发起一次http请求给Elasticsearch引擎,引擎根据索引(就是你已经绑定好的数据库)搜索,然后返回查询结果
2. http://localhost:9200/index/type/document(记录的id) :这个就是标准的一次搜索的url,Elasticsearch启动以后就监听9200端口
二.对于es的一些理解
索引:一旦创建了一个索引,就相当于已经在es里注册好了一个数据库,每次搜索(发起http请求)都会根据url里的索引(哪个数据库)去搜索,至于你换另一个名字去创建索引,那就是另一个全新的索引,不会覆盖掉之前创建好的,说的更通俗一点就是,换了一个索引,就是换了一个你要搜索的数据库,而你去访问哪个索引,其实就是去哪个数据库里面查数据
三.Elasticsearch在laravel使用步骤
- 安装Elasticsearch和ik插件,ik是专门分析中文的插件。(必须先配好java8环境)
1.1下载:<a href="https://github.com/medcl/elasticsearch-rtf,win">https://github.com/medcl/elasticsearch-rtf,win</a>下,下载完成,启动 bin/elasticsearch.bat</li>
解释:rtf是ready to fly的意思,这个相当于增强版的Elasticsearch,里面已经集成了各种插件(ik也在里面,所以不用再下载ik),使用起来非常方便,但是正是因为集成了各种插件,所以启动后占内存很大(2.5g)作用,可以根据需要删除要加载的插件。
安装scout(详情去看官方文档)
2.1 composer require laravel/scout 2.2 在config/app.php 的 providers 数组中添加 Laravel\Scout\ScoutServiceProvider::class 2.3 php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"
给laravel安装scout,scout这个东西,我也解释不很清楚,查了资料,感觉它是一个万能驱动,可以操作各种引擎, 用了它,我们写代码和Elasticsearch打交道。所谓万能驱动就是,驱动其他插件貌似也是用它,Elasticsearch只是其 中一种,类似于:我们写的code 去操作 scout 去操作 es
安装laravel-scout-elastic,这个是scout和es之间的打交道的插件,https://github.com/ErickTamayo/laravel-scout-ela stic
3.1composer require tamayo/laravel-scout-elastic 3.2在config/app.php 的 providers 数组中添加 ScoutEngines\Elasticsearch\ElasticsearchProvider::class 3.3修改scout.php文件: 3.3.1 'driver' => env('SCOUT_DRIVER', 'elasticsearch'), 3.3.2 在最后添加 //配置elasticsearch引擎 'elasticsearch' => [ 'index' => env('ELASTICSEARCH_INDEX', 'laravel54'),//laravel54就是索引的名字,可以随便起 'hosts' => [ env('ELASTICSEARCH_HOST', 'http://127.0.0.1:9200'), ], ]
创建命令
4.1执行命令 php artisan make:command 命令的名字,eg: php artisan make:command ESinit 4.2会在 app\Console\Commands\目录下创建 ESinit.php class ESinit extends Command ``` { /** * The name and signature of the console command. * 这是命令的名字 * @var string */ //protected $signature = 'command:name'; protected $signature = 'es:init';
/** * The console command description. * 命令的描述 * @var string */ //protected $description = 'Command description'; 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() { //coding,待会儿我们要在这里写很多代码 } }
4.3在 app\Console\Kernel.php 里写 ``` protected $commands = [ \App\Console\Commands\ESinit::class ]; ``` 4.4 composer require guzzlehttp/guzzle 4.5 安装guzzlehttp/guzzle成功后,在ESinit.php里的handle()方法里写
//创建template $client = new Client(); $url = config('scout.elasticsearch.hosts')[0].'/_template/tmp'; $client->delete($url);//确定没有这个url /* * 这个模板作用于我要做用的索引 * */ $param = [ 'json'=>[ /* * 这句是取在scout.php(scout是驱动)里我们配置好elasticsearch引擎的 * index项。 * PS:其实都是取数组项,scout本身就是return一个数组, * scout.elasticsearch.index就是取 * scout[elasticsearch][index] * */ 'template'=>config('scout.elasticsearch.index'), 'mappings'=>[ '_default_'=>[ 'dynamic_templates'=>[ [ 'string'=>[ 'match_mapping_type'=>'string',//传进来的是string 'mapping'=>[ 'type'=>'text',//把传进来的string按text(文本)处理 'analyzer'=>'ik_smart',//用ik_smart进行解析(ik是专门解析中的插件) 'fields'=>[ 'keyword'=>[ 'type'=>'keyword' ] ] ] ] ] ] ] ], ], ]; $client->put($url,$param); $this->info('============create template success============'); //创建index $url = config('scout.elasticsearch.hosts')[0].'/'.config('scout.elasticsearch.index'); //$client->delete($url); $param = [ 'json'=>[ 'settings'=>[ 'refresh_interval'=>'5s', 'number_of_shards'=>1, 'number_of_replicas'=>0, ], 'mappings'=>[ '_default_'=>[ '_all'=>[ 'enabled'=>false ] ] ] ] ]; $client->put($url,$param); $this->info('============create index success============');
“`
PS:创建命令的作用:其实就是发送http请求去注册一个索引,命令的核心方法handle()做的事情很显然,就是把配置, 参数,模板都做好,然后发送http请求给es。
5.执行 php artisan es:init命令,出现下边的信息就说明执行成功了
============create template success============
============create index success===============
6.修改你要搜索的model,以PostModel为eg:
6.1 添加特性 6.6.1 在model上边导入use Laravel\Scout\Searchable; 6.6.2 在model 类第一行写 use Searchable;
6.2 重写searchableAs()方法
//return 定义索引里面的type值
public function searchableAs() {
return 'post';
}
6.3 重写toSearchableArray()方法
//定义有哪些字段需要搜索
public function toSearchableArray() {
return [
'title'=>$this->title,
'content'=>$this->content
];
}
6.4导入数据,把数据库已有的数据添加到es中
php artisan scout:import "\App\Model\Post"
ps:执行完这条命令,返回的应该是数据库post表最大的id
- 到这里,所有的工作就做完了,可是该怎么用呢?
在laravel对应url方法里写调用Post::search($query)->get();eg:
public function search(Request $request) {
$this->validate($request,[
'query'=>'required'
]);
$query = $request->input('query');
$posts = Post::search($query)->paginate(2);
return view('face.posts.search',compact('posts','query'));
}
PS:到这里,就可以使用es搜索了,中途肯定有很多坑,环境不一样,我也难以写的很全面,但是有坑就填,这样才能学到东西嘛,祝小伙伴们填坑顺利