承接 pascalvgemert/laravel-cloud-search 相关项目开发

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

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

pascalvgemert/laravel-cloud-search

最新稳定版本:0.4.0

Composer 安装命令:

composer require pascalvgemert/laravel-cloud-search

包简介

A package to use CloudSearch in an Eloquent way within Laravel

README 文档

README

An Eloquent way to use CloudSearch within Laravel

Requires PHP 7.1, Laravel 5.5 or higher and the Laravel AWS package!

Installation

You can install the package via composer:

composer require pascalvgemert/laravel-cloud-search

To install the AWS package follow the steps in the README.md on: AWS Service Provider for Laravel 5 & 6

Usage

Instead of using Models, you create Documents. They almost work the same as a Model Class.

Example:

use LaravelCloudSearch\Contracts\FieldType;
use LaravelCloudSearch\Document;
/**
 * Define your CloudSearch index fields here, this will help to define default values in your document result:
 *
 * @property-read int $id
 * @property-read string $title
 * @property-read string $description
 * @property-read string $country_code
 * @property-read array $images
 * @property-read int $stock
 * @property-read bool $pre_order
 */
class Product extends Document
{
    /** @var string */
    protected $domain = 'http://your-domain-url-for-cloudsearch.eu-west-1.cloudsearch.amazonaws.com';

    /** @var array */
    protected $casts = [
        'images' => FieldType::ARRAY,
        'pre_order' => FieldType::BOOL,
        'searchable' => FieldType::BOOL,
    ];
}

Now you can use this Document to query it like you would query an Eloquent model.

Example:

/** @var \LaravelCloudSearch\DocumentCollection|\LaravelCloudSearch\Document[] **/
$products = Product::query()
    ->select('id')
    ->where('country_code', 'NL')
    ->where(function ($query) {
        $query
            ->where('stock', '>', 0)
            ->orWhere('pre_order', 1);
    })
    ->orderBy('price', 'asc')
    ->take(10)
    ->get();

Extra CloudSearch Methods

Method Example
phrase Product::query()->phrase('Nemo')->get(); (see section: Searching below for more details)
whereLiteral Product::query()->whereLiteral('type', 'game')->get();

Debugging

To debug your build query, you can use the getQuery() method just like Eloquent.

Another great feature is that you can hook into the cloudsearch.query event. The event contains the time it took to execute the query at CloudSearch, which arguments where used and the trace from the place the query got executed. For example you can hook the CloudSearch queries into the Laravel-Debugbar

In Laravel you can listen to the Event as follows:

Event::listen('cloudsearch.query', function ($timeInMilliSeconds, $arguments, $trace) {
    dump($timeInMilliSeconds, $arguments, $trace);
});

Searching

To fuzzy search you can use the phrase(string $searchPhrase, int|float $fuzziness = null, bool $lookForAnyWord = false) method.

The $fuzziness is a decimal percentage 0 to 1 where the default is 0.25. The $lookForAnyWord is a boolean to search for all or any words, default is all words.

Facets

A much used functionality of CloudSearch is the use of Facets / Buckets. This can easily be taking in account while making your query:

/** @var \LaravelCloudSearch\DocumentCollection **/
$products = Product::facet('country_code', ['size' => 10])->get();

/** @var \Illuminate\Support\Collection|\LaravelCloudSearch\Facet[] **/
$productFacets = $products->getFacets();

Statistics (stats)

The same goes for Statistics or stats as they're called in AWS CloudSearch:

/** @var \LaravelCloudSearch\DocumentCollection **/
$products = Product::statistics('country_code')->get();

/** @var \Illuminate\Support\Collection **/
$productStatistics = $products->getStatistics();

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: GPL-3.0-or-later
  • 更新时间: 2019-10-18