API Reference / Android Widgets / Searcher
Apr. 24, 2019

About this widget

The component handling search requests. Objects implementing the Searcher interface manage the search sessions.

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

  • SearcherSingleIndex: Searches a single index.
  • SearcherMultipleIndex: Searches in multiple indices. This is useful for a federated search, or query suggestions search experience.
  • SearcherForFacets: Searches for facet values.

Examples

Instantiating the SearcherSingleIndex searcher:

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

Instantiating the SearcherMultipleIndex searcher:

1
2
3
4
5
6
7
8
val client = ClientSearch(ApplicationID("YourApplicationID"),
                          APIKey("YourSearchOnlyApiKey"))
val searcher = SearcherMultipleIndex(
    client = client, queries = listOf(
        IndexQuery(IndexName("index_name1")),
        IndexQuery(IndexName("index_name2"))
    )
)

Instantiating the SearcherForFacets searcher:

1
2
3
4
5
6
val client = ClientSearch(ApplicationID("YourApplicationID"),
                          APIKey("YourSearchOnlyApiKey"))
val searcher = SearcherForFacets(
    index = client.initIndex("index_name"),
    attribute = Attribute("color")
)

SearcherSingleIndex

index
type: Index
Required

The index to search into.

1
2
3
4
5
6
7
val client = ClientSearch(
    ApplicationID("YourApplicationID"),
    APIKey("YourSearchOnlyApiKey")
)
val searcher = SearcherSingleIndex(
  index: client.initIndex("index_name")
)
query
type: Query
default: Query()
Optional

A query used to perform the search.

1
2
3
4
5
6
7
8
val client = ClientSearch(
    ApplicationID("YourApplicationID"),
    APIKey("YourSearchOnlyApiKey")
)
val searcher = SearcherSingleIndex(
  client.initIndex("index_name"),
  Query(analytics = true)
)
requestOptions
type: RequestOptions
default: RequestOptions()
Optional

RequestOptions that apply to the search.

1
2
3
4
5
6
7
8
9
10
val client = ClientSearch(
    ApplicationID("YourApplicationID"),
    APIKey("YourSearchOnlyApiKey")
)
val requestOptions = RequestOptions().also {
    it.headers[KeyForwardedFor] = "1.2.3.4"
}
val searcher = SearcherSingleIndex(
  client.initIndex("index_name"),
  requestOptions = requestOptions)

SearcherMultipleIndex

queries
type: List<IndexQuery>
Required

A list of queries used to perform the search.

1
2
3
4
5
6
7
8
9
10
val queries = listOf(
  IndexQuery("index_name1"),
  IndexQuery("Index_name2")
)
queries.forEach { it.query.analytics = true }
val client = ClientSearch(
    ApplicationID("YourApplicationID"),
    APIKey("YourSearchOnlyApiKey")
)
val searcher = SearcherMultipleIndex(client, queries = queries)
requestOptions
type: RequestOptions
default: RequestOptions()
Optional

RequestOptions that apply to the search.

1
2
3
4
5
6
7
8
9
10
val client = ClientSearch(
    ApplicationID("YourApplicationID"),
    APIKey("YourSearchOnlyApiKey")
)
val requestOptions = RequestOptions().also {
    it.headers[KeyForwardedFor] = "1.2.3.4"
}
val searcher = SearcherSingleIndex(
  client.initIndex("index_name"),
  requestOptions = requestOptions)

SearcherForFacet

attribute
type: String
Required

An attribute to search facet values for.

1
2
3
4
5
6
7
8
9
10
val client = ClientSearch(
    ApplicationID("YourApplicationID"),
    APIKey("YourSearchOnlyApiKey")
)
val searcher = SearcherForFacet(
    appID: "YourApplicationID",
    apiKey: "YourSearchOnlyApiKey",
    index: "index_name",
    attribute: "facet_name"
)
index
type: Index
Required

The index to search into.

1
2
3
4
5
6
7
val client = ClientSearch(
    ApplicationID("YourApplicationID"),
    APIKey("YourSearchOnlyApiKey")
)
val searcher = SearcherForFacet(
  index: client.initIndex("index_name")
)
query
type: Query
default: Query()
Optional

A query used to perform the search.

1
2
3
4
5
6
7
8
val client = ClientSearch(
    ApplicationID("YourApplicationID"),
    APIKey("YourSearchOnlyApiKey")
)
val searcher = SearcherForFacet(
  client.initIndex("index_name"),
  Query(analytics = true)
)
requestOptions
type: RequestOptions
default: RequestOptions()
Optional

RequestOptions that apply to the search.

1
2
3
4
5
6
7
8
9
10
val client = ClientSearch(
    ApplicationID("YourApplicationID"),
    APIKey("YourSearchOnlyApiKey")
)
val requestOptions = RequestOptions().also {
    it.headers[KeyForwardedFor] = "1.2.3.4"
}
val searcher = SearcherSingleIndex(
  client.initIndex("index_name"),
  requestOptions = requestOptions)

Methods

Triggers the search. Notifies all listeners of the results.

1
searcher.search()
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?