- The primary search interface utilizes a standard index.
- As users start typing a phrase, suggestions from your Query Suggestions index are dynamically presented.
Usage
To effectively display the query suggestions- Create a Query Suggestions index specifically for storing and retrieving query suggestions.
- Integrate your main index and the Query Suggestions index into the search experience. This lets you fetch search results from the main index while simultaneously fetching and displaying relevant query suggestions from the Query Suggestions index.
- When a user selects a suggestion from the displayed options, update the search query to match the chosen suggestion. This ensures that the search results align with the selected suggestion, providing a seamless and efficient search experience.
Before you begin
To use InstantSearch iOS, you need an Algolia account. Either create a new account or use the following credentials:- Application ID:
latency
- Search API key:
af044fb0788d6bb15f807e4420592bc5
- Results index name:
instant_search
- Suggestions index name:
query_suggestions
Expected behavior
The initial screen shows the search box and results for an empty query:



- To implement search and suggestions in your app, SwiftUI offers a convenient set of components that can be utilized. These components will play a crucial role in the upcoming steps of this guide.
- To represent a suggestions search record, the
QuerySuggestion
model object is available in the InstantSearch Core library.
Project structure
Algolia’s query suggestions feature relies on two essential components:SearchViewModel
: This view model encapsulates all the search logic for your app. It handles tasks such as handling user input, querying the Algolia API, managing search results, and processing query suggestions.SearchView
: This SwiftUI view is responsible for presenting the search interface to users. It uses theSearchViewModel
to display search results, query suggestions, and other relevant information. TheSearchView
acts as the user-facing component that allows users to interact with the search.
Model object
To represent the items in your index, you can declare theItem
model object with the following code:
Swift
Item
struct is defined with two properties:
name
: A String property that represents the name of the item.image
: A URL property that holds the URL for the image associated with the item. By conforming to theCodable
protocol, theItem
struct can be easily encoded and decoded from JSON.
Result views
The ItemHitRow is a view that represents a row in the results list, rendering an item. Here’s the code for the ItemHitRow view:Swift
Hit<Item>
, representing a search hit containing an Item
object.
Search view model
To create a view model that encompasses all the logic for the search interface with query suggestions, subclassObservableObject
and define the necessary properties and the init
method.
Swift
SearchViewModel
class:
- Inherits from
ObservableObject
to enable SwiftUI to observe and update the view when the underlying data changes. - Declares two properties,
itemsSearcher
andsuggestionsSearcher
, of typeHitsSearcher
. These searchers will be responsible for querying the Algolia indices for searchable items and query suggestions, respectively. - In the
init
method, theappID
andapiKey
are set to the appropriate values for your Algolia app. Then, theitemsSearcher
andsuggestionsSearcher
instances are created, passing theappID
,apiKey
, and the relevant index names.
Swift
init
method, after initializing itemsSearcher
, the paginatedData(of:)
method is called on itemsSearcher
. It creates an PaginatedDataViewModel
specifically for Hit<Item>
objects. This view model is then assigned to the hits property.
To define the published properties searchQuery
and suggestions
in the SearchViewModel
that will be used in the SwiftUI view, you can update the class as follows:
Swift
- The
searchQuery
property is marked with @Published to make it observable and automatically update the SwiftUI view when its value changes. - The
suggestions
property is also marked with @Published to make it observable and update the SwiftUI view when its value changes. It is an array ofQuerySuggestion
objects, which will serve as the storage for the suggestions list to be displayed.
suggestionsSearcher
, and to include the necessary subscription logic, you can modify the SearchViewModel as follows:
Swift
SearchViewModel
:
- The
suggestionsSubscription
property is introduced as aCancellable
object to hold the subscription to thesuggestionsSearcher
results. - Inside the init method, the
suggestionsSubscription
is assigned the subscription to thesuggestionsSearcher
results. The closure within the subscription updates the suggestions property by extracting the hits from the latest search response. If an error occurs during extraction, an empty suggestions array is assigned. - In the
deinit
method, thecancel()
method is called onsuggestionsSubscription
to unsubscribe and cancel the subscription when theSearchViewModel
destroyed.
SearchViewModel
declared as a StateObject
:
Swift
InfiniteList
view coming with InstantSearch SwiftUI library to show the search results for empty query.
Launch the preview to see the results list.
Swift
.searchable
modifier to the InfiniteList
using the searchQuery
published property of the view model and set the search prompt.
Swift
SearchViewModel
by adding the didSet
property observer to searchQuery
and
add the notifyQueryChanged
function that launches the search.
Swift
suggestions
parameter of the searchable
modifier.
Use the ForEach
structure with the SuggestionRow
provided by the InstantSearch SwiftUI
.
Swift
submitSearch
method which clear suggestions list to make it disappear and launches the search on the itemsSearcher
.
Swift
didSubmitSuggestion
flag, which is set when a suggestion from the list has just been submitted.
Update the notifyQueryChanged
method to submit search if a suggestion has been submitted and toggle the didSubmitSuggestion
flag.
If the suggestions hasn’t been submitted, it triggers the search on both searchers.
Swift
completeSuggestion
and submitSuggestion
methods to handle actions from the suggestion row:
Swift
SearchView
implementation by assigning the SuggestionRow
actions callbacks and adding onSubmit
modifier.
Swift