不一样工具之间版本匹配很重要
由点及面,先实践起来再学细节的原理和使用php
laravel5.5框架+scout组件+elasticsearch6.3.0搜索引擎
elasticsearch-head 查看集群数据可视化 中文分词插件Ik
laravel是一款现代化的php框架
es是搜索引擎
es-head是管理查看使用es的图形界面工具
scout是laravel一款优秀的组件html
laravel安装器安装:java
laravel new larasearch
配置env文件:node
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=julyedu DB_USERNAME=root DB_PASSWORD=123456
这时php artisan命令启动,访问127.0.0.1:8000 就能够看到项目首页了。mysql
在es的官网挑选一个合适的版本,建议选择6.3.0,以便配套使用IK和ES-head。laravel
# 下载 https://www.elastic.co/downloads/past-releases
1.直接plugin命令安装git
./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.3.0/elasticsearch-analysis-ik-6.3.0.zip
2.配置修改ik的版本适应es6.3.1,修改文件plugin-descriptor.properties,config文件夹下的配置文件主要是IK自己暂时不须要修改,这个properties文件主要是和es交互,修改其es版本和jdk版本号es6
# 修改elasticsearch-head/plugin-descriptor.properties文件 description=head - A web front end for an elastic search cluster version=6.3.1 site=true name=analysis-ik classname=org.elasticsearch.plugin.analysis.ik.AnalysisIkPlugin java.version=1.8 elasticsearch.version=6.3.1
head是基于node开发的,因此须要先安装node
node下载地址:http://cdn.npm.taobao.org/dis...github
在电脑任意一个目录下(不要在elasticsearch目录里面),执行一下命令,web
git clone https://github.com/mobz/elasticsearch-head.git cd elasticsearch-head/ npm install
为了es-head能够访问es,因此须要配置跨域:
修改两个地方:
#elasticsearch-headGruntfile.js connect: { server: { options: { port: 9100, hostname: '*', base: '.', keepalive: true } } } #elasticsearch-5.6.0configelasticsearch.yml http.cors.enabled: true http.cors.allow-origin: "*"
经过composer安装依赖包
composer require laravel/scout composer require tamayo/laravel-scout-elastic
基本配置
在config/app.php文件中的providers数组中加入服务提供者
// config/app.php 'providers' => [ // ... Laravel\Scout\ScoutServiceProvider::class, // ... ScoutEngines\Elasticsearch\ElasticsearchProvider::class, ],
使用如下命令生成scout配置文件
php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"
在config/scout.php中加入elasticsearch的配置
'elasticsearch' => [ 'index' => env('ELASTICSEARCH_INDEX', 'laravel'), 'hosts' => [ env('ELASTICSEARCH_HOST', 'http://localhost:9200'), ], ],
而后咱们打开.env文件,加入scout和elasticsearch的配置
# scout配置 SCOUT_DRIVER=elasticsearch SCOUT_PREFIX= # elasticsearch 配置 ELASTICSEARCH_INDEX=esdemo # elasticsearch 地址 ELASTICSEARCH_HOST=http://172.30.6.1:9200
laravel scout中文文档地址:https://laravel-china.org/doc...
es中文文档地址:https://www.elastic.co/guide/...
es6.3.0地址:https://www.elastic.co/downlo...
IK github地址:https://github.com/medcl/elas...
启动es
./bin/elasticsearch
地址
http://127.0.0.1:9200/
启动es-head
npm run start
地址
http://127.0.0.1:9100
启动laravel项目
php artisan serve
地址
http://127.0.0.1:8000/es/s?page=1
建立模型app/Ques.php,为方便后续测试,请先建表和填充数据,能够手动使用sql语句添加数据,也使用laravel自动的数据迁移和填充。
<?php namespace App; use Illuminate\Database\Eloquent\Model; use Laravel\Scout\Searchable; /** * 学生模型 */ class Ques extends Model { use Searchable; //定义关联的表名,不定义的话默认此模型关联的表为 模型名s (users) protected $table = 'aws_ques_tb_0'; /******字段相关*******/ #定义主键字段名,默认是id protected $primaryKey = 'id'; #定义字段白名单,容许操做表中的哪些字段 // protected $fillable = ['ques','name']; #定义字段黑名单,不容许操做表中哪些字段 protected $guarded = []; //一、使用model::create([])等方法直接对orm对象操做使,必须定义$guarded或者$fillable //二、使用$m = new model();而后$m->save()的方式不须要定义 //三、简便的方式就是定义$fillable = []; #定义隐藏的字段 protected $hidden = []; /** * 索引名称 * * @return string */ public function searchableAs() { return 'ques_index'; } /** * 索引名称 * * @return string */ public function searchableAs() { return 'Quess_index'; } /** * 可搜索的数据索引 * * @return array */ public function toSearchableArray() { $array = $this->toArray(); // Customize array... return $array; } }
php artisan scout:import "App\Ques"
Imported [App\Ques] models up to ID: 500 Imported [App\Ques] models up to ID: 1000 Imported [App\Ques] models up to ID: 1500 Imported [App\Ques] models up to ID: 2000 All [App\Ques] records have been imported.
咱们访问es,是否是已经有了刚刚导入的Quess_index索引数据。
http://172.30.6.1:9200/esdemo/Ques_index/_search
在route/web.php中写个demo,试试看;
Route::get('/search/{content}', function ($content) { //直接输出数组data,限制1000条 // $res = App\Ques::search($content)->take(1000)->get()->toArray(); // 分页请求 http://127.0.0.1:8000/es/机器学习?page=1 $res = App\Ques::search($content)->paginate(100)->toArray(); dd($res); });
输出:
array:12 [▼ "current_page" => 1 "data" => array:9 [▼ 0 => array:9 [▼ "id" => 922 "ques" => "哪些机器学习算法不须要作归一化处理?" "analysis" => """ 几率模型不须要归一化,由于它们不关心变量的值,而是关心变量的分布和变量之间的条件几率,如决策树、rf。而像adaboost、svm、lr、KNN、KMeans之类的最优化问题就须要归一化。\r\n 我理解归一化和标准化主要是为了使计算更方便 好比两个变量的量纲不一样 可能一个的数值远大于另外一个那么他们同时做为变量的时候 可能会形成数值计算的问题,好比说求矩阵的逆可能很不精确 或者梯度降低法的收敛比较困难,还有若是须要计算欧式距离的话可能 量纲也须要调整 因此我估计lr 和 knn 保准话一下应该有好处。至于其余的算 ▶ 通常我习惯说树形模型,这里说的几率模型多是差很少的意思。引用自@寒小阳 """ "type_id" => 3 "diff" => 0 "isdelete" => 1 "created_time" => "2017-12-10 18:57:13" "update_time" => "0000-00-00 00:00:00" "is_show" => 1 ] 1 => array:9 [▶] 2 => array:9 [▶] 3 => array:9 [▶] 4 => array:9 [▶] 5 => array:9 [▶] 6 => array:9 [▶] 7 => array:9 [▶] 8 => array:9 [▶] ] "first_page_url" => "http://127.0.0.1:8000/search/%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0?query=%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0&page=1" "from" => 1 "last_page" => 1 "last_page_url" => "http://127.0.0.1:8000/search/%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0?query=%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0&page=1" "next_page_url" => null "path" => "http://127.0.0.1:8000/search/%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0" "per_page" => 100 "prev_page_url" => null "to" => 9 "total" => 9 ]
PHP使用elasticsearch搜索安装及分词方法【https://segmentfault.com/a/11...】
Laravel中利用Scout集成Elasticsearch搜索引擎【https://segmentfault.com/a/11...】
全文搜索引擎 Elasticsearch 入门教程【http://www.ruanyifeng.com/blo...】
laravel使用ElasticSearch进行搜索【https://blog.csdn.net/lingche...】
elasticsearch6.3.1+IK插件安装部署全攻略【https://blog.csdn.net/superhe...】