lumen集成elasticsearch检索

3 篇文章 0 订阅

需求:只查不需要更新,删除,添加操作,因为添加通过es的filebeat或logstash中间件完成,日志性数据更是不会删除和更新,一致在不停积累,增加,海量的数据;

1,在https://packagist.org/包管理库中搜elasticsearch关键词,按照排名,找到一个basemkhirat/elasticsearch,排名比较考前;用composer一步完成:$ composer require basemkhirat/elasticsearch

2,和大多的elasticsearch一样,这个插件也是基于laravel的scout这个检索工具完成。关于scout可查看:https://learnku.com/docs/laravel/7.x/scout/7516#ec6819 (若有必要可以详细读一遍)默认的驱动并不是es,我们更换成上面安装的那个插件即可,

3,再发一个basemkhirat/elasticsearch比较详细的使用方法,参考地址:https://github.com/babenkoivan/scout-elasticsearch-driver

4,lumen为了快,精简了很多功能,很多命令的用不了的,那些命令都是自动化完成一些工作,这些工作完全可以手工完成;

比如:配置文件,一共2个,一个scout的配置文件:scout.php文件,可以在/vendor/laravel/scout/config/scout.php 找到;复制到/config中,改成自己的配置;需要自己配置的请在.env文件中自定义设置;一个elasticsearch配置文件;同样在/vendor/basemkhirat/scout-elasticsearch-driver/config中,复制到/config中,修改自己的配置;

在/bootstrap/app.php中增加服务提供者:

$app->register(Laravel\Scout\ScoutServiceProvider::class);//scout全文检索
$app->register(ScoutElastic\ScoutElasticServiceProvider::class);//用elasetic驱动scout检索

5,新建一个model类和一个检索配置类,我都写到了一个文件中,位置在:/app/models/;名字随便写一个,可以按照es的检索名创建;
我的model类叫EsDeviceIndex;配置类叫EsIndexConfigurator;都在一个文件中,文件名叫EsDeviceIndex.php

<?php
/**
 * Created by PhpStorm.
 * User: fangliang
 * Date: 2021/4/27
 * Time: 5:36 PM
 */

namespace App\Models;

use ScoutElastic\Searchable;
use ScoutElastic\IndexConfigurator;
use Illuminate\Database\Eloquent\Model;

class EsDeviceIndex extends Model
{
    use Searchable;
    //检索配置类,在下面,基本复制官网
    protected $indexConfigurator = EsIndexConfigurator::class;

    protected $table = 'test';//mysql表名,因为我都model类名和表不一样,需指定
    //protected $fileable = ['uuid', 'version'];
    public function __construct(array $attributes = [])
    {
        parent::__construct($attributes);

        //dump(Searchable::getMapping());
    }
    protected $searchRules = [
        //
    ];
    //es检索都type名,6.0之后type都是_doc
    public function searchableAs()
    {
        return '_doc';
    }
    /**
     * 这个方法是mysql表中字段和es中字段都映射关系;比如在mysql中user表中字段叫name,而在es中字段叫my_name;
     * 这个时候映射关系就是:
     * return ['my_name' => $this->name]
     * 如果一致就不用重写这个方法了
     *
     * @return array
     */
    public function toSearchableArray()
    {
        $array = $this->toArray();

        // 自定义数组...

        return $array;
    }

    protected $mapping = [
        'properties' => [
            'uuid' => [
                'type' => 'text',
                // Also you can configure multi-fields, more details you can find here https://www.elastic.co/guide/en/elasticsearch/reference/current/multi-fields.html
                'fields' => [
                    'keyword' => [
                        'type' => 'keyword',
                    ]
                ]
            ],
            'version'=>[
                'type'=>'long'

            ],
        ]
    ];
}

//索引配置

class EsIndexConfigurator extends IndexConfigurator
{
    public function __construct()
    {
        //dump(parent::getSettings());
    }

    // It's not obligatory to determine name. By default it'll be a snaked class name without `IndexConfigurator` part.
    protected $name = 'test';//es索引名称,很关键

    // You can specify any settings you want, for example, analyzers.

    protected $settings = [
        'index' => [
            'routing' => [
                'allocation' => [
                    'include' => [
                        '_tier_preference'=>'data_content'
                    ]
                ]
            ]
        ]
    ];

}

重点(下面的注释部分):

/*
         * elasticSearch
         * get()方法查询的过程是按条件查es中的结果,找到es的主键_id,再根据这些_id去in 数据库(mysql)中的主键id;
         * 也就是说get方法where的条件去es的检索中查,但返回的结果是mysql表中的数据;
         * 这个机制需要保证要检索的字段在es和mysql一致,在mysql表中存在name字段,就需要再es中也有这个字段;
         * 检索的数据是按照es中而不是mysql;
         *
         * */

        $rs = $this->esDeviceIndex::search('jack')
            //->select(['uuid'])
            //->where("data_type.keyword","deviceReport")
            //->where("_id","3")
            //->where('version','1')
            ->get();
        //推荐用下面这个或用rew()方法
        $res = $this->esDeviceIndex::searchRaw([
            'query' => [
                'term' => [
                    'uuid' => '866002054322556'
                ]
            ]
        ]);

        //$rs = $this->esDeviceIndex::search();

        dump($rs);
        echo json_encode($rs['_payload']['body']);//能打印出原始都es查询结果
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

yijiliangfang

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

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

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

打赏作者

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

抵扣说明:

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

余额充值