Search code examples
iosswiftalgoliainstantsearch

How to configure the Algolia InstantSearch class if you have 2 different search bars for 2 distinct indexes?


I'm using InstantSearch from Algolia and it's basically a Singleton that you configure and bind to a search bar (a widget). The problem is that in their demo, the InstantSearch class is a Singleton and therefore can't be instantiated multiple times. It can however be configured multiple times but then changes it's configuration for your whole app.

InstantSearch.shared.configure(
                    appID: algoliaAppID,
                    apiKey: key,
                    index: algoliaUserIndex
                )

One solution they offer is Multiple indexes search but it's an aggregation of Indexes when in my case would like to have simply different isolated searches.

for the multiple indexes reference:

let searcherIds = [SearcherId(index: algoliaUserIndex),
                                   SearcherId(index: algoliaMessageSessionsIndex)]

InstantSearch.shared.configure(appID: algoliaAppID, 
                               apiKey: key, 
                               searcherIds: searcherIds)

So, my question is: How can I have two different Search bars each searching a different index?


Solution

  • By looking at their code You can have another instance of InstantSearch using their convenience init method by providing the required fields. below are the Four init methods they provide:

    /// Create a new InstantSearch reference with the given configurations.
    ///
    /// - parameter appID: the Algolia AppID.
    /// - parameter apiKey: the Algolia ApiKey.
    /// - parameter index: the name of the index.
    @objc public convenience init(appID: String, apiKey: String, index: String) {
        self.init()
        self.configure(appID: appID, apiKey: apiKey, index: index)
    }
    
    /// Create a new InstantSearch reference.
    ///
    /// - parameter searcher: the `Searcher` used by InstantSearch
    @objc public convenience init(searcher: Searcher) {
        self.init()
        configure(searcher: searcher)
    }
    
    // Multi-Index init
    
    /// Create a new InstantSearch reference with the given configurations.
    ///
    /// - parameter appID: the Algolia AppID.
    /// - parameter apiKey: the Algolia ApiKey.
    /// - parameter indexIds: the identifications for each index
    @objc public convenience init(appID: String, apiKey: String, searcherIds: [SearcherId]) {
        self.init()
        self.configure(appID: appID, apiKey: apiKey, searcherIds: searcherIds)
    }
    
    /// Create a new InstantSearch reference with the given configurations.
    ///
    /// - parameter searchables: an array of searchables
    /// - parameter searchersIds: an array of searcherId that identifies a specific index
    @objc public convenience init(searchables: [Searchable], searcherIds: [SearcherId]) {
        self.init()
        self.configure(searchables: searchables, searcherIds: searcherIds)
    }
    

    Now it's upto you if you want to create instance for one view controller or for the whole app