Learn how to reshape your Autocomplete sources to create richer search experiences.
Autocomplete is also available as an experimental widget in InstantSearch,
making it easier to integrate into your search experience.
For more information,
see the API reference for InstantSearch.js or
React InstantSearch.
When you’re browsing a website that fetches content from a database,
the UI isn’t fully representative of how that data is structured on the backend.
This allows more human-friendly experiences and interactions.
A search UI doesn’t have to be a one-to-one mapping with your search engine either.Autocomplete lets you transform static,
dynamic,
and asynchronous sources into a friendlier search UI.Here are some examples of what you can do with the Reshape API:
Apply a limit of items for a group of sources
Remove duplicates in a group of sources
Group sources by an attribute
Sort sources
In this guide, you’ll learn how to use multiple reshape functions to remove duplicates and create a shared limit for your autocomplete suggestions.
To remove duplicates between sources, you can start by creating a uniqBy function.
You can later apply this function to your recent searches
and Query Suggestions sources.
Autocomplete supports conditional sources: sources sometimes exist and sometimes don’t.
This can happen when you display a source on an empty but not when users start typing
(for example, with a popular ).
Your function can support this by removing non-existent sources with filter(Boolean).
The uniqBy function uses item.query as identifier for the Query Suggestions plugin
and item.label for the recent searches plugin.
These are the shape of the items that the plugins return.
If you use custom sources, you can use a switch statement based on source.sourceId.Sources are retrieved from sourcesBySourceId with their sourceId using object destructuring.
You can return sources that you didn’t reshape with Object.values(rest).
You can create a function that combines results from two different sources.
Use it with uniqBy to ensure there are no duplicates and there’s a fixed number of combined items.In the video, four results are always displayed and the number of Query Suggestions varies depending on the number of recent searches.Here’s a limit function: