Before you begin
You need:- An Algolia account
- PHP 8.2 or later for Symfony 7, or PHP 8.4 or later for Symfony 8
- The PHP
pdo_sqliteextension - Symfony 7 or 8
- Composer
Set up the bundle
Set up the bundle in a new Symfony app. Then index and search an entity.1
Create a Symfony app
To create a new Symfony app and add Doctrine, run:To store the data of this test in a local SQLite database,
add the following line to a
.env.local file:.env.local
2
Install the bundle
To add the bundle to your To register the bundle, add the following entry to
composer.json file and install it, run:config/bundles.php:config/bundles.php
3
Add your credentials
The bundle needs your Algolia and .
It loads them from the Replace
ALGOLIA_APP_ID and ALGOLIA_API_KEY environment variables.
To set them, add the following lines to your .env.local file:.env.local
ALGOLIA_APPLICATION_ID and ALGOLIA_API_KEY with your Algolia credentials.
Find both in the Algolia dashboard.
To run this test, use an API key with the search and addObject ACL.4
Create an entity
To create a To create the database table, run:
Contact entity, add the following file:src/Entity/Contact.php
5
Connect the entity to Algolia
To send your entity to Algolia, add it to the bundle’s configuration.
Create the following file:By default, the bundle prefixes index names with the Symfony environment followed by an underscore character:
config/packages/algolia_search.yaml
_.
In the dev environment, it stores the entity’s records in an index named dev_contacts.
To change the prefix, set the prefix option.6
Index a record
To add a record to the database, run:To send all If the setup works, the command prints
Contact entities to Algolia, run:Indexed 1 / 1 App\Entity\Contact entities into dev_contacts index.7
Search the record
To search from your app, add a controller that uses the Algolia indexes records asynchronously, so wait 5 seconds.
Then, to start a local server, run:In a second terminal, to search for the record, run:If the setup works, the command prints
SearchService:src/Controller/SearchController.php
["Jon Doe"].Each run of search:import updates the same record, so your test index stays small.To remove the indexed test data, delete the dev_contacts index in the Algolia dashboard or with the Algolia CLI.
This deletes the index and its settings.
The contact is stored separately in your SQLite database.Keep your index in sync
After you configure an index, the bundle updates it whenever you create, update, or delete entities through Doctrine. Raw SQL bypasses Doctrine. After inserting or updating rows with SQL, runsearch:import.
To learn more about importing data, including zero-downtime reindexing,
see Send data to Algolia.
Use more bundle commands
Commands such assearch:clear and search:settings:push need more permissions than the test in this guide.
To run them, use an API key with the matching ACL, such as deleteObject and editSettings.
Next steps
- Configure your index: set index settings from your Symfony app
- Choose what to index: control which data the bundle indexes
- Server-side search: search your entities from Symfony