algolia/laravel-scout-algolia-macros 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

algolia/laravel-scout-algolia-macros

Composer 安装命令:

composer require algolia/laravel-scout-algolia-macros

包简介

A collection macros to extend Laravel Scout with more Algolia capabilities

README 文档

README

DEPRECATED: Use of this repository is deprecated. Please use Scout Extended - https://github.com/algolia/scout-extended instead.

Laravel Scout Algolia Macros

A collection of useful macros to extend Laravel Scout capabilities when using the Algolia engine.

This package aims to provide a set of macros to take advantage of the Algolia-specific feature.

Installation

Pull the package using composer

composer require algolia/laravel-scout-algolia-macros

Next, you should add the Algolia\ScoutMacros\ServiceProvider to the providers array of your config/app.php configuration file:

Algolia\ScoutMacros\ServiceProvider::class

Usage

count

The count method will return the number of results after the request to Algolia.

The point is to avoid pull data from the database and building the collection.

$nbHits = Model::search('query')->count();

hydrate

The hydrate method is similar to the standard get() method, except it hydrates the models from your Algolia index.

By default, Scout will use the IDs of the results from Algolia and pull the data from the local database to create the collection.

Hence, hydrate will be much faster than get.

Note: By using this method you must be sure that you are correctly keeping your algolia index in sync with your database to avoid populating stale data.

Restrict attributes

By default, this method will add all attributes from Algolia's record to your model. If you want to remove sensitive or irrelevant data from your model, you have two options.

You can set a list of retrievable attributes in your Algolia dashboard. In this case, Algolia will only return these attributes while still searching every searchableAttributes.

You may as well use the laravel $guarded attributes of your model class. For instance, if you don't want to see the _h attribute in your collection, you will have the following.

namespace App;

use Illuminate\Database\Eloquent\Model;
use Laravel\Scout\Searchable;

class People extends Model
{
    use Searchable;

    protected $guarded = ['_highlightResult'];
}

with

The with method gives you complete access to the Algolia options parameter. This allows you to customise the search parameters exactly the same as you would using the algolia php library directly.

Simple example

$result = Model::search('')
					->with(['hitsPerPage' => 30])
					->get();

Advanced example

$filters = [
    'averge_ratings >= 3',
    'total_reviews >= 1'
];

$filterString = implode(' AND ', $filters);

$params = [
            'aroundLatLng' => $lat.','.$lon,
            'hitsPerPage' => 30,
            'page' => 0,
            'aroundRadius' => 30000, //30km
            'aroundPrecision' => 200, //200 Meters
            'getRankingInfo' => true, //return ranking information in results
            'filters' => $filterString // add filters
        ];

$result = Model::search('')->with($params)->get();

aroundLatLng

ThearoundLatLng method will add geolocation parameter to the search request. You can define a point with its coordinate.

Note that this method is pure syntactic sugar, you can use with to specify more location details (like radius for instance)

// Models around Paris latitude and longitude
Model::search('query')->aroundLatLng(48.8588536, 2.3125377)->get();

Where clauses can also be added

Model::search('query')
    ->aroundLatLng(48.8588536, 2.3125377)
    ->where('something_id', 1)
    ->get();

Contributing

Feel free to open an issue to request a macro.

Open any pull request you want, so we can talk about it and improve the package. 🎉

统计信息

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

GitHub 信息

  • Stars: 36
  • Watchers: 66
  • Forks: 8
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-06-14

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固