Download the PHP package leinonen/yii2-algolia without Composer
On this page you can find all versions of the php package leinonen/yii2-algolia. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package yii2-algolia
Yii2 Algolia
Yii2 Algolia is an Algolia bridge for Yii2. It uses the official Algolia Search API package.
Table of contents
- Installation
- Usage
- ActiveRecordHelpers
- Indexing
- Manual Indexing
- Manual Removal
- Manual Updating
- Reindexing
- Clearing Indices
- Auto-indexing
- Backend search
- Using multiple environments
- Contributing
Installation
Require this package, with Composer, in the root directory of your project.
Configuration
Add the component to your application config. Also bootstrap the component.
Usage
The preferred way of using the package is through dependency injection. Just inject the leinonen\Yii2Algolia\AlgoliaManager
.
It has all the same methods available as the official Algolia Client (AlgoliaSearch\Client
). The documentation can be found here. The manager class delegates all the methods to the original Client and provides some addittional helpers on top.
You can also access the manager like a Yii component.
ActiveRecord Helpers
This package also provides helpers for dealing with Yii's ActiveRecord Models.
Configuring an ActiveRecord Class
To use the helpers just implement the leinonen\Yii2Algolia\SearchableInterface
. The leinonen\Yii2Algolia\Searchable
trait provides everything that you need. You can control what fields are indexed to Algolia by using the fields()
and extraFields()
methods like you normally would. You can also override the getAlgoliaRecord()
for more custom use cases.
By default the helpers will use the class name as the name of the index. You can also specify the indices you want to sync the class to:
By default the model is converted into an array in background with Yii's toArray()
method. If you want to customize it you can override the getAlgoliaRecord() method
.
For providing the Algolia's ObjectID the package uses getObjectID()
method from the Searchable model. The helper trait will use ActiveRecord's getPrimaryKey()
method by default. If you want to use some other key just override the method (or implement your own if you aren't using the trait):
You can also also implement the leinonen\Yii2Algolia\SearchableInterface
for plain old PHP objects and then use the leinonen\Yii2Algolia\AlgoliaManager
to control them. Note that all helpers are not available for use other than with ActiveRecord classes.
Indexing
Manual Indexing
You can trigger indexing using the index()
instance method on an ActiveRecord model with the help of leinonen\Yii2Algolia\Searchable
trait.
Or if you fancy a more service like architecture, you can use the methods on leinonen\Yii2Algolia\AlgoliaManager
:
It's also possible to index multiple models of the same class in a batch with the service's pushMultipleToIndices()
.
Manual Removal
Removing is triggered using the removeFromIndices()
instance method.
Or with the service:
It's also possible to delete multiple models of the same class in a batch with the service's removeMultipleFromIndices()
Manual Updating
Update is triggered using the updateInIndices()
instance method.
Or with the service:
It's also possible to update multiple models of the same class in a batch with the service's updateMultipleInIndices()
.
Reindexing
To safely reindex all your ActiveRecord models(index to a temporary index + move the temporary index to the current one), use the leinonen\Yii2Algolia\AlgoliaManager::reindex()
method:
You can also use the static method on ActiveRecord class if you prefer Yii's style:
In background reindexing is done by chunking through all of the ActiveRecord models of the given class, 500 objects at time. This means you can safely use reindexing even over really large datasets without consuming too much memory. Just mind your Algolia quota.
Reindexing By ActiveQuery
If you need to index a lot of related relationships to Algolia you can use the powerful reindexByActiveQuery()
method found in leinonen\Yii2Algolia\AlgoliaManager
class:
The reindexByActiveQuery()
method also uses chunking in background, so it's safe to do query's over really big datasets. The indices to be reindexed will be resolved from the result of the queries models.
To get the relations indexed into Algolia you of course need to also modify the getAlgoliaRecord()
or the fields()
method from the ActiveRecord model.
Yii provides a handy isRelationPopulated()
method for customizing this:
Reindexing With a set of explicit SearchableModels
It's also possible to explicitly define which objects should be reindexed. This can be done by using reindexOnly()
method found in leinonen\Yii2Algolia\AlgoliaManager
class:
In the background the method figures out the indices of that need to be reindexed and therefore the array must consist only models of a same class. Be wary of the memory consumption if you are fetching a lot of ActiveRecords this way.
Clearing Indices
To clear indices where the ActiveRecord is synced to, use the clearIndices()
method found in leinonen\Yii2Algolia\AlgoliaManager
class:
You can also use the static method on ActiveRecord class if you prefer Yii's style:
Auto-indexing
Another solution is to attach the leinonen\Yii2Algolia\ActiveRecord\SynchronousAutoIndexBehavior
behavior to the ActiveRecord model. This behavior will then trigger automatically when the model is created, updated or deleted. The model needs of course to implement the leinonen\Yii2Algolia\SearchableInterface
via the mentioned trait or your custom methods.
Beware that the Algolia API will be called every time separately when something happens to the specified ActiveRecord model. This can cause performance issues. At the moment Yii2 doesn't provide queues out of the box, so asynchronous updating isn't available.
Configuration
You can also explicitly turn off events for insert, update or delete with props afterInsert
, afterUpdate
, afterDelete
:
Backend Search
Like Algolia I would strongly recommend using Algolia's JavaScript client for the best search experience. You can however use some helpers for doing search on php side:
With the service:
The method also accepts optional search parameters:
Searching is also available from a ActiveRecord class that uses the leinonen\Yii2Algolia\ActiveRecord\Searchable
trait:
Using multiple environments
You can automatically prefix all the index names with the current App environment using the following configuration:
Then when using any of the helpers methods from leinonen\Yii2Algolia\AlgoliaManager
the environment will be prefixed to the index name. Also using the helper methods found on leinonen\Yii2Algolia\Searchable
trait will work. Note if you use methods straight from The Official Algolia Client the env config will have no effect.
Contributing
Pull requests are welcome! Have a look at the CONTRIBUTING.md document for some instructions.