Limit searches to 3 characters or more with InstantSearch?

I’m customizing the vanilla InstantSearch for a client and they asked that (in part to reduce costs) the site only issue a query when a user has entered more than 3 characters. I don’t see any way to do this while using InstantSearch - am I missing something? (hopefully something obvious and easy to implement!)



Hi @info.fastspot,

We do not recommend this approach because it introduces a perceived “lag” in search results that your users might interpret as a slow response. It no longer becomes search-as-you-type, but if you do want to trigger a search only after a certain number of minimum characters you can change this default behavior by intercepting the search using our helper.js library.

For example, you can limit the search to only trigger on a certain number of characters, such as at least 3 characters:

searchFunction: function(helper) {              
     if (helper.state.query.length < 2) {                         
        return; // no search if less than 2 character               
    }           );     

Again, this may cause users to perceive the search to be lagging, but will reduce the number of search operations and reduce the cost.

Best regards,

1 Like

Thanks Cindy! I agree with you, and think that the client is being overly cautious, but I’m hoping that actual usage will mean that I can remove this code relatively soon.

Thanks again - I will try that out ASAP!

Edited to add: Works a treat - I really appreciate your helpf @cindy.cullen!