Concepts / Building Search UI / Conditional display
May. 10, 2019

Conditional Display

Handling no results

When no results are found you might want to display some specific contents helping the user go back to a search that was successful. This can be done using the Hits widget’s Empty View mechanism.

Example

No results

Handling errors

When an error occurs you might want to display some specific content helping the user go back to a search that was successful.

To help you with that, we offer a few APIs:

AlgoliaErrorListener

The Searcher class lets you register error listeners that will receive events on any search error. You can use it to implement your own error handling logic:

1
2
3
4
5
6
7
searcher.registerErrorListener(new AlgoliaErrorListener() {
            @Override
            public void onError(@NonNull Query query, @NonNull AlgoliaException error) {
                Log.d("TAG", "Error while searching '" + query.getQuery()
                + "':" + error.getMessage());
            }
});

Events

The Searcher emits events during the search lifecycle. If a search request results in an error, it will send an ErrorEvent that you can subscribe to:

1
2
3
4
5
6
7
8
9
10
11
12
13
public class ErrorHandler {
  public ErrorHandler() {
      EventBus.getDefault().register(this);
      // When your object gets destroyed, remember to call unregister()
      // to avoid memory leaks.
  }

  @Subscribe
  public void onError(ErrorEvent e) {
      Log.e("TAG", "Error while searching '" + event.query.getQuery()
      + "':" + event.error.getMessage());
  }
}

Handling empty query

By default InstantSearch Android will always show you results even when the query is empty. In some case you may want to hide the results on the empty query.

To let you control this behavior, InstantSearch exposes a property searchOnEmptyString. When false, it will display an empty hits widget when there is no query text entered by the user instead of getting the associated results. It defaults to true.

1
instantSearch.setSearchOnEmptyString(false);

Did you find this page helpful?