需求:只查不需要更新,删除,添加操作,因为添加通过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查询结果