Guides / Building Search UI / Going further

Conditional Requests with InstantSearch Android

By default, InstantSearch sends an initial request to Algolia’s servers with an empty query. This connection helps speed up later requests.

However, sometimes you don’t want to perform more network calls than are necessary. For example, you may want to limit the number of search requests and reduce your overall Algolia usage. This guide helps you build a UI that prevents this initial request.

Set search trigger property

InstantSearch paging data sources provide a parameter which defines the boolean condition for triggering a search operation.

The function blocking searches for empty queries should look as follows:

  • Single index search:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    val triggerSearch = { query: Query -> 
      query.query?.isNotEmpty() ?: false 
    }
    val dataSourceFactory = SearcherSingleIndexDataSource.Factory(
      searcher = searcher,
      triggerSearchForQuery = triggerSearch
    ) { hit ->
      // model creation..
    }
    
  • Multi index search:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    
    val triggerSearch = { queries: List<IndexQuery> ->
      queries.all { it.query.query?.isNotEmpty() ?: false }
    }
    val dataSourceFactory = SearcherMultipleIndexDataSource.Factory(
      searcher = searcher,
      indexQuery = index,
      triggerSearchForQueries = triggerSearch
    ) { hit ->
      // model creation...
    }
    
Did you find this page helpful?