參考文檔:
運行環境:
php7
laravel框架 5.5
調用:
//執行命令,加載laravel-elastic驅動
# composer require babenkoivan/scout-elasticsearch-driver
//修改laravel配置文件(如果 框架版本小於5.4)
//在config/app.php最後增加
'providers' => [
Laravel\Scout\ScoutServiceProvider::class,
ScoutElastic\ScoutElasticServiceProvider::class,
]
//執行命令,發佈(命令生成elastic配置文件)
# php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"
# php artisan vendor:publish --provider="ScoutElastic\ScoutElasticServiceProvider"
//修改elastic配置文件 config/sout.php
//(兩處地方需要改)
'driver' => env('SCOUT_DRIVER', 'elastic'),
'elastic' => [
'index' => env('ELASTICSEARCH_INDEX', 'my_elastic'),
'hosts' => [
env('ELASTICSEARCH_HOST', 'http://(服務器elastic的ip):9200'),
],
],
//修改 config/scout_elastic.php
return [
'client' => [
'hosts' => [
env('SCOUT_ELASTIC_HOST', '/(服務器elastic的ip):9200')
]
],
'update_mapping' => env('SCOUT_ELASTIC_UPDATE_MAPPING', true),
'indexer' => env('SCOUT_ELASTIC_INDEXER', 'single'),
'document_refresh' => env('SCOUT_ELASTIC_DOCUMENT_REFRESH')
];
//修改環境配置文件 根目錄下的 .env 文件,增加
SCOUT_DRIVER=elastic
SCOUT_ELASTIC_HOST=服務器elastic的ip:9200
ELASTICSEARCH_HOST=http://服務器elastic的ip:9200
ELASTICSEARCH_INDEX=my_elastic
!!!/* 注意了 上面配置的 my_elastic 相當於數據庫的名字 */!!!
//索引就是數據庫名嘛
//生成索引文件 MyElasticIndexConfigurator 爲 數據庫名字IndexConfigurator
php artisan make:index-configurator MyElasticIndexConfigurator
//其中找到 文件 MyElasticIndexConfigurator
protected $name = 'my_index'; //數據庫名字
protected $settings = [
'refresh_interval' => '5s',
'number_of_shards' => 1,
'number_of_replicas' => 0,
];
protected $defaultMapping = [
];
//新增 搜索的規則文件夾 App\Search\Rules
//新增 搜索的數據表文件夾 App\Search\Models
//向服務器請求,新增索引(新增數據庫意思)
//其中MyElasticIndexConfigurator 爲 剛剛上面創建的索引文件,注意命名空間App
php artisan elastic:create-index App\\MyElasticIndexConfigurator
//生成一個elasticsearch專用的 Model,如 用戶表文件
//注意--index選項 意思是,把UserModel這個表 放到 索引 --index指定的文件,即 數據表存放到數據庫下
//執行以下命令
php artisan make:searchable-model Search\\Models\\UserModel --index-configurator=MyIndexConfigurator
//打開上面創建的 UserModel
//看下指定的索引(數據庫)是否正確
protected $indexConfigurator = \App\LogisticsArticleIndexConfigurator::class;
//指定數據庫的表名
protected $table = 'users';
//UserModel完整如下
//其中規則文件生成 下一步添加
namespace App\Search\Models;
use App\Search\Rules\UserRule;
use ScoutElastic\Searchable;
use Illuminate\Database\Eloquent\Model;
class UserModel extends Model
{
use Searchable;
//表名
protected $table = 'users';
protected $indexConfigurator = \App\MyElasticIndexConfigurator::class;
/**
* 索引類型 類似 mysql的表
* @return string
*/
public function searchableAs()
{
return 'users';
}
//搜索規則,指定規則文件
protected $searchRules = [
UserRule::class
];
protected $mapping = [
'_source' => [
'enabled' => true
],
'properties' => [ //⽂文檔類型設置(相當於mysql的數據類型)
'id' => [
'type' => 'integer', // //類型 string、integer、float、double、boolean、date,text,keyword
//'index'=> 'not_analyzed',//索引是否精確值 analyzed not_analyzed
],
'content' => [
'type' => 'text', // 字段類型爲全⽂文檢索,如果需要關鍵字,則修改爲keyword,注意keyword字段爲整體查詢,不不能作爲模糊搜索
"analyzer" => "ik_max_word",
"search_analyzer" => "ik_max_word",
]
]
];
//當查詢的時候,默認返回的內容
public function toSearchableArray()
{
return [
'id'=>$this->id,
'content'=>$this->content
];
//return array_only($this->toArray(), ['id', 'name', 'email']);
}
}
//執行命令 搜索規則文件添加
# php artisan make:search-rule App\\Search\\Rules\\UserRule
//打開創建的規則文件,內容如下
//規則怎麼配置,看看參考文檔的 laravel-elastic有說明
public function buildHighlightPayload()
{
return [
'fields' => [
// 'title' => [
// 'type' => 'plain'
// ],
'content' => [
'type' => 'plain'
]
]
];
}
/**
* @inheritdoc
*/
public function buildQueryPayload()
{
return [
'must' => [
'match' => [
// 'title' => $this->builder->query,
'content' => $this->builder->query
]
]
];
}
//下一步,把userModel對應的數據表數據 映射(插入到)elastic服務器上
//執行命令
//userModel更改了,則需要更新下
# php artisan elastic:update-mapping App\\Search\\Models\\UserModel
//把數據庫的數據 插入映射到 elastic服務器上
# php artisan scout:import "App\Search\Models\UserModel"
//說明:如果 UserModel的 properties 既字段的屬性類型更改了,那麼 執行上面的兩個命令將會失敗
//因此需要 先執行刪除索引,創建索引 這兩個命令,重複上面的步驟。
//最後控制器 調用elasticearch的model進行查詢
\App\Search\Models\UserModel::search('查詢的字眼')->get();