laravel 下 elasticsearch/algolia 全文搜索 使用案例 | laravel china 社区-大发黄金版app下载
大发黄金版app下载官网网址:https://github.com/medcl/elasticsearch-rtfwindows环境下,进入安装目录。点击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如图既安装成功
注意: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:initnamespace 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里面也会将数据库指定字段写入

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

algolia是法国初创公司为你提供毫秒级的数据库实时搜索服务。在这里我也配置实验了下,具体没有压测,有兴趣的朋友可以尝试下,这里只写简单的使用。具体elsaticsearch和algolia如何选择,看业务需求具体来选择
在项目env文件添加
scout_driver=algolia
scout_prefix=
algolia_app_id=xxxxxxx
algolia_secret=xxxxxxxxxxx这里的appid和secret需要自己注册获取
大发黄金版app下载官网网址:
注册成功后;

然后执行命令
php artisan scout:import "app\models\article"执行完会发现数据已经写到后台了

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

elasticsearch作为一款强大的开源分布式搜索与数据分析引擎,可快速从海量数据总找到相关信息,近年来搜索dbranking一直位列第一。还被广泛应用于大数据近实时分析,包括日志分析,指标监控,信息安全等等,国内很多公司都在使用,腾讯,滴滴,小米,饿了么,今日头条等。所以这里面要学的东西还是很多很多啊,具体数据如何分片,索引如何划分,map设置,analysis具体细节,最佳数据模型构建,集群扩展,参数配置,性能调优,等等等,都是我们需要去掌握的,前路漫漫其修远兮啊,加油!
本作品采用《cc 协议》,转载必须注明作者和本文链接
 
 
博客用es还是太重了,我现在用的tntsearch
请问学长,
刚才 进行到这一步报错了,该怎么解决呢?非常谢谢!

另一个问题,我的项目是一些视频,音频,文章表,每个表大概1万条数据,全网查询时,需要对这些表的标题、内容,字段进行中文检索,这种情况,是否适合用这个elasticsearch/algolia,还是用tntsearch 。
非常谢谢楼主回复! 我记得昨天是安装了。

今天再重安装一下看看,从 composer require guzzlehttp/guzzle 这一步开始。配置...,esinit 文件内容复制过来。
进行到 php artisan es:init ,又报错了,和昨天的不一样。
是不是因为我在vagrant里的原因呢, 于是把 app/scout.php, '' , 换成 192.168.10.10 或 larabbs.test 仍不行。
报错如下:非常谢谢指点。
在命令行输入 curl 是正常的。
谢谢分享 你这个关键词高亮后续有文章吗
问个问题
为甚么要访问这个地址呢
怎么在config/scout.php 配置elasticsearch外网访问所需的账号密码呢
最新版本已经自带java了 不过有个问题
问一下我用laravel做es搜索 就是这个案例 都安装好了 也运行成功了 数据同步也成功了 head上也能看到数据 但是我用laravel里面的模型::search()得不出数据 是为什么
建议你先看看是否真的导入成功了
马克
安装教程有问题,大家都是同一个问题,又没有大发黄金版app下载的解决方案,这样的教程就不要发出来祸害人了