十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
这篇文章主要讲解了“怎么在laravel中使用elaticsearch”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“怎么在laravel中使用elaticsearch”吧!
公司主营业务:成都做网站、成都网站建设、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。创新互联是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。创新互联推出安化免费做网站回馈大家。
安装相关扩展包
guzzlehttp/guzzle
elasticsearch/elasticsearch
laravel/scout
babenkoivan/scout-elasticsearch-driver
predis/predis 数据量大需要使用队列同步、拉取数据时安装
composer require guzzlehttp/guzzle
client = new Client(['verify' => false, 'timeout' => 0,]); } /** * 发送 get 请求 * @param $url * @param array $aQueryParam * @param string $isDecode * [@return](https://learnku.com/users/31554) mixed * @throws \GuzzleHttp\Exception\GuzzleException */ public function get($url, $aQueryParam = [], $isDecode = true) { $response = $this->client->request('GET', $url, [ 'query' => $aQueryParam ]); if($isDecode){ return \GuzzleHttp\json_decode($response->getbody()->getContents(), true); } return $response->getbody()->getContents(); } /** * 发送 post 请求 * @param $url * @param array $aParam * @param string $type * @param string $isDecode * [@return](https://learnku.com/users/31554) mixed * @throws \GuzzleHttp\Exception\GuzzleException */ public function post($url, $aParam = [], $type = 'form_params', $isDecode = true) { $aOptions = []; // Sending application/x-www-form-urlencoded POST if ($type == 'form_params') { $aOptions['form_params'] = $aParam; } // upload JSON data if ($type == 'json') { $aOptions['json'] = $aParam; } $response = $this->client->request('POST', $url, $aOptions); if($isDecode){ return \GuzzleHttp\json_decode($response->getbody()->getContents(), true); } return $response->getbody()->getContents(); } /** * 发送 put 请求 * @param $url * @param array $aParam * @param string $type * @param string $isDecode * [@return](https://learnku.com/users/31554) mixed * @throws \GuzzleHttp\Exception\GuzzleException */ public function put($url, $aParam = [], $type = 'form_params', $isDecode = true) { $aOptions = []; // Sending application/x-www-form-urlencoded POST if ($type == 'form_params') { $aOptions['form_params'] = $aParam; } // upload JSON data if ($type == 'json') { $aOptions['json'] = $aParam; } $response = $this->client->request('PUT', $url, $aOptions); if($isDecode){ return \GuzzleHttp\json_decode($response->getbody()->getContents(), true); } return $response->getbody()->getContents(); } /** * 保存远程文件到本地 * 跟随第三方服务器url重定向 * @param $url * [@return](https://learnku.com/users/31554) bool|string */ public function getRemoteFile($url) { $jar = new CookieJar(); $aOptions = ['cookies' => $jar]; $response = $this->client->request('GET', $url, $aOptions); return $response->getBody()->getContents(); }}
composer require elasticsearch/elasticsearch
composer require laravel/scout php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"
composer require babenkoivan/scout-elasticsearch-driver php artisan vendor:publish --provider="ScoutElastic\ScoutElasticServiceProvider"
scout 服务配置,在 env 中增加配置项
// 驱动的host,若需账密:http://es_username:password@127.0.0.1:9200SCOUT_ELASTIC_HOST=elasticsearch:9200// 驱动SCOUT_DRIVER=elastic// 队列配置,数据量大时建议开启SCOUT_QUEUE=true
composer require predis/predis
这里以 artisan 命令的方式配置 生成命令文件
php artisan make:command EsInit
http = new HttpService(); } /** * Execute the console command. * * [@return](https://learnku.com/users/31554) mixed */ public function handle() { $this->createTemplate(); } protected function createTemplate() { $aData = [ /* * 这句是取在scout.php(scout是驱动)里我们配置好elasticsearch引擎的index项。 * PS:其实都是取数组项,scout本身就是return一个数组, * scout.elasticsearch.index就是取 * scout[elasticsearch][index] * */ 'template'=>config('scout.elasticsearch.index'), 'mappings'=>[ 'articles' => [ 'properties' => [ 'title' => [ 'type' => 'text' ], 'content' => [ 'type' => 'text' ], 'created_at' => [ 'format' => 'yy-MM-dd HHss', 'type' => 'date' ], 'updated_at' => [ 'format' => 'yy-MM-dd HHss', 'type' => 'date' ] ] ] ], ]; $url = config('scout.elasticsearch.hosts')[0] . '/' . '_template/rtf'; $this->http->put($url,$aData,'json'); }}
php artisan make:model Models/Article
Elasticsearch\ArticleIndexConfigurator.php
[ 'analyzer' => [ 'es_std' => [ 'type' => 'standard', 'stopwords' => '_spanish_' ] ] ] ];}
Elasticsearch\SearchRules\ArticleRule.php
[ 'title' => [ 'type' => 'unified', ], 'content' => [ 'type' => 'unified', ], ] ]; } //进行 match 搜索,会分词 public function buildQueryPayload() { $query = $this->builder->query; return [ 'must' => [ 'query_string' => [ 'query' => $query, ], ], ]; }}
class Article extends Model{ protected $indexConfigurator = ArticleIndexConfigurator::class; use Searchable; /** * 检索规则 * @var string[] */ protected $searchRules = [ ArticleRule::class ]; // 设置模型字段的映射关系 protected $mapping = [ 'properties' => [ 'id' => [ 'type' => 'integer', ], 'title' => [ 'type' => 'text', 'analyzer' => 'ik_max_word', 'search_analyzer' => 'ik_max_word', 'index_options' => 'offsets', 'store' => true ], 'content' => [ 'type' => 'text', 'analyzer' => 'ik_max_word', 'search_analyzer' => 'ik_max_word', 'index_options' => 'offsets', 'store' => true ], 'number' => [ 'type' => 'integer', ], ], ]; /** * 设置 es 检索返回的字段 * [@return](https://learnku.com/users/31554) array */ public function toSearchableArray() { return [ 'id' => $this->id, 'title' => $this->title, 'content' => $this->content, ]; }}
生成 elatic Template 类似 MySQL 表结构
php artisan es:init
更新 elatic 类型映射
php artisan elastic:update-mapping "App\Models\Article"
数据库数据导入 elatic
php artisan scout:import "App\Models\Article"
PS: 其他命令
清空 elatic 数据
php artisan scout:flush "App\Models\Article"
$query = Article::search('二胡') ->paginateRaw(3,'article',1); dd($query->items()['hits']);
感谢各位的阅读,以上就是“怎么在laravel中使用elaticsearch”的内容了,经过本文的学习后,相信大家对怎么在laravel中使用elaticsearch这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是创新互联,小编将为大家推送更多相关知识点的文章,欢迎关注!