laravel8 ES搜索(高亮)

laravel8 composer安装 :

composer require elasticsearch/elasticsearch

es安装好后打开:

elasticsearch-7.17.0>bin>elasticsearch运行程序

kibana-7.4.0-windows-x86_64>bin>kibana运行程序

 路由:

//es添加
Route::get('adddata',[\App\Http\Controllers\EsController::class,'adddata']);
//es搜索
Route::get('seek',[\App\Http\Controllers\EsController::class,'seek']);

控制器 :

<?php

namespace App\Http\Controllers;

use App\Http\ES\elasti;
use App\Http\ES\elasticsearch;
use App\Models\DataModel;
use Illuminate\Http\Request;
class EsController extends Controller
{
    //es添加
    public function adddata(Request $request)
    {
        $data=$request->all();
        $obj=new DataModel();
        $res=$obj->addData($data);
        //转化成数组才可添加es中
        $a=$res->toArray();
          $es=new elasticsearch();
          //es数据库名:database  es表:surface   (自定义)
          $result=$es->addES('database','surface',$a['id'],$a);
          if($result)
          {
              return response()->json(['code'=>200,'mes'=>'es添加成功','data'=>[]]);
          }
    }
    //es搜索
    public function seek(Request $request)
    {
        $name=$request->get('name');
        $es=new elasti();
//        database ES库   surfaceES表
       $data= $es->seek('database','surface',$name);
       print_r($data);
    }
}

封装好的ES函数:

<?php
namespace  App\Http\ES;
use Elasticsearch\ClientBuilder;
class elasticsearch
{
    protected $licent;
    //构造函数(实例化这个类时调用这个类来连接es后复制$licent)
    function __construct()
    {
        $this->licent=ClientBuilder::create()->build();
    }
    //ES添加
    public function addes($index,$type,$id,$data)
    {
        $param=[
            //自定义es里面的数据库
            'index'=>$index,
            //自定义es里面的数据表
            'type'=>$type,
            //添加到mysql数据库的那条数据的id拿来做存在es里数据key
            'id'=>$id,
            //要添加es里面的数据(要求是数组)
            'body'=>$data
        ];
        $res=$this->licent->index($param);
        return $res;

    }
    //ES查询
    public function seek($index,$type,$name)
    {
        $param=[
            'index'=>$index,
            'type'=>$type,
            'body'=>[
                'query'=>[
                    'match'=>[
                        'name'=>$name
                    ]
                ],
                //搜索数据高亮
                'highlight'=>[
                    'fields'=>[
                        'name'=>[
                            'pre_tags'=>['<span style="color: red">'],
                            'post_tags'=>['</span>']
                        ]
                    ]
                ]
            ]
        ];
        //执行搜索
        $data=$this->licent->search($param);
        return $data['hits']['hits'];
    }
}

模型:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class DataModel extends Model
{
    use HasFactory;
    protected $table='data';
    //白名单(使用create必须加白名单)
    protected $guarded=[];
    public function addData($data)
    {
        //create添加会返回添加的这条数据(包括id)
        return $this->create($data);
    }
}

效果:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值