Search/Algolia/
2022-10-17T07:03:56.654246Z
Published on
Algolia is an AI-powered search and discovery platform allowing you to create cutting-edge customer experiences for your ecommerce website and mobile apps. It is the perfect mediator between you and your customers to make sure the conversation is as smooth and efficient as possible.
Algolia account ( https://www.algolia.com )
Python ( above 3.8 )
If you don't have an Algolia account ( https://www.algolia.com), please proceed with the signup option, otherwise, log in with your existing credentials.
You can create multiple Algolia applications to have different environments for development, testing, and production. You can also use different indices within the same application for different environments. For example, If you’re using a single Algolia application, you can separate your production from your development and testing environments by creating separate indices.
To create an application, In the Algolia dashboard go to Settings > Applications > click New Application
After clicking New Application, Please fill the following details
Add your application name in the NAME YOUR APPLICATION field
Choose your subscription > Select Free
Click Next Step: Data Center
After clicking the Data Center, choose data center close to geographical location, my case I am choosing India and click Review Application Details
The next screen will show your application details. After Reviewing your application details, click Create Application
The application will be created. To get your application go to Settings > Applications
In the Algolia dashboard go to Settings > Applications > Select your application (i.e created in Step 2 :
Create an application )
Then, click API keys, In the API key section, find the Search-Only API Key and Admin API Key. Copy them and paste it into a local text file. These Key`s will be used to interact with Alogia API`s (The Python API client | Algolia)
In the Algolia dashboard go to Settings > Applications > Select your application (i.e created in Step 2 :
Create an application )
After selecting your application, click Search.
On the next screen click Create index, it will prompt with an index name request popup. Fill your index name and click Create
Install the Python API client using pip:
1pip install --upgrade 'algoliasearch>=2.0,<3.0'
To test whether you can connect to Algolia using python, run a simple program that adds a record to a new index.
Create an algolia_insert.py file and add the following code.
Note:* Please replace "YourApplicationID"
, "YourAdminAPIkey" and "YourIndexName"
with actual values obtained from Step 3: To get the API key.
1from algoliasearch.search_client import SearchClient
2
3client = SearchClient.create("<YourApplicationID>", "<YourAdminAPIkey>")
4index = client.init_index("<YourIndexName>")
5
6record = {"objectID": 1, "name": "test_record"}
7index.save_object(record)
Example Code is given in the following github repo file.
To check the record is stored, In the Algolia dashboard go to Application > Search. The record will be created.
Note:* Please replace "YourIndexName"
, "objectID"
with actual values
Create an algolia_get.py file and add the following code.
1from algoliasearch.search_client import SearchClient
2
3# replace with your ApplicationID and AdminAPIkey
4client = SearchClient.create("<YourApplicationID>", "<YourAdminAPIkey>")
5index = client.init_index('<YourIndexName>')
6
7data = index.get_object('<objectID>')
8print(data)
Example Code is given in the following github repo file.
Create an algolia_delete.py file and add the following code.
1from algoliasearch.search_client import SearchClient
2
3# replace with your ApplicationID and AdminAPIkey
4client = SearchClient.create("<YourApplicationID>", "<YourAdminAPIkey>")
5index = client.init_index('<YourIndexName>')
6
7index.delete_object("<objectID>")
Example Code is given in the following github repo file.
Create an algolia_insert.py file and add the following code.
1from algoliasearch.search_client import SearchClient
2
3# replace with your ApplicationID and AdminAPIkey
4client = SearchClient.create("<YourApplicationID>", "<YourAdminAPIkey>")
5index = client.init_index('<YourIndexName>')
6
7record = {"objectID": 1, "name": "test_record"}
8index.save_object(record)
Example Code is given in the following github repo file.
Create an algolia_update.py file and add the following code.
1from algoliasearch.search_client import SearchClient
2
3# replace with your ApplicationID and AdminAPIkey
4client = SearchClient.create("<YourApplicationID>", "<YourAdminAPIkey>")
5index = client.init_index('<YourIndexName>')
6
7record = {"objectID": 1, "name": "test_record_updated"}
8index.partial_update_object(record)
Example Code is given in the following github repo file.
Search in Algolia
1from algoliasearch.search_client import SearchClient
2
3# replace with your ApplicationID and AdminAPIkey
4client = SearchClient.create("<YourApplicationID>", "<YourAdminAPIkey>")
5index = client.init_index('<YourIndexName>')
6
7res = index.search('')
8print(res)
Example Code is given in the following github repo file.
Comments