Skip to main content

Autocomplete

The Autocomplete feature allows you to retrieve the most relevant products and categories based on a user's search query. This guide outlines how to use this feature to enhance search functionality in your application. By sending a GET request with a specific search text to the provided endpoint, you can retrieve a structured response containing product and category suggestions. This guide also provides insights into filtering options and customization settings to optimize search results for your application.

GET Most Relevant Products via Searched Text

Returns the most relevant products and categories upon searching the word sent with ‘search_text.’

Path: https://{storefront_url}/autocomplete/?search_text=<SEARCH_TEXT>

Example Response

{
"groups": [
{
"entries": [
{
"label": "Electro Guitar",
"url": "/electro-guitar/",
"suggestion_type": "category",
"extra": {
"category_id": 3,
"parent_categories": [
"Music"
]
}
},

],
"suggestion_type": "Category"
},
{
"entries": [
{
"label": "Bass Guitar",
"url": "/bass-guitar/",
"suggestion_type": "product",
"extra": {
"image": null,
"retail_price": 249.98,
"product_type": 1,
"price": 99.99
}
},

],
"suggestion_type": "Product"
}
]
}

Filtering is limited to the first four words of the search text; any additional words are disregarded.

Additionally, there are customizable settings available to refine the filtering of the most relevant products and categories. These settings are evaluated using the and logical operator in conjunction with the search text. The following sections provide insights into these settings.

Product Filtering

For products, the provided search text is compared to the name field as an exact match and the search_text field as a prefix match, utilizing the or logical operator.

Category Filtering

When filtering categories, the provided search text is divided by spaces, and each word is compared to the name field of the categories using a case-insensitive contain lookup operator. The results are then evaluated with the and logical operator.

Custom Settings

The CATEGORY_PRE_FILTER is used to configure Django Q object or its combination.

Example

CATEGORY_PRE_FILTER = Q(path__startswith='000100010') | Q(order=1)

In this example, the system will return categories that match the search text and meet one of two conditions: either the path starts with 000100010 or the order equals 1.