Using algoliasearch for custom source

Hi, I’m using algolia/autocomplete.js for my suggestion (autocomplete) input. The source is function
function search(query, callback) {} with simple ajax request:
'method': 'post',
'url': '/',
'data': { q: query }
Results are returned via callback(results). The problem is, that the autocomplete.sources.hits() has its own cache mechanism. Is here some way to use algoliasearch/client/index with faked search url to keep its cache ability? We’re using elastic on backend so we can modify the response.

Hey! Could you give the complete code? I’m a bit confused how you’re combining both of these.

Also, it would be nice to know why you want to do this.



Here is my code Algolia autocomplete using custom source · GitHub and here is an example, which I’ve used for implementation of custom source:

In gist I used an array to simple cache results. I would like to use native index, which have its own cache implementation.

So when you’re using Autocomplete you have two options.

  1. use the hits source
  2. use your own source

When you’re using the hits source, you need to be using Algolia for the search, but it then indeed will have a cache etc.

If you’re using your own source, you can use LRU cache yourself (there are a few on npm that work well).

However, if you are using Algolia for the source, but just want to concatenate things, you can use the algoliasearch library to call with the query on the two indices you want, and call the callback using that data concatenated however you want. Something like this will work:

var client = algoliasearch("appId", "apiKey");

function search(query, callback) {
      { indexName: "clothes", query: query },
      { indexName: "parties", query: query }
    function(results) {
      var hits = results[0].hits.concat(result[1].hits);

However, you can also use multiple datasets and not need to concatenate at all.

Hope that helped!

1 Like