algolia / algoliasearch-laravel
Requires
- php: >=5.5.9
- vinkla/algolia: ~2.0
Requires (Dev)
- mockery/mockery: ~0.9
- orchestra/testbench: 3.1.*
- phpunit/phpunit: ~4.0
- dev-master / 1.0.x-dev
- 4.2.x-dev
- 1.7.1
- 1.7.0
- 1.6.1
- 1.6.0
- 1.5.0
- 1.4.0
- 1.3.0
- 1.2.2
- 1.2.1
- 1.2.0
- 1.1.1
- 1.1.0
- 1.0.10
- 1.0.9
- 1.0.8
- 1.0.7
- 1.0.6
- 1.0.5
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
- dev-feat/temporaryDisableIndexing
- dev-slaves-replicas-renaming
- dev-getting-started-reindex
- dev-fix_slaves
- dev-custom-auto-index-and-delete
This package is not auto-updated.
Last update: 2019-02-20 18:33:03 UTC
README
Algolia Search 是一个托管的全文、数值和分面搜索引擎,可以从第一个按键开始实时返回结果。
此包已过时,我们建议您使用Laravel Scout。如果您想扩展 Scout 的功能,请参阅我们的专用文档
此 PHP 包将 Algolia Search API 集成到 Laravel Eloquent ORM 中。它基于algoliasearch-client-php 包。
注意:如果您使用 Laravel 4,请查看algoliasearch-laravel-4 存储库。
API 文档
您可以在Algolia 网站上找到完整参考。
目录
安装
通过 composer 安装
将 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' ]); } }
设置好模型后,您需要手动执行数据的初始导入。您可以通过在模型类上调用 reindex
来完成此操作。使用我们之前的例子,这将是这样
Contact::reindex();
排名与相关性
我们提供了多种方式来配置索引设置以调整整体相关性,但最重要的还是可搜索属性和反映记录流行度的属性。您可以使用以下代码进行配置
use Illuminate\Database\Eloquent\Model; class Contact extends Model { use AlgoliaEloquentTrait; public $algoliaSettings = [ 'searchableAttributes' => [ 'id', 'name', ], 'customRanking' => [ 'desc(popularity)', 'asc(name)', ], ]; }
您可以使用setSetting
方法将设置传播(保存)到algolia
Contact::setSettings();
同义词
同义词用于告知引擎哪些词或表达式在文本相关性方面应被视为相等。
我们的同义词API被设计为可以尽可能容易地管理索引及其副本的大批同义词。
您可以通过在$algoliaSettings
类属性中添加synonyms
来使用同义词API,如下所示
use Illuminate\Database\Eloquent\Model; class Contact extends Model { use AlgoliaEloquentTrait; public $algoliaSettings = [ 'synonyms' => [ [ 'objectID' => 'red-color', 'type' => 'synonym', 'synonyms' => ['red', 'another red', 'yet another red'] ] ] ]; }
您可以使用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. Contact::$autoIndex = true;
您还可以通过在模型上创建autoIndex
和/或autoDelete方法
来为这两个参数设置动态条件
use Illuminate\Database\Eloquent\Model; class Contact extends Model { use AlgoliaEloquentTrait; public function autoIndex() { if (\App::environment() === 'test') { return false; } return true; } public static autoDelete() { if (\App::environment() === 'test') { return false; } return true; } }
请注意,在AlgoliaEloquentTrait中定义这两个方法。当将这些方法放入父类中时,如果在子类中使用它们,它们将被AlgoliaEloquentTrait“擦除”(因为PHP继承)。
自定义索引名称
默认情况下,索引名称将是复数类名,例如“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); }
索引
可见性
默认情况下,Algolia只能访问模型的可见属性。因此,例如,当使用此示例代码时,您将收到No content in PUT request
异常,因为invisible_attribute
键返回一个空/空变量。
protected $visible = ['visible_attribute', 'other_visible_attribute']; public function getAlgoliaRecord() { return [ 'invisible_attribute' => $this->invisible_attribute ]; }
在索引之前,请确保已正确列出您的可见属性。要绕过Laravel施加的安全掩码,您可以使用$this->attributes['invisible_attribute']
直接访问属性,即使它不可见,但建议您避免在模型中访问属性的类型。
手动索引
您可以使用pushToIndex
实例方法触发索引。
$contact = Contact::firstOrCreate(['name' => 'Jean']); $contact->pushToIndex();
手动删除
并使用removeFromIndex
实例方法触发删除。
$contact = Contact::firstOrCreate(['name' => 'Jean']); $contact->removeFromIndex();
重新索引
要安全地重新索引所有记录(将索引到临时索引 + 原子性地将临时索引移动到当前索引),请使用reindex
类方法
Contact::reindex();
要重新索引所有记录(就地,不删除过时记录)
Contact::reindex(false);
要在重新索引过程中设置设置
Contact::reindex(true, true);
要在重新索引和更改设置时保留在Algolia仪表板上设置的设置
Contact::reindex(true, true, true);
要实现一个回调,该回调在每次索引一批实体时被调用
Contact::reindex(true, true, false, function ($entities) { foreach ($entities as $entity) { var_dump($entity->id); // Contact::$id } });
清除索引
要清除索引,请使用clearIndices
类方法
Contact::clearIndices();
管理索引
主/副本
您可以使用$algolia_settings
变量定义副本索引
use Illuminate\Database\Eloquent\Model; class Contact extends Model { use AlgoliaEloquentTrait; public $algoliaSettings = [ 'searchableAttributes' => [ 'id', 'name', ], 'customRanking' => [ 'desc(popularity)', 'asc(name)', ], 'replicas' => [ 'contacts_desc', ], ]; public $replicasSettings = [ '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 兼容性
Eloquent 兼容性
执行
Ad::where('id', $id)->update($attributes);
不会在模型中触发任何操作(因此不会在Algolia中发生更新)。这是因为这不是Eloquent调用。它只是生成模型背后隐藏的查询的便捷方式。
要使此查询与Algolia一起工作,您需要这样做
Ad::find($id)->update($attributes);
兼容性
兼容5.x应用程序