Since May 1st, 2024, Apple requires all iOS apps to include a privacy manifest.
For more details, see Privacy Manifest.
A quick overview on how InstantSearch works
InstantSearch, as you probably know, offers reactive UI widgets that automatically update when new search events occur. Internally, it uses aSearchable interface that takes care of making network calls to get search results.
The most important method of that Searchable is a simple search() function that takes in a parameter that contains all the search query parameters and then expects a callback to be called with the search results that you get from your backend.
Basic implementation of using a custom backend
The most basic implementation of using a custom backend uses theDefaultSearchClient and requires you to implement just one method:
search(query:searchResultsHandler:).
In this function, you use the query passed to you, make a network request to your backend server, transform the response into a SearchResults instance, and then finally call the searchResultsHandler callback with searchResults.
If there’s an error, call the callback with the error.
Here is an example using the Alamofire networking library.
Swift
- Get the query text from the Query parameter that is passed in the method.
- Make your request to your backend using the
queryTextparse in step 1. - Serialize your response into a
SearchResultsinstance. If your response data is different than the original one returned by Algolia, especially if where you’re not using Algolia at all in your backend, use aSearchResultsinitializer such asSearchResults(nbHits:hits). - Call the
searchResultsHandlerfunction to instruct InstantSearch about the new search event, in this case the arrival of new search results, or an error.
Integrate the custom backend
-
To use the custom backend with InstantSearch core only (the
Searcherclass):Swift -
To use the custom backend with InstantSearch with a single index:
Swift
-
To use the custom backend with InstantSearch with multiple indices:
Swift
Advanced implementation of using a custom backend
The above snippet only covers the case of doing a basic search of hits, with conjunctive (contrary to disjunctive) filtering. Here, we’ll take a look at improving the structure of our custom backend class, as well as supporting disjunctive faceting. Start with this code snippet:Swift
- Create your models that will hold the query parameters and results that you need to make your custom backend call.
- Create your class that inherits from
SearchClient. Use the two models created in step 1 for the generics of that class. This will ensure strong typing and good practices throughout this implementation. - Implement the basic parameter mapper function that converts a query to your parameter model. Make sure you take all the fields you need from the query parameter.
- Implement the advanced parameter mapper function. It’s the same as the one used in step 3, but with two more parameters that you can use for your call:
disjunctiveFacetsandrefinements. - Implement the result mapper function that converts your result model back to an Algolia
SearchResultsthat can be understood by InstantSearch. - If you want to specify the possible facets for a refinement list, make sure you specify the facets property appropriately. In the code snippet, we just give an example, but usually you’ll want to get this data from your custom result model.
- Implement the search method, same idea as the basic implementation. The only difference is that now it provides your custom parameter model as its parameter.
- When you get new search results, you serialize them into your custom response model and then call the
searchResultsHandlermethod.
Integrate the custom backend
-
To use the custom backend with InstantSearch core only (the
Searcherclass):Swift -
To use the custom backend with InstantSearch with a single index:
Swift
-
To use the custom backend with InstantSearch with multiple indices:
Swift
Trick to get more out of the query
One little trick you can use to get more detailed information about thequery being passed as a parameter is to upcast it to a SearchParameters by doing
Swift
disjunctiveFacets, facetRefinements, disjunctiveNumerics and numericRefinements. This can be useful when transforming Algolia’s Query.