The need for fast, relevant, and personalized search experiences has become paramount. GraphCommerce, This integration offers developers a robust solution to enhance search capabilities while maintaining the flexibility and performance that GraphCommerce is known for.
In this article, we'll dive deep into the technical aspects of implementing Algolia in GraphCommerce, exploring the architecture, key components, and best practices for leveraging this powerful integration.
The Algolia integration in GraphCommerce is designed with a clear separation between the GraphQL layer and the frontend implementation. This architecture allows for seamless integration with existing GraphCommerce setups while providing the flexibility to customize and extend functionality as needed.
The is composed of five packages:
@graphcommerce/algolia-products
@graphcommerce/algolia-categories
@graphcommerce/algolia-insights
@graphcommerce/algolia-personalization
@graphcommerce/algolia-recommend
Algolia goes hand-in-hand with a proper search UI. We've got a separate 'Magento
Search Overlay' package that adds a search overlay experience to GraphCommerce.
This search overlay is not dependent on Algolia so you can use it with default
Magento. So before starting, install the @graphcommerce/magento-search-overlay
package first.
To integrate Magento's product catalog with GraphCommerce, you need to set up your Algolia indexes. This is typically done using the Magento 2 Algolia module, which handles the synchronization of product and category data.
Stores > configuration > Algolia Search > Products > Products
Stores > Configuration > Algolia Search > Instant Search Results Page > Facets
Stores > Products > Attributes
Stores > Configuration > Algolia Search > Instant Search Results Page > Use Virtual Replica
After this package is installed and configured the search is automatically replaced by Algolia's search.
This package is responsible for integrating Algolia's search functionality into GraphCommerce's GraphQL layer. It includes several key components:
Find current version of your @graphcommerce/next-ui
in your package.json. And
run yarn add @graphcommerce/algolia-products@9.0.0
(replace 9.0.0 with the
same version as the @graphcommerce/next-ui
)
To enable Algolia in your GraphCommerce project, you'll need to add the
necessary configuration to your graphcommerce.config.js
file:
module.exports = {
// Other configuration...
algolia: {
// Configure your Algolia application ID. [Algolia API Keys Dashboard](https://www.algolia.com/account/api-keys)
applicationId: '',
// Configure your Search API Key. [Algolia API Keys Dashboard](https://www.algolia.com/account/api-keys)
// Make sure the API key has the following ACL: search, listIndexes and settings [Lookup here](https://dashboard.algolia.com/account/api-keys/restricted)
searchOnlyApiKey: '',
// Stores > Configuration > Algolia Search > Credentials and Basic Setup > Index name prefix
indexNamePrefix: 'SAME_AS_MAGENTO',
// By default the catalog will not use algolia. Set this to true to enable Algolia for the catalog.
catalogEnabled: true,
},
}
When writing your own product queries, you can set the engine to algolia
to
use Algolia's search.
query AlgoliaProducts {
products(filter: { engine: { eq: "algolia" } }) {
items {
name
}
}
}
See the algoliaHitToMagentoProduct for all the fields that are returned.
This package is responsible for integrating Algolia's category search functionality into GraphCommerce's GraphQL layer.
We currenly do not support replacing the whole category tree with Algolia's categories, because only a reduced set of fields are indexed to algolia.
When writing your own category queries, you can set the engine to algolia
to
use Algolia's search.
query AlgoliaCategories {
categories(filters: { engine: { eq: "algolia" } }) {
items {
name
breadcrumbs {
category_name
}
}
}
}
See the algoliaHitToMagentoCategory for all the fields that are returned.
This package implements Algolia's insights features, which are used to track user interactions and improve search relevance. These events are crucial for enabling features like NeuralSearch, Dynamic Re-Ranking, Query Categorization, Analytics, A/B Testing, Recommend, and Personalization.
Data Sources > Events > Debugger
.The events as described in Algolia's event types page are sent to Algolia from the codebase.
The event flow implemented is as follows:
viewedObjectIDs
/viewedObjectIDsAfterSearch
is send.viewedFilters
/clickedFilters
are
send.clickedObjectIDs
/clickedObjectIDsAfterSearch
is send.addedToCartObjectIDs
/addedToCartObjectIDsAfterSearch
is send. We
automatically track where each item in the cart originated form. We track
every item in the cart and match them to the original query that was used to
find them.purchasedObjectIDs
/purchasedObjectIDsAfterSearch
is send. We
automatically track where each item in the purchase originated form. We track
every item in the purchase and match them to the original query that was used
to find them.Note: The convertedObjectIds
/convertedObjectIDsAfterSearch
are not
implemented, they are a simplified version of the
addedToCartObjectIDs
/addedToCartObjectIDsAfterSearch
The useSendAlgoliaEvent
hook in @graphcommerce/algolia-insights
is
responsible for sending these events to Algolia.
This package requires insights to be sent to Algolia for them to be able to
create personalized queries. Once you've collected several events, set up
personalization strategies in the Algolia dashboard under
Enhance > Personalization
.
This package implements Algolia's personalization features, including: Personalized search results and User token management for consistent identification
Note: This package is optional because this will increase the amount of API calls send to Algolia which can increase your costs when viewing lists of products that otherwise are static like related products, category queries etc.
This package requires insights to be sent to Algolia for them to be able to
create recommendations. Once you've collected several events, set up
recommendation strategies in the Algolia dashboard under Recommend > Models
.
This package implements Algolia's recommendation features, including:
The recommend package can replace various sections of Magento's default fields.
So this can be a drop-in replacement. You can configure which fields to replace
by updating your graphcommerce.config.js
:
module.exports = {
algolia: {
lookingSimilar: 'UPSELL_PRODUCTS',
frequentlyBoughtTogether: 'CROSSSELL_PRODUCTS',
relatedProducts: 'RELATED_PRODUCTS',
},
}
Besides this the features are also available as separate fields on products:
query AlgoliaProducts {
products(filter: { engine: { eq: "algolia" } }) {
items {
name
algolia_looking_similar {
name
}
algolia_related_products {
name
}
algolia_frequently_bought_together {
name
}
}
}
}
The Algolia integration in GraphCommerce supports multi-store and multi-language setups out of the box. It uses the indexes created by the Magento 2 Algolia module.
Stores > Configuration > Algolia Search > Instant Search Results Page
Number of values per facet
to 1000
or a value that is high enough to
fetch everyting at once.By default Algolia will index all products will have fields like, however those fields are NOT filterable.
visibility_search: 1
visibility_catalog: 1
To properly support the visibility filter the visibility attribute needs to be added to the index:
Stores > Configuration > Algolia Search > Instant Search Results Page
visibility
to Facets and set it to Filter only
${index}_query_suggestions
.const config = {
algolia: {
/*
* To enable Algolia suggestions, please provide the Suffix that is used for your suggestions index.
* For the index `magento2_demo_en_US_query_suggestions` this would be `_query_suggestions`
*/
suggestionsSuffix: '_query_suggestions,
},
}
⚠️ Warning: This does not use the Magento 2 Algolia suggestions feature. That feature only syncs Magento's suggestions to Algolia and doesn't use the Algolia Suggestions feature.
Stores > Configuration > Algolia Search > Credentials and Basic Setup
\Enable Query Suggestions Index
to No
.To configure sorting options for your Algolia-powered search, you need to enable Virtual Replicas in your Magento 2 Algolia module configuration:
Stores > Configuration > Algolia Search > Instant Search Results Page
This allows you to define different sorting options, each of which will be treated as a separate (virtual) index in Algolia.
const config = {
algolia: {
customerGroupPricingEnabled: true,
},
}
To enable customer group pricing, make sure customers groups prices are mapped
to algolia.
Stores > Configuration > Algolia Search > Advanced > Enable Customer Groups
.
⚠️ Warning: Magento needs to be configured the same as customerGroupPricingEnabled, else sorting index selection will not work properly for customer group pricing.
⚠️ Warning: Catalog price rules for a specific customer group do not seem to be
indexed. It seems only: [Product] > Advanced Pricing > Customer Group Price
gets indexed.
⚠️ Warning: The GraphQL API does not expose the customer group_id by default. To make this functionality work, an additional REST API call is made when loggin the customer in. This means a somewhat slower (few hundred ms) when the Customer is loaded.
By default algoliaFacetsToAggregations
and algoliaHitToMagentoProduct
are
split into it's own functions so plugins can be easily written.
The Algolia integration in GraphCommerce offers a powerful toolset for implementing advanced search and discovery features in your e-commerce application. By leveraging the separation of concerns between the GraphQL layer and frontend implementation, you can create a flexible, performant, and feature-rich search experience.
As you implement and customize the Algolia integration, remember to focus on performance, user experience, and data privacy. Regularly analyze search analytics and user behavior to continually refine and improve your search implementation.
For more information and detailed documentation, visit the GraphCommerce GitHub repository and Algolia's developer documentation. Happy coding!