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
:
val searcher = HitsSearcher(
applicationID = ApplicationID("YourApplicationID"),
apiKey = APIKey("YourSearchOnlyAPIKey"),
indexName = IndexName("index_name"),
isAutoSendingHitsViewEvents = true
)
Instantiating a FacetSearcher
:
val searcher = FacetsSearcher(
applicationID = ApplicationID("YourApplicationID"),
apiKey = APIKey("YourSearchOnlyAPIKey"),
indexName = IndexName("index_name"),
attribute = Attribute("color")
)
Instantiating a MultiSearcher
:
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
The index to search into.val indexName = IndexName("index_name")
val searcher = HitsSearcher(
applicationID = ApplicationID("YourApplicationID"),
apiKey = APIKey("YourSearchOnlyAPIKey"),
indexName = indexName
)
A query used to perform the search.val query = Query(analytics = false)
val searcher = HitsSearcher(
applicationID = ApplicationID("YourApplicationID"),
apiKey = APIKey("YourSearchOnlyAPIKey"),
indexName = IndexName("index_name"),
query = query
)
isAutoSendingHitsViewEvents
Flag defining whether the automatic hits view Insights events sending is enabled.The default value was true
until version 3.3.1.
val searcher = HitsSearcher(
applicationID = ApplicationID("YourApplicationID"),
apiKey = APIKey("YourSearchOnlyAPIKey"),
indexName = IndexName("index_name"),
isAutoSendingHitsViewEvents = true,
)
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.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
The index to search into.val indexName = IndexName("index_name")
val searcher = FacetsSearcher(
applicationID = ApplicationID("YourApplicationID"),
apiKey = APIKey("YourSearchOnlyAPIKey"),
indexName = indexName,
attribute = Attribute("color")
)
An attribute to search facet values for.val attribute = Attribute("color")
val searcher = FacetsSearcher(
applicationID = ApplicationID("YourApplicationID"),
apiKey = APIKey("YourSearchOnlyAPIKey"),
indexName = IndexName("index_name"),
attribute = attribute
)
A query used to perform the search.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.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.
val multiSearcher = MultiSearcher(
applicationID = ApplicationID("YourApplicationID"),
apiKey = APIKey("YourSearchOnlyAPIKey"),
strategy = MultipleQueriesStrategy.None
)
requestOptions
RequestOptions
default:"RequestOptions()"
RequestOptions
that apply to the search.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.
Triggers the search.
Notifies all listeners of the results.
Cancels the ongoing search requests.
Sets the query to the string provided.
Events
Triggered when the status of the search request is changed.searcher.onLoadingChanged += { loading ->
print(if (loading) "Currently loading search response" else "Done loading")
}
Triggered when a new response has arrived.searcher.onResponseChanged += { response ->
val hits = response.hits.deserialize(MovieHit.serializer())
// Do something with hits...
}
Triggered when an error was encountered during a search request.searcher.onErrorChanged += {
errorTextView.text = it.localizedMessage
}