japseyz / algoliasearch-laravel
Laravel Algolia 扩展
Requires
- php: >=5.5.9
- vinkla/algolia: ~2.0
Requires (Dev)
- mockery/mockery: ~0.9
- orchestra/testbench: 3.1.*
- phpunit/phpunit: ~4.0
README
此 PHP 包将 Algolia 搜索 API 集成到 Laravel Eloquent ORM 中。它基于 algoliasearch-client-php 包。支持 PHP 5.5.9+。
注意:如果您正在使用 Laravel 4,请查看 algoliasearch-laravel-4 仓库。
目录
安装
将 algolia/algoliasearch-laravel
添加到您的 composer.json
文件
composer require algolia/algoliasearch-laravel
将服务提供者添加到 config/app.php
中的 providers
数组。
AlgoliaSearch\Laravel\AlgoliaServiceProvider::class
配置
Laravel Algolia 需要一个连接配置。要开始,您需要发布所有供应商资产
php artisan vendor:publish
您可以将 --provider="Vinkla\Algolia\AlgoliaServiceProvider"
选项添加到仅发布 Algolia 包的资产。
这将在您的应用中创建一个 config/algolia.php
文件,您可以修改它来设置配置。此外,请确保在升级后检查与原始配置文件的更改。
快速入门
以下代码为您的 Contact
模型添加搜索功能,创建一个 Contact
索引
use Illuminate\Database\Eloquent\Model; use AlgoliaSearch\Laravel\AlgoliaEloquentTrait; class Contact extends Model { use AlgoliaEloquentTrait; }
默认情况下,所有可见属性都会发送。如果您想发送特定的属性,您可以这样做
use Illuminate\Database\Eloquent\Model; class Contact extends Model { use AlgoliaEloquentTrait; public function getAlgoliaRecord() { return array_merge($this->toArray(), [ 'custom_name' => 'Custom Name' ]); } }
排名 & 相关性
我们提供了多种方法来配置索引设置以调整整体相关性,但最重要的设置是 可搜索属性 和反映 记录流行度 的属性。您可以使用以下代码配置它们
use Illuminate\Database\Eloquent\Model; class Contact extends Model { use AlgoliaEloquentTrait; public $algoliaSettings = [ 'attributesToIndex' => [ 'id', 'name', ], 'customRanking' => [ 'desc(popularity)', 'asc(name)', ], ]; }
您可以使用 setSetting
方法将设置传播(保存)到 algolia
Contact::setSettings();
前端搜索(实时体验)
传统的搜索实现往往在后端具有搜索逻辑和功能。当搜索体验仅由用户输入搜索查询、执行搜索然后重定向到搜索结果页面时,这样做是有意义的。
在后端实现搜索不再是必要的。实际上,在大多数情况下,由于额外的网络和处理延迟,它可能对性能有害。我们强烈推荐使用我们的 JavaScript API 客户端,直接从终端用户的浏览器、移动设备或客户端发起所有搜索请求。这将减少整体搜索延迟,同时同时减轻服务器的负担。
在您的 JavaScript 代码中,您可以这样做
var client = algoliasearch('ApplicationID', 'Search-Only-API-Key'); var index = client.initIndex('YourIndexName'); index.search('something', function(success, hits) { console.log(success, hits) }, { hitsPerPage: 10, page: 0 });
后端搜索
您也可以使用 search
方法,但不建议实现即时/实时搜索体验
Contact::search('jon doe');
选项
自动索引 & 异步
每次保存记录时;它将被 - 异步 - 索引。另一方面,每次销毁记录时,它将 - 异步 - 从索引中删除。
您可以通过设置以下选项禁用自动索引和自动删除设置
use Illuminate\Database\Eloquent\Model; class Contact extends Model { use AlgoliaEloquentTrait; public static $autoIndex = false; public static $autoDelete = false; }
您可以临时禁用自动索引。这通常用于性能原因。
Contact::$autoIndex = false; Contact::clearIndices(); for ($i = 0; $i < 10000; $i++) { $contact = Contact::firstOrCreate(['name' => 'Jean']); } Contact::reindex(); // Will use batch operations.
自定义索引名称
默认情况下,索引名称将是复数形式类名,例如 "Contacts"。您可以通过使用 $indices
选项来自定义索引名称
use Illuminate\Database\Eloquent\Model; class Contact extends Model { use AlgoliaEloquentTrait; public $indices = ['contact_all']; }
按环境索引
您可以使用以下选项在索引名称后缀添加当前 App 环境
use Illuminate\Database\Eloquent\Model; class Contact extends Model { use AlgoliaEloquentTrait; public static $perEnvironment = true; // Index name will be 'Contacts_{\App::environnement()}'; }
自定义 objectID
默认情况下,objectID
基于您的记录的 keyName
(默认为 id
)。您可以通过指定 objectIdKey
选项来更改此行为(请确保使用一个唯一的字段)。
use Illuminate\Database\Eloquent\Model; class Contact extends Model { use AlgoliaEloquentTrait; public static $objectIdKey = 'new_key'; }
限制索引到数据子集
您可以通过定义 indexOnly()
方法来添加约束,以控制记录是否必须被索引。
use Illuminate\Database\Eloquent\Model; class Contact extends Model { use AlgoliaEloquentTrait; public function indexOnly($index_name) { return (bool) $condition; } }
关系
默认情况下,Algolia 包将获取 已加载 的关系。
如果您想索引尚未加载任何关系的记录,您可以在您的模型中创建的 getAlgoliaRecord
中加载它们。
它将看起来像
public function getAlgoliaRecord() { /** * Load the categories relation so that it's available * in the laravel toArray method */ $this->categories; return $this->toArray(); }
在结果对象中,您将会有由 Laravel 转换为数组的类别。如果您想自定义关系结构,您将做如下操作
public function getAlgoliaRecord() { /** * Load the categories relation so that it's available * in the laravel toArray method */ $extra_data = []; $extra_data['categories'] = array_map(function ($data) { return $data['name']; }, $this->categories->toArray(); return array_merge($this->toArray(), $extra_data); }
索引
手动索引
您可以使用 pushToIndex
实例方法触发索引。
$contact = Contact::firstOrCreate(['name' => 'Jean']); $contact->pushToIndex();
手动删除
并使用 removeFromIndex
实例方法触发删除。
$contact = Contact::firstOrCreate(['name' => 'Jean']); $contact->removeFromIndex();
重新索引
要 安全地 重新索引所有记录(将索引移动到临时索引 + 原子性地将临时索引移动到当前索引),请使用 reindex
类方法
Contact::reindex();
要重新索引所有记录(就地,不删除过时记录)
Contact::reindex(false);
清除索引
要清除索引,请使用 clearIndices
类方法
Contact::clearIndices();
主/从
您可以使用 $algolia_settings
变量定义从索引
use Illuminate\Database\Eloquent\Model; class Contact extends Model { use AlgoliaEloquentTrait; public $algoliaSettings = [ 'attributesToIndex' => [ 'id', 'name', ], 'customRanking' => [ 'desc(popularity)', 'asc(name)', ], 'slaves' => [ 'contacts_desc', ], ]; public $slavesSettings = [ 'contacts_desc' => [ 'ranking' => [ 'desc(name)', 'typo', 'geo', 'words', 'proximity', 'attribute', 'exact', 'custom' ] ] ]; }
要使用从索引进行搜索,请使用以下代码
Book::search('foo bar', ['index' => 'contacts_desc']);
针对多个索引
您可以使用 $indices
属性在多个索引中索引记录
use Illuminate\Database\Eloquent\Model; class Contact extends Model { use AlgoliaEloquentTrait; public $indices = [ 'contact_public', 'contact_private', ]; public function indexOnly($indexName) { if ($indexName == 'contact_public') return true; return $this->private; } }
要使用额外索引进行搜索,请使用以下代码
Book::search('foo bar', ['index' => 'contacts_private']);
Eloquent 兼容性
执行
Ad::where('id', $id)->update($attributes);
不会在模型中触发任何操作(因此不会在 Algolia 中更新),因为这不是一个 Eloquent 调用,而是一个方便的方式来生成模型背后的查询
为了让此查询与Algolia兼容,您需要这样做
Ad::find($id)->update($attributes);
兼容性
与5.x应用程序兼容
许可证
Laravel Algolia Search遵循MIT许可证(MIT)。