UI libraries / InstantSearch Android / Widgets

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.
  • SearcherAnswers: Searches an index for Answers.

Examples

Instantiating a HitsSearcher:

1
2
3
4
5
val searcher = HitsSearcher(
    applicationID = ApplicationID("YourApplicationID"),
    apiKey = APIKey("YourSearchOnlyApiKey"),
    indexName = IndexName("index_name")
)

Instantiating a FacetSearcher:

1
2
3
4
5
6
val searcher = FacetsSearcher(
    applicationID = ApplicationID("YourApplicationID"),
    apiKey = APIKey("YourSearchOnlyApiKey"),
    indexName = IndexName("index_name"),
    attribute = Attribute("color")
)

Instantiating a MultiSearcher:

1
2
3
4
5
6
7
8
9
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")
)

Instantiate a SearcherAnswers

1
2
3
4
5
val client = ClientSearch(ApplicationID("YourApplicationID"),
                          APIKey("YourSearchOnlyApiKey"))
val searcher = SearcherAnswers(
    index = client.initIndex(IndexName("index_name"))
)

HitsSearcher

indexName
type: IndexName
Required

The index to search into.

1
2
3
4
5
6
val indexName = IndexName("index_name")
val searcher = HitsSearcher(
    applicationID = ApplicationID("YourApplicationID"),
    apiKey = APIKey("YourSearchOnlyApiKey"),
    indexName = indexName
)
query
type: Query
default: Query()
Optional

A query used to perform the search.

1
2
3
4
5
6
7
val query = Query(analytics = false)
val searcher = HitsSearcher(
    applicationID = ApplicationID("YourApplicationID"),
    apiKey = APIKey("YourSearchOnlyApiKey"),
    indexName = IndexName("index_name"),
    query = query
)
requestOptions
type: RequestOptions
default: RequestOptions()
Optional

RequestOptions that apply to the search.

1
2
3
4
5
6
7
8
9
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
type: IndexName
Required

The index to search into.

1
2
3
4
5
6
7
val indexName = IndexName("index_name")
val searcher = FacetsSearcher(
    applicationID = ApplicationID("YourApplicationID"),
    apiKey = APIKey("YourSearchOnlyApiKey"),
    indexName = indexName,
    attribute = Attribute("color")
)
attribute
type: Attribute
Required

An attribute to search facet values for.

1
2
3
4
5
6
7
val attribute = Attribute("color")
val searcher = FacetsSearcher(
    applicationID = ApplicationID("YourApplicationID"),
    apiKey = APIKey("YourSearchOnlyApiKey"),
    indexName = IndexName("index_name"),
    attribute = attribute
)
query
type: Query
default: Query()
Optional

A query used to perform the search.

1
2
3
4
5
6
7
8
val query = Query(analytics = true)
val searcher = FacetsSearcher(
    applicationID = ApplicationID("YourApplicationID"),
    apiKey = APIKey("YourSearchOnlyApiKey"),
    indexName = IndexName("index_name"),
    attribute = Attribute("color"),
    query = query
)
requestOptions
type: RequestOptions
default: RequestOptions()
Optional

RequestOptions that apply to the search.

1
2
3
4
5
6
7
8
9
10
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
type: MultipleQueriesStrategy
default: None
Optional

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.
1
2
3
4
5
val multiSearcher = MultiSearcher(
    applicationID = ApplicationID("YourApplicationID"),
    apiKey = APIKey("YourSearchOnlyApiKey"),
    strategy = MultipleQueriesStrategy.None
)
requestOptions
type: RequestOptions
default: RequestOptions()
Optional

RequestOptions that apply to the search.

1
2
3
4
5
6
7
8
val requestOptions = RequestOptions().also {
    it.headers[KeyForwardedFor] = "1.2.3.4"
}
val multiSearcher = MultiSearcher(
    applicationID = ApplicationID("YourApplicationID"),
    apiKey = APIKey("YourSearchOnlyApiKey"),
    requestOptions = requestOptions
)

SearcherAnswers

index
type: Index
Required

The index to search into for Answers.

1
2
3
4
5
val client = ClientSearch(ApplicationID("YourApplicationID"),
                          APIKey("YourSearchOnlyApiKey"))
val searcher = SearcherAnswers(
    index = client.initIndex(IndexName("index_name"))
)
query
type: AnswersQuery
Optional

The query used when performing an Answers search.

1
2
3
4
5
6
7
8
9
10
11
12
val client = ClientSearch(ApplicationID("YourApplicationID"),
    APIKey("YourSearchOnlyApiKey"))
val query = AnswersQuery(
    query = "when do babies start learning?",
    queryLanguages = listOf(Language.English),
    nbHits = 20,
    attributesForPrediction = listOf(Attribute("description"), Attribute("title"), Attribute("transcript"))
)
val searcher = SearcherAnswers(
    index = client.initIndex(IndexName("index_name")),
    query = query
)

Methods

Triggers the search and returns a search response.

1
searcher.search()
searchAsync

Triggers the search. Notifies all listeners of the results.

1
searcher.searchAsync()
cancel

Cancels the ongoing search requests.

1
searcher.cancel()
setQuery

Sets the query to the string provided.

1
searcher.setQuery("foo")

Events

onLoadingChanged

Triggered when the status of the search request is changed.

1
2
3
searcher.onLoadingChanged += { loading ->
    print(if (loading) "Currently loading search response" else "Done loading")
}
onResponseChanged

Triggered when a new response has arrived.

1
2
3
4
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.

1
2
3
searcher.onErrorChanged += {
   errorTextView.text = it.localizedMessage
}
Did you find this page helpful?