承接 romangrinev/laravel-opensearch-engine 相关项目开发

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

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

romangrinev/laravel-opensearch-engine

最新稳定版本:v0.0.16

Composer 安装命令:

composer require romangrinev/laravel-opensearch-engine

包简介

Custom Laravel Scout OpenSearch Engine

README 文档

README

Installation

composer require romangrinev/laravel-opensearch-engine

Update your App\Providers\AppServiceProvider

<?php

namespace App\Providers;

// ...

use Grinev\LaravelOpenSearchEngine\OpenSearchEngine;
use Laravel\Scout\EngineManager;

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        // ...

        resolve(EngineManager::class)->extend(config('scout.driver'), function () {
            return new OpenSearchEngine;
        });

    }
}

Update config\scout.php

<?php

return [

    //

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

    'opensearch' => [
        'host' => env('OPENSEACH_HOST', 'http://localhost:9200'),
        'user' => env('OPENSEACH_USER', 'admin'),
        'pass' => env('OPENSEACH_PASS', 'admin'),
    ],

    //
];

Usage

Search by keyphrase

$posts = Post::search('Key phrase')->get();

Order search results

$posts = Post::search()->orderBy('posted_at', 'desc')->get();

Search by term

$posts = Post::search()->where('category_id', '48')->get();

Search by range

$posts = Post::search()->where('range', [
    'price' => [
        'gte' => 100,
        'lte' => 200
    ]
])->get();

Learn more about OpenSearch Range Queries

Seach by geo location

Here is more about OpenSearch Geo-bounding box queries

$posts = Post::search()->->where('geo_bounding_box', [
    "location" => [
        "top_left" => [
            "lat" => 48.0,
            "lon" => -123.0
        ],
        "bottom_right" => [
            "lat" => 46.0,
            "lon" => -121.0
        ]
    ]
])->get();

In order to perform this type of seary make sure the mapping for the location field is set as geo_point in your config/scout.php config file.

config/scout.php

<?php
return [
    //
    'opensearch' => [
        //
        'mappings' => [
            'posts_index' =>[
                "mappings" => [
                    "properties" => [
                        "location" => [
                            "type" => "geo_point"
                        ]
                    ]
                ]
            ]
        ]
    ],
];

app/models/Post.php

    public function searchableAs(){
        return 'posts_index';
    }
	public function toSearchableArray(){
		$data = [
            //
			'location' => "{$this->lat},{$this->lng}",
		];

		return $data;
	}

After changing scout.php for mapping and updating the toSearchableArray() make sure to update your OpenSearch index like this:

php artisan scout:flush App\\Models\\Post
php artisan scout:index App\\Models\\Post
php artisan scout:import App\\Models\\Post

Group By / Aggregate results

This example shows how to group by / aggregate open search results.

$raw = Post::search('Key phrase')
    ->whereRaw([
        'aggs' => [
            'categories' => [
                'terms' => [
                    'field' => 'category_id'
                ]
            ]
        ]
    ])->raw();
$categories = collect(data_get($raw, 'aggregations.categories.buckets', []))->pluck('key')->map(fn($id) => Category::find($id));

Learn more about OpenSearch Aggregations

Learn more about Laravel Scout

统计信息

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

GitHub 信息

  • Stars: 13
  • Watchers: 2
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-05-17