UI libraries / InstantSearch iOS / Widgets

About this widget

Objects conforming to the Searcher protocol manage the search sessions.

Algolia provides you with several out of the box searchers to help build your InstantSearch experience:

  • HitsSearcher: searches a single index.
  • FacetSearcher: searches for facet values.
  • MultiSearcher: aggregates the hits and facet searchers. This is useful for a federated search, or query suggestions .
  • AnswersSearcher: searches an index for Answers.

Examples

Instantiate a HitsSearcher.

1
2
3
4
let searcher = HitsSearcher(appID: "YourApplicationID",
                            apiKey: "YourSearchOnlyApiKey",
                            indexName: "index_name",
                            isAutoSendingHitsViewEvents: true)

Instantiate a FacetSearcher.

1
2
3
4
let searcher = FacetSearcher(appID: "YourApplicationID",
                             apiKey: "YourSearchOnlyApiKey",
                             indexName: "index_name",
                             facetName: "facet_name")

Instantiate a MultiSearcher.

1
2
3
4
5
6
let searcher = MultiSearcher(appID: "YourApplicationID",
                             apiKey: "YourSearchOnlyApiKey")
searcher.addHitsSearcher(indexName: "index_name1")
searcher.addFacetsSearcher(indexName: "index_name1", 
                            attribute: "facet_name")
searcher.addHitsSearcher(indexName: "index_name2")

Instantiate a AnswersSearcher

1
2
3
let searcher = AnswersSearcher(appID: "YourApplicationID", 
                               apiKey: "YourSearchOnlyApiKey", 
                               indexName: "index_name")

HitsSearcher

Parameter Description
indexName
type: String
Required

The index to search.

1
2
3
let searcher = HitsSearcher(appID: "YourApplicationID",
                            apiKey: "YourSearchOnlyApiKey",
                            indexName: "index_name")
query
type: Query
default: Query()
Optional

The query used when doing a search.

1
2
3
4
5
6
let query = Query()
query.analytics = true
let searcher = HitsSearcher(appID: "YourApplicationID",
                            apiKey: "YourSearchOnlyApiKey",
                            indexName: "index_name",
                            query: query)
isAutoSendingHitsViewEvents
since: v7.26.2
type: Bool
default: false
Optional

Whether to automatically send view events for hits.

1
2
3
4
let searcher = HitsSearcher(appID: "YourApplicationID",
                            apiKey: "YourSearchOnlyApiKey",
                            indexName: "index_name",
                            isAutoSendingHitsViewEvents: true)

FacetSearcher

Parameter Description
indexName
type: String
Required

The index to search into.

1
2
3
let searcher = FacetSearcher(appID: "YourApplicationID",
                             apiKey: "YourSearchOnlyApiKey",
                             indexName: "index_name")
facetName
type: String
Required

The facet name to search into when doing search for facet values.

1
2
3
4
let searcher = FacetSearcher(appID: "YourApplicationID",
                             apiKey: "YourSearchOnlyApiKey",
                             indexName: "index_name",
                             facetName: "facet_name")
query
type: Query
default: Query()
Optional

The Query used when doing a search.

1
2
3
4
5
6
let query = Query()
query.analytics = true
let searcher = FacetSearcher(appID: "YourApplicationID",
                             apiKey: "YourSearchOnlyApiKey",
                             indexName: "index_name",
                             query: query)

AnswersSearcher

Parameter Description
indexName
type: String
Required

The index to search into for Answers.

1
2
3
let searcher = AnswersSearcher(appID: "YourApplicationID", 
                              apiKey: "YourSearchOnlyApiKey", 
                              indexName: "index_name")
query
type: AnswersQuery
Optional

The query used when performing an Answers search.

1
2
3
4
5
6
7
8
9
let query = AnswersQuery(query: "when do babies start learning?")
  .set(\.queryLanguages, to: [.english])
  .set(\.attributesForPrediction, to: ["description", "title", "transcript"])
  .set(\.nbHits, to: 20)

let searcher = AnswersSearcher(appID: "YourApplicationID", 
                               apiKey: "YourSearchOnlyApiKey", 
                               indexName: "index_name",
                               query: query)

Methods

Parameter Description

Triggers the search. Notifies all listeners of the results.

1
searcher.search()
cancel

Cancels all ongoing search requests.

1
searcher.cancel()

Events

Parameter Description
isLoading

Triggered when the Searcher starts or finishes query execution.

1
2
3
searcher.isLoading.subscribe(with: self) { (_, isLoading) in
  
}
onQueryChanged

Triggered when the query text is modified.

1
2
3
searcher.onQueryChanged.subscribe(with: self) { (_, query) in
  
}
onResults

Triggered when a new search result is received.

1
2
3
searcher.onResults.subscribe(with: self) { (_, results) in

}
onError

Triggered when a new error is received.

1
2
3
4
searcher.onError.subscribe(with: self) { (_, args) in
  let (query, error) = args

}
Did you find this page helpful?