Skip to main content

About this widget

The component handling search requests. Objects implementing the Searcher interface 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: Searches in multiple indices. This is useful for a federated search, or query suggestions search experience.

Examples

Instantiating a HitsSearcher:
Kotlin
val searcher = HitsSearcher(
    applicationID = ApplicationID("YourApplicationID"),
    apiKey = APIKey("YourSearchOnlyAPIKey"),
    indexName = IndexName("index_name"),
    isAutoSendingHitsViewEvents = true
)
Instantiating a FacetSearcher:
Kotlin
val searcher = FacetsSearcher(
    applicationID = ApplicationID("YourApplicationID"),
    apiKey = APIKey("YourSearchOnlyAPIKey"),
    indexName = IndexName("index_name"),
    attribute = Attribute("color")
)
Instantiating a MultiSearcher:
Kotlin
val multiSearcher = MultiSearcher(
    applicationID = ApplicationID("YourApplicationID"),
    apiKey = APIKey("YourSearchOnlyAPIKey"),
)
val hitsSearcher = multiSearcher.addHitsSearcher(indexName = IndexName("index_name1"))
val facetsSearcher = multiSearcher.addFacetsSearcher(
    indexName = IndexName("index_name1"),
    attribute = Attribute("facet_name")
)

HitsSearcher

indexName
IndexName
required
The index to search into.
Kotlin
val indexName = IndexName("index_name")
val searcher = HitsSearcher(
    applicationID = ApplicationID("YourApplicationID"),
    apiKey = APIKey("YourSearchOnlyAPIKey"),
    indexName = indexName
)
query
Query
default:"Query()"
A query used to perform the search.
Kotlin
val query = Query(analytics = false)
val searcher = HitsSearcher(
    applicationID = ApplicationID("YourApplicationID"),
    apiKey = APIKey("YourSearchOnlyAPIKey"),
    indexName = IndexName("index_name"),
    query = query
)
isAutoSendingHitsViewEvents
Boolean
default:false
Flag defining whether the automatic hits view Insights events sending is enabled.
The default value was true until version 3.3.1.
Kotlin
val searcher = HitsSearcher(
    applicationID = ApplicationID("YourApplicationID"),
    apiKey = APIKey("YourSearchOnlyAPIKey"),
    indexName = IndexName("index_name"),
    isAutoSendingHitsViewEvents = true,
)
userToken
UserToken
User token assigned to automatically sent Insights events in the HitsSearcher component. If not explicitly set during initialization, the HitsSearcher generates and assigns a default user token.
requestOptions
RequestOptions
default:"RequestOptions()"
RequestOptions that apply to the search.
Kotlin
val requestOptions = RequestOptions().also {
    it.headers[KeyForwardedFor] = "1.2.3.4"
}
val searcher = HitsSearcher(
    applicationID = ApplicationID("YourApplicationID"),
    apiKey = APIKey("YourSearchOnlyAPIKey"),
    indexName = IndexName("index_name"),
    requestOptions = requestOptions
)

FacetSearcher

indexName
IndexName
required
The index to search into.
Kotlin
val indexName = IndexName("index_name")
val searcher = FacetsSearcher(
    applicationID = ApplicationID("YourApplicationID"),
    apiKey = APIKey("YourSearchOnlyAPIKey"),
    indexName = indexName,
    attribute = Attribute("color")
)
attribute
Attribute
required
An attribute to search facet values for.
Kotlin
val attribute = Attribute("color")
val searcher = FacetsSearcher(
    applicationID = ApplicationID("YourApplicationID"),
    apiKey = APIKey("YourSearchOnlyAPIKey"),
    indexName = IndexName("index_name"),
    attribute = attribute
)
query
Query
default:"Query()"
A query used to perform the search.
Kotlin
val query = Query(analytics = true)
val searcher = FacetsSearcher(
    applicationID = ApplicationID("YourApplicationID"),
    apiKey = APIKey("YourSearchOnlyAPIKey"),
    indexName = IndexName("index_name"),
    attribute = Attribute("color"),
    query = query
)
requestOptions
RequestOptions
default:"RequestOptions()"
RequestOptions that apply to the search.
Kotlin
val requestOptions = RequestOptions().also {
    it.headers[KeyForwardedFor] = "1.2.3.4"
}
val searcher = FacetsSearcher(
    applicationID = ApplicationID("YourApplicationID"),
    apiKey = APIKey("YourSearchOnlyAPIKey"),
    indexName = IndexName("index_name"),
    attribute = Attribute("facet_name"),
    requestOptions = requestOptions
)

MultiSearcher

strategy
MultipleQueriesStrategy
default:"None"
The strategy of the query.Can be one of the following values:
  • None. Execute the sequence of queries until the end. This is recommended when each query is of equal importance, meaning all records of all queries need to be returned.
  • StopIfEnoughMatches. Execute queries one by one, but stop as soon as the cumulated number of hits is at least hitsPerPage. This is recommended when each query is an alternative, and where, if the first returns enough records, there is no need to perform the remaining queries.
Kotlin
val multiSearcher = MultiSearcher(
    applicationID = ApplicationID("YourApplicationID"),
    apiKey = APIKey("YourSearchOnlyAPIKey"),
    strategy = MultipleQueriesStrategy.None
)
requestOptions
RequestOptions
default:"RequestOptions()"
RequestOptions that apply to the search.
Kotlin
val requestOptions = RequestOptions().also {
    it.headers[KeyForwardedFor] = "1.2.3.4"
}
val multiSearcher = MultiSearcher(
    applicationID = ApplicationID("YourApplicationID"),
    apiKey = APIKey("YourSearchOnlyAPIKey"),
    requestOptions = requestOptions
)

Methods

Triggers the search and returns a search response.
Kotlin
searcher.search()
searchAsync
Triggers the search. Notifies all listeners of the results.
Kotlin
searcher.searchAsync()
cancel
Cancels the ongoing search requests.
Kotlin
searcher.cancel()
setQuery
Sets the query to the string provided.
Kotlin
searcher.setQuery("foo")

Events

onLoadingChanged
Triggered when the status of the search request is changed.
Kotlin
searcher.onLoadingChanged += { loading ->
    print(if (loading) "Currently loading search response" else "Done loading")
}
onResponseChanged
Triggered when a new response has arrived.
Kotlin
searcher.onResponseChanged += { response ->
    val hits = response.hits.deserialize(MovieHit.serializer())
    // Do something with hits...
}
onErrorChanged
Triggered when an error was encountered during a search request.
Kotlin
searcher.onErrorChanged += {
   errorTextView.text = it.localizedMessage
}
I