episyche logo

Episyche

Search/Algolia/

How to create a search application using Algolia and python?

Published on

How to create a search application using Algolia and python?
Algolia is an AI-powered search and discovery platform, using Algolia we can develop numerous search applications. In this Blog, we are gonna discuss how to develop a simple search application with Algolia and Python.

Introduction:

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.

Flow diagram:

 

flow diagram

Prerequisites :

Steps :

Step 1: Signup and login to Algolia

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.

Step 2: Create an application

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



settings


application


create

  • 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



create

 

  • 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




location 1


create

  • The application will be created. To get your application go to Settings > Applications

Step 3: To get the API key

  • 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 ()




api key


api key

Step 4: Create an index

  • 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




create index


create index


index

Step 5: Integrate Algolia with python

  • 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.



index

Step 6: Algolia Index CRUD Operation example scripts.

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.

Result:



result

Comments