API Reference / Android Widgets / Stats
Aug. 23, 2019

About this widget

Each search Response contains various metadata that you might display in your search experience.

The following information is available as a part of the Response:

  • hitsPerPage: Number of hits per page.
  • totalHitsCount: Total number of hits.
  • pagesCount: Total number of pages.
  • page: Current page.
  • processingTimeMS: Processing time of the request (in ms).
  • query: Query text that produced these results.

To add Stats to your search experience, use these components:

  • Searcher: The Searcher that handles your searches.
  • StatsViewModel: The logic applied to the stats.
  • StatsView: The view that will render the stats.

Examples

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class MyActivity : AppCompatActivity() {

      val client = ClientSearch(
          ApplicationID("YourApplicationID"),
          APIKey("YourAPIKey")
      )
      val index = client.initIndex(IndexName("YourIndexName"))
      val searcher = SearcherSingleIndex(index)
      val statsViewModel = StatsViewModel()
      val connection = ConnectionHandler()

      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)

          val textView = TextView(this)
          val statsView = StatsTextView(textView)

          connection += statsViewModel.connectSearcher(searcher)
          connection += statsViewModel.connectView(statsView)

          searcher.searchAsync()
      }

      override fun onDestroy() {
          super.onDestroy()
          connection.disconnect()
          searcher.cancel()
      }
}

Presenter

StatsPresenter
type: StatsPresenter<T>
default: StatsPresenterImpl()
Optional

The presenter that defines the way we want to display stats, taking as input a Response and returning a T.

1
2
3
4
5
6
7
8
9
10
11
12
13
val presenter: StatsPresenter<String> = { response ->
    buildString {
        if (response != null) {
            append("${response.nbHits} hits")
            searcher.query.query.let {
                if (!it.isNullOrBlank()) {
                    append(" for \"$it\"")
                }
            }
        }
    }
}
viewModel.connectView(view, presenter)

Did you find this page helpful?