algoliasearch-client-javascript icon indicating copy to clipboard operation
algoliasearch-client-javascript copied to clipboard

Is there a way to abort a search request?

Open romaincointepas opened this issue 11 years ago • 23 comments

Useful when connectivity is bad and search results don't come back nearly instantly.

User search for something, we call index.search(), then the user do something that would make the search results obsolete (navigates away?). In this case it would be good to have a way to abort the current request (and index.search callback would either never be called or be called with an error).

romaincointepas avatar Nov 03 '15 11:11 romaincointepas

@vvo +1 for .cancel method.

use case: in an autocomplete field, when using algolia via the JS api I'd like to cancel old calls to reduce load on the client side when I know the app won't use any of the 'old' data.

make sense?

Im also having the same problem with this use case in fetch call can anyone help me to abort the API call of previous text and to request only the input given by the user now

For example : search text is agile when user types a and g cancel the request for 'a' and fire the request for ag

KeerthanaManoharan avatar Jul 17 '20 06:07 KeerthanaManoharan

@KeerthanaManoharan I guess you could use the v4 of the client, create your own requester as shown here:

  • https://www.algolia.com/doc/api-client/advanced/configure-the-client/javascript/?language=javascript#requester
  • example: https://github.com/algolia/algoliasearch-client-javascript/blob/c14f631abd111b822ef92d8d25b321c325959e44/packages/requester-browser-xhr/src/createBrowserXhrRequester.ts

And use fetch (https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch) instead of XHR, plus an abort controller: https://developer.mozilla.org/en-US/docs/Web/API/AbortController#Examples

Then when using the client, when you receive user input, if there's a request going on, you could abort it. That's some good work to do but doable. ~Maybe it will be inside Algolia at some point~ (I have no idea what I am talking about :D).

But again, you could also just do what the JavaScript helper does: instead of aborting you just discard old requests. To do so, when receiving responses from Algolia, you compare the response query to the user query, if they do not match then your discard the response because it's outdated.

vvo avatar Jul 17 '20 08:07 vvo

Our official stance is still the following:

  1. canceling a search request does not free up significant resources
  2. the request has still happened, meaning it's still counted in your operations
  3. we see people using backspace fairly frequently, which would need to be requested again, even if it has already happened

Therefore we see the most efficient way to deal with stale requests to be to cached, but only display the result of the current search query.

If you really want to cancel a request, you could write your custom requester (with fetch), and accept an AbortController yourself, however, you'll need to also change the implementation of transporter (see createMappedRequestOptions & retryableRequest) to accept that argument and forward it to fetch.

Haroenv avatar Jul 17 '20 08:07 Haroenv