laravel-elasticsearch 配置以及运用

laravel-elasticsearch 配置以及运用

git:https://github.com/babenkoivan/scout-elasticsearch-driver

composer require babenkoivan/scout-elasticsearch-driver

//在config/app.php最后增加

'providers' => [
    Laravel\Scout\ScoutServiceProvider::class,
    ScoutElastic\ScoutElasticServiceProvider::class,
]

//执行命令,发布(命令生成elastic配置文件)

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

//修改elastic配置文件 config/sout.php

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

添加

'elastic' => [
    'index' => env('ELASTICSEARCH_INDEX', 'people'), // people是 ES索引
        'hosts' => [
        env('ELASTICSEARCH_HOST', 'http://localhost:9200'),
    ],
],

配置完成开始创建ES 索引 类型
创建索引模板(目录可随意)

php artisan make:index-configurator App\\Es\\PeopleConfigurator

生成文件:
在这里插入图片描述
创建索引(指向的是刚刚创建的索引模板):

php artisan elastic:create-index "App\Es\PeopleConfigurator"

创建ES搜索的模型(People是新建的模型名 指向索引模板)

php artisan make:searchable-model People --index-configurator=App\\Es\\PeopleConfigurator
<?php

namespace App;

use App\Es\PeopleConfigurator;
use ScoutElastic\Searchable;
use Illuminate\Database\Eloquent\Model;

class People extends Model
{
    use Searchable;

    /**
     * @var string
     */
    protected $indexConfigurator = PeopleConfigurator::class;

    /**
     * @var array
     */
    protected $searchRules = [
        //
    ];

    /**
     * @var array
     */
    protected $mapping = [
        'properties' => [ //⽂文档类型设置(相当于mysql的数据类型)
            'name'   => [
                'type'            => 'text',// 字段类型为全⽂文检索,如果需要关键字,则修改为keyword,注意keyword字段为整体查询,不不能作为模糊搜索
                'analyzer'        => 'ik_smart',
                'search_analyzer' => 'ik_smart',
                'boost'           => 5,
                'fields'          => [
                    'company_name' => [
                        'type'         => 'keyword',
                        'ignore_above' => 256,
                    ],
                ],
            ],
            'city'   => [
                'type' => 'keyword',
            ],
            'vision' => [
                'type' => 'text',
                'analyzer'        => 'ik_smart',
                'search_analyzer' => 'ik_smart',
            ],
            'email'  => [
                'type' => 'keyword',
            ],
            'phone'  => [
                'type' => 'keyword',
            ],
        ],
    ];

    /**
     * 设置索引的type 类似 mysql的表
     *
     * @return string
     */
    public function searchableAs()
    {
        return 'people';
    }
}

更新索引mapp

php artisan elastic:update-mapping "App\Models\Search\People"

//把数据库的数据 插入映射到 elastic服务器上

php artisan scout:import "App\Models\Search\People"

//最后控制器 调用elasticearch的model进行查询

\AppPeople::search('查询的字眼')->get();
php artisan elastic:create-index "App\Es\PeopleConfigurator" // 创建索引
php artisan elastic:update-mapping "App\Models\Search\People" // 更新映射
php artisan scout:import "App\Models\Search\People" // 全量导入数据
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将Laravel-Admin与ELK整合在一起,你可以按照以下步骤进行操作: 1. 首先,按照之前提供的方法安装并配置Laravel-Admin。 2. 接下来,使用docker-compose安装ELK堆栈,可以参考前面提供的教程。 3. 在Laravel项目中安装Elasticsearch客户端库。可以使用Composer运行以下命令: ``` composer require elasticsearch/elasticsearch ``` 4. 创建一个新的控制器来处理日志数据。在命令行中运行以下命令来生成控制器: ``` php artisan make:controller LogController ``` 5. 在LogController中,你可以编写相应的方法来将日志数据发送到Elasticsearch。以下是一个简单的示例: ```php <?php namespace App\Http\Controllers; use Elasticsearch\ClientBuilder; use Illuminate\Http\Request; class LogController extends Controller { public function sendLog(Request $request) { $client = ClientBuilder::create()->build(); // 将日志数据发送到Elasticsearch $params = [ 'index' => 'logs', // 定义索引名称 'body' => [ 'message' => $request->input('message'), // 日志消息 'level' => $request->input('level'), // 日志级别 ], ]; $response = $client->index($params); return response()->json($response); } } ``` 请根据你的实际需求进行适当的调整。 6. 在routes/web.php文件中定义相关的路由。例如,可以添加以下路由: ```php Route::post('/log', 'LogController@sendLog'); ``` 7. 在Laravel-Admin中创建相应的页面和表单,用于输入日志数据。你可以使用Laravel-Admin的表单组件来创建一个用于输入日志消息和级别的表单。 8. 在Laravel-Admin的表单提交处理方法中,使用Guzzle或其他HTTP客户端库将日志数据发送到上面定义的路由。以下是一个简单的示例: ```php use GuzzleHttp\Client; // ... public function handleFormSubmit(Request $request) { $client = new Client(); $response = $client->post('http://your-domain/log', [ 'form_params' => [ 'message' => $request->input('message'), 'level' => $request->input('level'), ], ]); // 处理响应结果 return redirect()->back()->with('success', 'Log sent successfully.'); } ``` 请根据你的实际情况进行适当调整。 通过以上步骤,你可以将Laravel-Admin与ELK整合在一起。在Laravel-Admin中创建一个页面和表单,用于输入日志数据,并在后台将这些数据发送到Elasticsearch。然后,你可以使用Kibana等工具对这些日志数据进行可视化和查询。记得根据你的实际需求进行适当的调整和扩展。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值