laravel-scout包的安装和适配elasticsearch引擎

1.laravel-scout扩展包的安装

composer require laravel/scout

会下载到laravel框架的vendor/laravle/scout目录下

2.在config/app.php配置providers 数组中:添加scout服务提供者

Laravel\Scout\ScoutServiceProvider::class,

3.注册好 Scout 的服务提供者之后,你可以使用 vendor:publish Artisan 命令生成 Scout 的配置文件。这个命令会在你的 config 目录下生成 scout.php 配置文件:

php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"

这个命令的作用就是把/vendor/laravel/scout/config/scout.php配置文件复制到config目录下,scout服务的配置就在这个配置文件中。

此时laravel-scout扩展包就算安装好了。

2.给scout包适配elasticsearch引擎

我们一般使用elasticsearch作为全文搜索引擎

2.1 安装laravel-scout-elastic扩展包,这个就是操作elasticsearch的扩展包

composer require tamayo/laravel-scout-elastic

安装包会下载到vendor/tamayo/laravel-scout-elastic目录下

2.2在config/app.php的providers 数组中添加服务提供者

ScoutEngines\Elasticsearch\ElasticsearchProvider::class,

2.3修改scout配置文件,把驱动修改为elasticsearch并且配置索引和es服务地址

'driver' => env('SCOUT_DRIVER', 'elasticsearch'),
//elasticsearch引擎的配置
    'elasticsearch' => [
        'index' => env('ELASTICSEARCH_INDEX', 'laravel54'),//这是索引名(相当于mysql的数据库)
        'hosts' => [
            env('ELASTICSEARCH_HOST', 'http://127.0.0.1:9200'),//这是es服务的地址
        ],
    ],

3.启动elasticsearch服务可用

先确保elasticsearch服务可用,就是用浏览器访问http://127.0.0.1:9200,如果出现下面,表示服务正常

{
  "name" : "vhr6l8u",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "fIwrtOj4SUyHxO9ddul4_g",
  "version" : {
    "number" : "5.1.1",
    "build_hash" : "5395e21",
    "build_date" : "2016-12-06T12:36:15.409Z",
    "build_snapshot" : false,
    "lucene_version" : "6.3.0"
  },
  "tagline" : "You Know, for Search"
}

elasticsearch的restfulAPI

我们和es服务交互时通过restfulAPI,通过http协议,所以引入Guzzle这个PHP HTTP客户端,他可以在php中直接发送各种的http请求

4.laravel的Guzzle包安装使用

composer require guzzlehttp/guzzle

4.1常用的用法

$client = new Client;
$url = "http://192.168.1.207/icar_dev101/miniapp.php/sstore/hot_goods_list/";
//get通过普通参数获取
$response = $client->get($url,['query' => ['sstore_id' => 1]]);
//post提交普通参数
$response = $client->post($url,['form_params'=>['id'=>2]]);
//post提交文件
$response = $client->post($url,['multipart'=>[
    		[
            'name'     => 'qux',//字段键名
            'contents' => fopen('/path/to/file', 'r'),//文件句柄
            'filename' => 'custom_filename.txt' //文件名
        	]
        ]]);
//put提交json
$response = $client->put($url, ['json' => ['foo' => 'bar']]);
//返回的是response对象,下面就是接口返回的数据
dd($response->getBody()->getContents());

 5.使用elasticsearch

5.1创建索引,一般用mappings指定好映射

{
    "mappings":{
        "articles":{
            "properties":{
                "id":{
                    "type":"integer"
                },
                "title":{
                    "type":"text"
                },
                "content":{
                    "type":"text"
                },
                "user_id":{
                    "type":"integer"
                },
                "updated_at":{
                    "type":"date",
                    "format":"yyyy-MM-dd HH:mm:ss"
                }
            }
        }   
    }
}

把这个json字符用put方式提交给http://localhost:9200/blog,系统会创建blog索引,类型是article,字段就是id,title等。可以用postman提交创建

5.2使用laravel-commend创建索引

php artisan make:command ESInit

会创建app\Console\Command\ESInit.php文件 ,可以看到一个属性protected $signature = 'es:init';则使用这个命令就用

 php artisan es:init
//就会执行app\Console\Command\ESInit.php文件的handle方法

在app\Console\Kernel.php中挂载命令

protected $commands = [
        \App\Console\Commands\ESInit::class,
    ];

 修改编辑handle方法

public function handle()
    {
        $client = new Client;
        //创建ES的索引
        $url = config('scout.elasticsearch.hosts')[0].'/'.config('scout.elasticsearch.index');
        //$client->delete($url);//删除这个索引
        $param = [
            "json"=>[
                "mappings"=>[
                    "article"=>[
                        "properties"=>[
                            "title"=>[
                                "type"=>"keyword"
                            ],
                            "content"=>[
                                "type"=>"text"
                            ]
                        ]
                    ]
                ]
            ]
        ];
        $client->put($url,$param);
        $this->info("创建ES索引成功!");
    }

使用命令 php artisan es:init 就会执行handle方法。根据配置创建一个索引,里面类型是article,字段有title,content

5.3与对应的model关联

创建一个articlemodel,然后修改articlemodel类

namespace App\Models;
​
use Illuminate\Database\Eloquent\Model;
use Laravel\Scout\Searchable;
class Article extends Model
{
    use Searchable;
    // 定义索引里面的type,相当于表
    public function searchableAs(){
        return 'article';
    }
    // 定义哪些字段需要搜索
    public function toSearchableArray(){
        return [           
            'title'=>$this->title,
            'content'=>$this->content,           
        ];
    }   
}

当我们的控制器使用App\Models\Article添加修改article表时,会对应修改到elasticsearch的索引blog,类型为articles里面的文档。scout.php配置文件配置了elasticsearch绑定的index和host.相当于指定了数据库。而类型则是在model里面绑定。

6.window使用elasticsearch和head

https://www.cnblogs.com/gangle/p/9328257.html

6.1elasticsearch服务开启

运行elasticsearch目录下的bin下面的elasticsearch.bat,然后访问http://127.0.0.1:9200

6.2head工具开启

安装好(需要安装node.js和grunt),到/htdocs/elasticsearch-head-master目录下,执行

grunt server

就会启动head服务,访问http://localhost:9100/即可

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

runtoweb3

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值