laravel 下 elasticsearch/algolia 全文搜索 使用案例 | laravel china 社区-江南app体育官方入口

江南app体育官方入口官网网址:https://github.com/medcl/elasticsearch-rtf

windows环境下,进入安装目录。点击elasticsearch.bat即可。
如果是linux环境,内存要大一点.

1.下载:wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.5.1.zip
2.解压:unzip elasticsearch-5.5.1.zip
3.进入目录:cd elasticsearch-5.5.1/
4.前台启动: ./bin/elasticsearch
5.后台启动: ./bin/elasticsearch -d
6.检查启动: curl localhost:9200

如图既安装成功
laravel 融合 elasticsearch 在个人博客中使用

注意:elasticsearch 依赖jdk环境,具体环境自己网上安装查看,这里不详述

安装scout

composer require laravel/scout

安装elasticsearch

composer require tamayo/laravel-scout-elastic

安装guzzlehttp包:

composer require guzzlehttp/guzzle

在config/app.php的providers 数组添加:

laravel\scout\scoutserviceprovider::class,
scoutengines\elasticsearch\elasticsearchprovider::class,

发布配置文件:

php artisan vendor:publish --provider="laravel\scout\scoutserviceprovider"

发布生成的config/scout.php文件添加

 'driver' => env('scout_driver', 'elasticsearch'),
    'elasticsearch' => [
        'index' => env('elasticsearch_index', 'laravel'),//索引名称
        'hosts' => [
            env('elasticsearch_host', 'http://127.0.0.1:9200'),
        ],
    ],
php artisan make:command esinit

具体实现代码如下:

namespace app\console\commands;
use guzzlehttp\client;
use illuminate\console\command;
class esinit extends command
{
    /**
     * the name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'es:init';
    /**
     * the console command description.
     *
     * @var string
     */
    protected $description = 'init laravel';
    /**
     * create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }
    /**
     * execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $client = new client();
        $this->createtemplate($client);
        $this->createindex($client);
    }
//创建索引
    protected function createindex($client)
    {
        $url = config('scout.elasticsearch.hosts')[0].'/'.config('scout.elasticsearch.index');
        $client->put($url, [
            'json' => [
                'settings' => [
                    'refresh_interval' => '5s',
                    'number_of_shards' => 1,
                    'number_of_replicas' => 0,
                ],
                'mappings' => [
                    '_default_' => [
                        '_all' => [
                            'enabled' => false
                        ]
                    ]
                ]
            ]
        ]);
    }
//创建模板
    protected function createtemplate($client)
    {
        $url = config('scout.elasticsearch.hosts')[0] .'/_template/rtf';
        $client->put($url, [
            'json' => [
                'template' => '*',
                'settings' => [
                    'number_of_shards' => 1
                ],
                'mappings' => [
                    '_default_' => [
                        '_all' => [
                            'enabled' => true
                        ],
                        'dynamic_templates' => [
                            [
                                'strings' => [
                                    'match_mapping_type' => 'string',
                                    'mapping' => [
                                        'type' => 'text',
                                        'analyzer' => 'ik_smart',
                                        'ignore_above' => 256,
                                        'fields' => [
                                            'keyword' => [
                                                'type' => 'keyword'
                                            ]
                                        ]
                                    ]
                                ]
                            ]
                        ]
                    ]
                ]
            ]
        ]);
    }
}

初始化脚本

php artisan es:init
namespace app\models;
use laravel\scout\searchable;
class article extends basemodel
{
    use searchable;
    protected $guarded = [];
    protected $table = 'article';
    public function tosearchablearray()
    {
        return [
            'title' => $this->title,
            'content' => $this->content
        ];
    }
}
namespace app\http\controllers\web;
use app\http\controllers\controller;
use app\models\article;
use illuminate\http\request;
class indexcontroller extends controller
{
    public function search(request $request)
    {
        $params=$request->keyword;
        $article= article::search($params)->paginate();
        return $this->output($article, '请求成功', status_ok);
    }
}
php artisan scout:import "app\models\article"

命令完成会有如下提示,同时es里面也会将数据库指定字段写入
laravel 融合 elasticsearch 在个人博客中使用

laravel 融合 elasticsearch 在个人博客中使用

注:es可视化工具自行百度安装
可使用kibana:

laravel 融合 elasticsearch 在个人博客中使用

algolia是法国初创公司为你提供毫秒级的数据库实时搜索服务。在这里我也配置实验了下,具体没有压测,有兴趣的朋友可以尝试下,这里只写简单的使用。具体elsaticsearch和algolia如何选择,看业务需求具体来选择
在项目env文件添加

scout_driver=algolia
scout_prefix=
algolia_app_id=xxxxxxx
algolia_secret=xxxxxxxxxxx

这里的appid和secret需要自己注册获取
江南app体育官方入口官网网址:
注册成功后;

laravel 融合 elasticsearch 在个人博客中使用

然后执行命令

php artisan scout:import "app\models\article"

执行完会发现数据已经写到后台了

laravel 融合 elasticsearch 在个人博客中使用

然后按照之前配置好的路由去访问

laravel 融合 elasticsearch 在个人博客中使用

elasticsearch作为一款强大的开源分布式搜索与数据分析引擎,可快速从海量数据总找到相关信息,近年来搜索dbranking一直位列第一。还被广泛应用于大数据近实时分析,包括日志分析,指标监控,信息安全等等,国内很多公司都在使用,腾讯,滴滴,小米,饿了么,今日头条等。所以这里面要学的东西还是很多很多啊,具体数据如何分片,索引如何划分,map设置,analysis具体细节,最佳数据模型构建,集群扩展,参数配置,性能调优,等等等,都是我们需要去掌握的,前路漫漫其修远兮啊,加油!

本作品采用《cc 协议》,转载必须注明作者和本文链接
cfun
本帖由系统于 5年前 自动加精
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
从零开始带你一步步开发一个 go 博客项目,让你在最短的时间内学会使用 go 进行编码。项目结构很大程度上参考了 laravel。
讨论数量: 11

博客用es还是太重了,我现在用的tntsearch

5年前
(楼主) 5年前

请问学长,

刚才 进行到这一步报错了,该怎么解决呢?非常谢谢!
laravel

另一个问题,我的项目是一些视频,音频,文章表,每个表大概1万条数据,全网查询时,需要对这些表的标题、内容,字段进行中文检索,这种情况,是否适合用这个elasticsearch/algolia,还是用tntsearch 。

5年前
(楼主) 5年前

非常谢谢楼主回复! 我记得昨天是安装了。
今天再重安装一下看看,从 composer require guzzlehttp/guzzle 这一步开始。配置...,esinit 文件内容复制过来。
进行到 php artisan es:init ,又报错了,和昨天的不一样。
是不是因为我在vagrant里的原因呢, 于是把 app/scout.php, '' , 换成 192.168.10.10 或 larabbs.test 仍不行。
报错如下:非常谢谢指点。
file
在命令行输入 curl 是正常的。

laravel

5年前
(楼主) 5年前
phpervip (作者) 5年前
5年前
5年前

谢谢分享 你这个关键词高亮后续有文章吗

5年前

问个问题

file
为甚么要访问这个地址呢

5年前
(楼主) 5年前

怎么在config/scout.php 配置elasticsearch外网访问所需的账号密码呢

5年前

最新版本已经自带java了 不过有个问题

λ php artisan es:init
in requestexception.php line 113:
  client error: `put http://127.0.0.1:9200/_template/rtf` resulted in a `400 bad request` response:
  {"error":{"root_cause":[{"type":"mapper_parsing_exception","reason":"root mapping definition has u
  nsupported parameters: (truncated...)
5年前
5年前
5年前
4年前
(楼主) 4年前

问一下我用laravel做es搜索  就是这个案例   都安装好了  也运行成功了    数据同步也成功了     head上也能看到数据        但是我用laravel里面的模型::search()得不出数据   是为什么

4年前

建议你先看看是否真的导入成功了file

4年前

马克

4年前

安装教程有问题,大家都是同一个问题,又没有江南app体育官方入口的解决方案,这样的教程就不要发出来祸害人了

3年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!
未填写
文章
17
粉丝
12
喜欢
139
收藏
315
排名:235
访问:4.1 万
博客标签
社区赞助商
网站地图