Conditional requests with InstantSearch Android
On this page
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 Searcher triggerSearchFor
property
All the Searcher
implementations such as HitsSearcher
and FacetSearcher
provide the triggerSearchFor
closure which defines the boolean condition for triggering a search operation.
By default has a null
value so the search will be triggered on each search()
function call.
HitsSearcher
Copy1 2 3 4 5
val searcher = HitsSearcher( client = client, indexName = indexName, triggerSearchFor = { query -> query.query?.startsWith("a") == true } )
Copy1 2 3 4 5
val searcher = HitsSearcher( client = client, indexName = indexName, triggerSearchFor = SearchForQuery.lengthAtLeast(1) // not empty query )
FacetsSearcher
Copy1 2 3 4 5 6
val searcherForFacet = FacetsSearcher( client = client, indexName = indexName, attribute = categories, triggerSearchFor = { query, attribute, facetQuery -> facetQuery?.startsWith("a") == true } )
Copy1 2 3 4 5 6
val searcherForFacet = FacetsSearcher( client = client, indexName = indexName, attribute = attribute, triggerSearchFor = SearchForFacetQuery.lengthAtLeast(1) // not empty query )
MultiSearcher
Copy1 2 3 4
val searcher = multiSearcher.addHitsSearcher( indexName = indexName, triggerSearchFor = SearchForQuery.lengthAtLeast(1) )
Copy1 2 3 4 5
val searcher = multiSearcher.addHitsSearcher( indexName = indexName, attribute = attribute, triggerSearchFor = SearchForFacetQuery.lengthAtLeast(1) )