elastic學習筆記

要點

不一樣工具之間版本匹配很重要
由點及面,先實踐起來再學細節的原理和使用php

技術棧

laravel5.5框架+scout組件+elasticsearch6.3.0搜索引擎

輔助

elasticsearch-head 查看集羣數據可視化
中文分詞插件Ik

介紹

laravel是一款現代化的php框架
es是搜索引擎
es-head是管理查看使用es的圖形界面工具
scout是laravel一款優秀的組件html

安裝流程

laravel

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

在es的官網挑選一個合適的版本,建議選擇6.3.0,以便配套使用IK和ES-head。laravel

# 下載
https://www.elastic.co/downloads/past-releases

IK

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

es-head

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: "*"

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';
   
 
    /******字段相關*******/
    #定義主鍵字段名,默認是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...

相關文章
相關標籤/搜索