laravel 使用es实现全文搜索功能

laravel 如何使用方便的使用es实现全文搜索功能?

本扩展包支持IK分词设置。

在按下文操作前请先阅读 laravel scout 全文搜索文档

安装

您可以通过composer安装软件包 wannanbigpig/laravel-scout-elastic 点此查看更多:

composer require wannanbigpig/laravel-scout-elastic

Laravel 会自动注册驱动服务提供者。

Elasticsearch 配置

安装完成后,您应该使用vendor:publish Artisan命令发布Scout配置文件。该命令将把scout.php配置文件发布到应用程序的config目录中:

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

发布Laravel Scout包配置后,您需要将驱动程序设置为弹性搜索并添加其配置:

// config/scout.php
<?php

return [
    // ...

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

    // ...

    /*
    |--------------------------------------------------------------------------
    | Elasticsearch Configuration
    |--------------------------------------------------------------------------
    |
    | Here you may configure your Elasticsearch settings.
    |
    */

    'elasticsearch' => [
        'hosts' => [env('ELASTICSEARCH_HOST', 'http://127.0.0.1:9200')],
        // 如果你的es没有开启校验账号密码则忽略该配置
        // 'auth' => [
        //     'username' => 'elastic',
        //     'password' => 'password copied during Elasticsearch start',
        // ],
        // index_ 后跟索引名称。如果不需要自定义索引分词模式,则跳过下面的设置
        'index_article' => [
            'settings' => [
                'number_of_shards' => 5,
                'number_of_replicas' => 1,
            ],
            'mappings' => [
                "properties" => [
                    "title" => [
                        "type" => "text",
                        "analyzer" => "ik_max_word",
                        "search_analyzer" => "ik_smart",
                        "fields" => ["keyword" => ["type" => "keyword", "ignore_above" => 256]],
                    ],
                ],
            ],
        ],
    ],
];

使用

命令
// 创建索引
php artisan scout:index article

// 删除
php artisan scout:delete-index article

// 批量更新数据到es
// Article这个model需引入use Laravel\Scout\Searchable;
// 想自定义同步到es的字段需自己实现toSearchableArray这个方法
php artisan scout:import "App\Models\Article"
搜索示例
use App\Models\Article;

// $condition = "test";
// ... or
// $condition = [
//     "title" => "test",
//     "abstract" => "test"
// ];
// ... or
$keyword = "test";
$source = [1,2];
$startTime = '2023-05-01T00:00:00.000+0800';
$endTime = '2023-05-20T00:00:00.000+0800';
$condition = [
    // 该字段仅用来区分是否选择自定义es搜索body请求体,不会实际发送至es
    "_customize_body" => 1,
    "query"=>[
        "bool" => [
            "should" => [
                [
                    "match" => [
                        "title" => ["query" => $keyword, 'boost' => 5]
                    ]
                ],
                [
                    "match" => [
                        "abstract" => ["query" => $keyword, 'boost' => 3]
                    ]
                ],
            ],
            "must" => [
                [
                    "terms" => ["source" => $source]
                ],
                [
                    "range" => [
                        "created_at" => [
                            'gte' => $startTime,
                            'lte' => $endTime
                        ]
                    ]
                ]
            ]
        ],
    ],  
];

$data = Article::search($condition)
        ->orderBy('_score', 'desc')
        ->paginate(10);

更多使用方法 Laravel Scout official documentation.

参考:

github.com/ErickTamayo/laravel-sco...

github.com/laravel/scout/tree/10.x

github.com/medcl/elasticsearch-ana...

License

The MIT License (MIT).

本作品采用《CC 协议》,转载必须注明作者和本文链接
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!