Integrating Algolia
- Sign up for a free Algolia account here. 
- Log into your account. 
- On the Get Started page, create your first index with a desired name. 
- Retrieve your Algolia API keys by following these steps: - Go to Settings > Team and Access > API Keys and make a note of the following values: - Application ID
- Search API Key
- Admin API Key
 
- Add your API keys to the - scripts/.envfile and setup your index name as in the following example for creating an index named demo_products:- ALGOLIA_APP_ID=YOUR_APPLICATION_ID
 ALGOLIA_API_ADMIN_KEY=YOUR_WRITE_API_KEY
 ALGOLIA_INDEX_NAME=demo_products
- Add your Application ID, Search API Key, and index name to the - /composable-ui/.envfile as in the following example:- ```
 NEXT_PUBLIC_ALGOLIA_APP_ID=YOUR_APPLICATION_ID
 NEXT_PUBLIC_ALGOLIA_API_SEARCH_KEY=YOUR_SEARCH_API_KEY
 NEXT_PUBLIC_ALGOLIA_INDEX_NAME=demo_products
 ```info- Do not include your Admin API Key in - /composable-ui/.env. This key is not required for your Next.js application. For more information about public environment variables in Next.js, see the Exposing Environment Variables to the Browser page.
 
- Configure and populate the new index using one of the following options: 
Configuring and Populating the New Index Using the Script
Composable UI offers a preconfigured script located in the scripts directory. This script performs the following tasks:
- Creates a primary index in Algolia.
- Configures the index by specifying searchable attributes, facets, and replicas.
- Populates the index with product data.
Prerequisites
- Create an .envfile in the root of thescriptsdirectory and set the following variables:
ALGOLIA_APP_ID
ALGOLIA_API_ADMIN_KEY
ALGOLIA_INDEX_NAME
You can use the .env.example file as a template.
Procedure
- Open the terminal and navigate to the scriptsdirectory.
- In the scriptsdirectory, run the following command:pnpm install
- To set up Algolia, run the following command:pnpm algolia-setup
Configuring the Algolia indexes manually
Configuring the Algolia indexes manually requires more time and effort compared to other methods.
- Import products by doing the following: - If you haven't already created an index, in the Algolia Getting Started page, create a new index. - This is used as the - ALGOLIA_INDEX_NAMEwhen configuring the replicas in step 3.
- In the Import your records section, click Upload your records > Upload file. 
- Drag and drop the JSON file, - packages/commerce-generic/src/data/products.json, that contains the demo products. The Configure page for your index is displayed.
 
- To configure searchable attributes and facets, in the Configure > Index page, click the Configuration tab and do the following: - In RELEVANCE ESSENTIALS -> Searchable attributes tab, click Add a searchable attribute and add the following attributes: - name.
- category.
 - These attributed are used as the searchable attributes. 
- In FILTERING AND FACETING tab, click Facets > Add an Attribute and add the following attributes: - type
- brand
- price
 - These attributes are used for faceted search in your application. 
 
- To create replicas for different sorting options, do the following: - In the top menu, click the New... drop-down menu. 
- Click Replica. - The Create a new replica window is displayed with the default replica type set as Standard Replica (default). 
- Create four replicas, - newest,- priceDesc,- priceAscand- nameAsc, in the following format:- {ALGOLIA_INDEX_NAME}_{SORTING_OPTION}. For example, if your- ALGOLIA_INDEX_NAMEis- MyFirstIndex:- MyFirstIndex_newest
- MyFirstIndex_priceDesc
- MyFirstIndex_priceAsc
- MyFirstIndex_nameAsc
 
 
- For each Replica, to configure sorting to match the name of the index, doo the following: - Go to Configuration and click RELEVANCE ESSENTIALS > Ranking and Sorting.s
- Click + Add sort-by attribute, enter the field name.
- Select Ascending or Descending for the sorting order.
You can create different sorting options by modifying the ALGOLIA_SORTING_OPTIONSconstant in the application.
 
 To ensure the proper functioning of Composable UI's search feature, the ALGOLIA_INDEX_NAME value in your .env file and your Algolia index configuration must match. If these values do not match, Composable UI's search functionality will fail.