安装 laravel elasticsearch

  1. 安装elasticsearch和ik插件 (elasticsearch的使用需要配置java环境,自行百度配置好java环境) elasticsearch集成包(包括ik中文插件)安装地址:https://github.com/medcl/elasticsearch-rtf       
  2. 测试安装  启动elasticSearch:bin/elasticSearch -d       windows系统以管理员身份运行elasticsearch.bat
  3. 测试是否安装成功  127.0.0.1:9200 

 

安装 ElasticSearch Scout Engine 包

composer require tamayo/laravel-scout-elastic

  安装这个包的时候,顺便就会装好 Laravel Scout,发布一下资源

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

 

如果报错了 

报错信息如下:

Problem 1
- Installation request for tamayo/laravel-scout-elastic ^4.0 -> satisfiable by tamayo/laravel-scout-elastic[4.0.0].
tamayo/laravel-scout-elastic 4.0.0 requires laravel/scout ^5.0 -> satisfiable by laravel/scout[5.0.x-dev, v5.0.0, v5.0.1, v5.0.2, v5.0.3] but these conflict with your requirements or minimum-stability.

 

析报错信息,大概原因就是 要安装的tamayo/laravel-scout-elastic 4.0版本 可满足的laravel/scout版本只有5.0.x-dev, v5.0.0, v5.0.1, v5.0.2, v5.0.3 

当前小编安装的laravel/scout为最新版本6.1

看来就是版本互相兼容的问题啦,那就只能对laravel/scout进行降级了

 composer require laravel/scout ^5.0.3


5.在app.php中添加对应的Provider

            Laravel\Scout\ScoutServiceProvider::class,
            ScoutEngines\Elasticsearch\ElasticsearchProvider::class,

6.在count.php中配置参数

 

'driver' => env('SCOUT_DRIVER', 'elasticsearch'),

'elasticsearch' => [
'index' => env('ELASTICSEARCH_INDEX', 'laravel5'),
'hosts' => [
env('ELASTICSEARCH_HOST', 'http://127.0.0.1:9200'),
],
],

 

 

7.创建ylaravel的索引和模板

  • php artisan make:command ESInit  创建命令行
  • 修改ESInit.php文件
安装guzzlehttp/guzzle 扩展
composer require guzzlehttp/guzzle

 

<?php

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 es for post';

/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{

$client = new Client();
// 创建模版
$url = config('scout.elasticsearch.hosts')[0] . '/_template/tmp';
$client->put($url, [
'json' => [
'template' => config('scout.elasticsearch.index'),
'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'
]
]
]
]
]
]
]
]
]
]);

$this->info("========创建模板成功=======");

$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
]
]
]
]
]);
$this->info("========创建索引成功=======");
}

}

 

我的项目中我使用文章数据表来搜索,因此需要修改post.php,也就是posts数据对于的数据模型

use Searchable;

    //定义索引里面的type
    public function searchableAs() { return "post"; } //定义有哪些字段需要搜索 public function toSearchableArray() { return [ 'title'=>$this->title, 'content'=>$this->content, ]; } 
导入数据

使用php artisan命令导入数据

php artisan scout:import "\App\Post"

导入成功之后我们在浏览器地址输栏入:127.0.0.1:9200/laravel54/post/23(laravel54是elasticsearch驱动定义的项目名,post对象的是我项目的post数据模型,23是某条数据的ID )

 


作者:曼巴童鞋
链接:https://www.jianshu.com/p/962e30a859ba
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

转载于:https://www.cnblogs.com/php-no-2/p/10497347.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值