承接 wannanbigpig/laravel-scout-elastic 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

wannanbigpig/laravel-scout-elastic

最新稳定版本:v1.1.1

Composer 安装命令:

composer require wannanbigpig/laravel-scout-elastic

包简介

Elastic Driver for Laravel Scout

README 文档

README

Latest Stable Version Total Downloads Latest Unstable Version License

Elastic Driver for Laravel Scout

Installation

You can install the package via composer:

composer require wannanbigpig/laravel-scout-elastic

Setting up Elasticsearch configuration

After installing, you should publish the Scout configuration file using the vendor:publish Artisan command. This command will publish the scout.php configuration file to your application's config directory:

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

After you've published the Laravel Scout package configuration, you need to set your driver to elasticsearch and add its configuration:

// 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')],
        // 'auth' => [
        //     'username' => 'elastic',
        //     'password' => 'password copied during Elasticsearch start',
        // ],
        // index_ is followed by the index name. If you do not need to customize the index analyzer, skip the following Settings
        '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]],
                    ],
                ],
            ],
        ],
    ],
];

Usage

console
// create index
php artisan scout:index article

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

// batch update data to es
php artisan scout:import "App\Models\Article"
search example
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 = [
    "_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);

More please see Laravel Scout official documentation.

Reference:

https://github.com/ErickTamayo/laravel-scout-elastic

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

https://github.com/medcl/elasticsearch-analysis-ik

License

The MIT License (MIT).

统计信息

  • 总下载量: 15
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-05-26