Download the PHP package algolia/algoliasearch-laravel without Composer
On this page you can find all versions of the php package algolia/algoliasearch-laravel. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package algoliasearch-laravel
[DEPRECATED] Algolia Search API Client for Laravel
Algolia Search is a hosted full-text, numerical, and faceted search engine capable of delivering realtime results from the first keystroke.
This package is deprecated, we recommend you to use Laravel Scout. If you want to extend Scout capabilities, please refer to our dedicated documentation
This PHP package integrates the Algolia Search API into the Laravel Eloquent ORM. It's based on the algoliasearch-client-php package.
Note: If you're using Laravel 4, checkout the algoliasearch-laravel-4 repository.
API Documentation
You can find the full reference on Algolia's website.
Table of Contents
-
Install
- Install via composer
- Service provider
- Publish vendor
-
Quick Start
- Quick Start
- Ranking & Relevance
- Frontend Search (realtime experience)
- Backend Search
-
Options
- Auto-indexing & Asynchronism
- Custom Index Name
- Per-environment Indexes
- Custom
objectID
- Restrict Indexing to a Subset of Your Data
-
Relationships
- Relationships
-
Indexing
- Manual Indexing
- Manual Removal
- Reindexing
- Clearing an Index
-
Manage indices
- Primary/Replica
- Target Multiple Indexes
-
Eloquent compatibility
- Eloquent compatibility
- Compatibility
Install
Install via composer
Add algolia/algoliasearch-laravel
to your composer.json
file:
Service provider
Add the service provider to config/app.php
in the providers
array.
Publish vendor
Laravel Algolia requires a connection configuration. To get started, you'll need to publish all vendor assets:
You can add the option to only publish assets of the Algolia package.
This will create a config/algolia.php
file in your app that you can modify to set your configuration. Also, make sure you check for changes compared to the original config file after an upgrade.
Quick Start
Quick Start
The following code adds search capabilities to your Contact
model creating a Contact
index:
By default all visible attributes are sent. If you want to send specific attributes you can do something like:
After setting up your model, you need to manually do an initial import of your data. You can do this by calling reindex
on your model class. Using our previous example, this would be:
Ranking & Relevance
We provide many ways to configure your index settings to tune the overall relevancy, but the most important ones are the searchable attributes and the attributes reflecting the record popularity. You can configure them with the following code:
You can propagate (save) the settings to algolia by using the setSetting
method:
Synonyms
Synonyms are used to tell the engine about words or expressions that should be considered equal in regard to the textual relevance.
Our synonyms API has been designed to manage as easily as possible a large set of synonyms for an index and its replicas.
You can use the synonyms API by adding a synonyms
in $algoliaSettings
class property like this:
You can propagate (save) the settings to algolia using the setSetting
method:
Frontend Search (realtime experience)
Traditional search implementations tend to have search logic and functionality on the backend. This made sense when the search experience consisted of a user entering a search query, executing that search, and then being redirected to a search result page.
Implementing search on the backend is no longer necessary. In fact, in most cases it is harmful to performance because of the extra network and processing latency. We highly recommend the usage of our JavaScript API Client issuing all search requests directly from the end user's browser, mobile device, or client. It will reduce the overall search latency while offloading your servers at the same time.
In your JavaScript code you can do:
Backend Search
You could also use the search
method, but it's not recommended to implement an instant/realtime search experience from the backend (having a frontend search gives a better user experience):
Options
Auto-indexing & Asynchronism
Each time a record is saved; it will be - asynchronously - indexed. On the other hand, each time a record is destroyed, it will be - asynchronously - removed from the index.
You can disable the auto-indexing and auto-removing by setting the following options:
You can temporarily disable auto-indexing. This is often done for performance reasons.
You can also make a dynamic condition for those two parameters by creating an autoIndex
and/or autoDelete method
on your model
Be careful to define those two methods in AlgoliaEloquentTrait. When putting those methods in a parent class they will be "erased" by AlgoliaEloquentTrait if used in a child class (because of php inheritance).
Custom Index Name
By default, the index name will be the pluralized class name, e.g. "Contacts". You can customize the index name by using the $indices
option:
Per-environment Indexes
You can suffix the index name with the current App environment using the following option:
Custom objectID
By default, the objectID
is based on your record's keyName
(id
by default). You can change this behavior specifying the objectIdKey
option (be sure to use a uniq field).
Restrict Indexing to a Subset of Your Data
You can add constraints controlling if a record must be indexed by defining the indexOnly()
method.
Relationships
Relationships
By default the Algolia package will fetch the loaded relationships.
If you want to index records that haven't yet loaded any relations, you can do it by loading them in the that you can create in your model.
It will look like:
In the resulted object, you will have categories converted to array by Laravel. If you want a custom relation structure you will instead do something like:
Indexing
Visibility
By default, Algolia will only be able to access visible attributes of your model. So, for example, you will receive a No content in PUT request
exception when using this example code, because invisible_attribute
key returns an empty/null variable.
Before Indexing, be sure to have correctly listed your visible attributes. To bypass this safety mask imposed by Laravel, you may use $this->attributes['invisible_attribute']
to access directly to the attribute even if is not visible, but the recommendation is to avoid this type of access to attributes in your Model.
Manual Indexing
You can trigger indexing using the pushToIndex
instance method.
Manual Removal
And trigger the removal using the removeFromIndex
instance method.
Reindexing
To safely reindex all your records (index to a temporary index + move the temporary index to the current one atomically), use the reindex
class method:
To reindex all your records (in place, without deleting outdated records):
To set settings during the reindexing process:
To keep settings that you set on the Algolia dashboard when reindexing and changing settings:
To implement a callback that gets called everytime a batch of entities is indexed:
Clearing an Index
To clear an index, use the clearIndices
class method:
Manage indices
Primary/Replica
You can define replica indexes using the $algolia_settings
variable:
To search using a replica, use the following code:
Target Multiple Indexes
You can index a record in several indexes using the $indices
property:
To search using an extra index, use the following code:
Eloquent compatibility
Eloquent compatibility
Doing:
will not trigger anything in the model (so no update will happen in Algolia). This is because it is not an Eloquent call. It is just a convenient way to generate the query hidden behind the model.
To make this query work with Algolia you need to do it like this:
Compatibility
Compatible with 5.x applications