elastic学习笔记
要點
不同工具之間版本匹配很重要
由點及面,先實踐起來再學細節的原理和使用
技術棧
laravel5.5框架+scout組件+elasticsearch6.3.0搜索引擎輔助
elasticsearch-head 查看集群數據可視化 中文分詞插件Ik介紹
laravel是一款現代化的php框架
es是搜索引擎
es-head是管理查看使用es的圖形界面工具
scout是laravel一款優秀的組件
安裝流程
laravel
laravel安裝器安裝:
laravel new larasearch配置env文件:
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 就可以看到項目首頁了。
es
在es的官網挑選一個合適的版本,建議選擇6.3.0,以便配套使用IK和ES-head。
# 下載 https://www.elastic.co/downloads/past-releasesIK
1.直接plugin命令安裝
./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.3.0/elasticsearch-analysis-ik-6.3.0.zip2.配置修改ik的版本適應es6.3.1,修改文件plugin-descriptor.properties,config文件夾下的配置文件主要是IK本身暫時不需要修改,這個properties文件主要是和es交互,修改其es版本和jdk版本號
# 修改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.1es-head
head是基于node開發的,所以需要先安裝node
node下載地址:http://cdn.npm.taobao.org/dis...
在電腦任意一個目錄下(不要在elasticsearch目錄里面),執行一下命令,
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: "*"scout
通過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';/******字段相關*******/#定義主鍵字段名,默認是idprotected $primaryKey = 'id';#定義字段白名單,允許操作表中的哪些字段// protected $fillable = ['ques','name'];#定義字段黑名單,不允許操作表中哪些字段protected $guarded = [];//1、使用model::create([])等方法直接對orm對象操作使,必須定義$guarded或者$fillable//2、使用$m = new model();然后$m->save()的方式不需要定義//3、簡便的方式就是定義$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: 2000All [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...】
原文地址:https://segmentfault.com/a/1190000016468707
轉載于:https://www.cnblogs.com/lalalagq/p/9970068.html
總結
以上是生活随笔為你收集整理的elastic学习笔记的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用LocalDateTime计算两个时
- 下一篇: 实验 4 [bx]和 loop 的使用