Return error message when character is found - autocomplete.js

How can immediately return a message when a user searches for a particular character? In our environment anytime a ‘X’ is the third character in the search query I want to return a string value of

‘You appear to be searching for a custom part. Please inquiry with Sales.’

Is that possible to do? Can this be done with querying multiple indices?

Hey,

This can be done the following way:

  1. use a different source than the algoliasearch one
  2. in that source, implement your custom logic
  3. use that source for all your indices

The source would look like this:

function algoliaOrSalesSource(index, searchParams) {
  params = searchParams || {};
  params.additionalUA = 'autocomplete.js ' + version;

  function sourceFn(query, cb) {
    if (query[3] === 'X') {
      const hits = [
        'You appear to be searching for a custom part. Please inquiry with Sales.',
      ];
      cb(hits, { hits });
    }
    index.search(query, params, function(error, content) {
      if (error) {
        _.error(error.message);
        return;
      }
      cb(content.hits, content);
    });
  }

  return sourceFn;
}

You can then use this source just like you would use the Algolia index source.