API Reference / iOS InstantSearch Widgets / Searcher
Apr. 24, 2019

About this widget

The component handling search requests. Objects conforming to the Searcher protocol manage the search sessions.

Out of the box we provide 3 searchers to build your InstantSearch experience:

  • SingleIndexSearcher: Searches a single index.
  • MultiIndexSearcher: Searches in multiple indices. This is useful for a federated search, or query suggestions search experience.
  • FacetSearcher: Searches for facet values.

Examples

To create a SingleIndexSearcher:

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

For the MultiIndexSearcher:

1
2
3
let searcher = MultiIndexSearcher(appID: "YourApplicationID",
                                  apiKey: "YourSearchOnlyApiKey",
                                  indexNames: ["index_name1", "index_name2"])

For the FacetSearcher:

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

SingleIndexSearcher

indexName
type: String
Required

The index to search into.

1
2
3
let searcher = SingleIndexSearcher(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 = SingleIndexSearcher(appID: "YourApplicationID",
                                   apiKey: "YourSearchOnlyApiKey",
                                   indexName: "index_name",
                                   query: query)

MultiIndexSearcher

indexNames
type: list
Required

The indices to search into.

1
2
3
let searcher = MultiIndexSearcher(appID: "YourApplicationID",
                                  apiKey: "YourSearchOnlyApiKey",
                                  indexNames: ["index_name1", "index_name2"])
indexQueryStates
type: [IndexQuery]
Optional

The indices to search into, with their respective queries.

1
2
3
4
5
6
7
8
let searcher = MultiIndexSearcher(
    appID: "YourApplicationID",
    apiKey: "YourSearchOnlyApiKey",
    indexQueries: [
      IndexQueryState(index: index1, query: Query("foo")),
      IndexQueryState(index: index2, query: Query("bar"))
    ]
)
requestOptions
type: RequestOptions
default: RequestOptions()
Optional

RequestOptions that apply to the search.

1
2
3
4
5
6
7
let query = Query()
let requestOptions = RequestOptions()
let searcher = SingleIndexSearcher(appID: "YourApplicationID",
                                   apiKey: "YourSearchOnlyApiKey",
                                   indexName: "index_name",
                                   query: query,
                                   requestOptions: requestOptions)

FacetSearcher

indexName
type: String
Required

The index to search into.

1
2
3
let searcher = SingleIndexSearcher(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)

Methods

Triggers the search. Notifies all listeners of the results.

1
searcher.search()
cancel

Cancels all ongoing search requests.

1
searcher.cancel()

Events

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?