Search using multiple search terms

Hi, I have implemented Algolia Search to returns records that are basically just questions asked of an expert.

I want it so the user can search for questions that contain multiple key words e.g. “Free Will” and / or “meditation”.

I’ve been reading the documentation and I know that the default is for Algolia to only return a hit if all of the searched for words
are present.

I added both

index.setSettings({removeWordsIfNoResults: 'allOptional'});{
    query: 'query',
    removeWordsIfNoResults: 'allOptional'

to my code and now it does seem to return a hit if any one of the key words are present, including just ‘will’. How can I get it to treat ‘free will’ as a single complete search term (i.e. not return records that contain just ‘will’) and also maybe return hits that contain both ‘free will’ and ‘meditation’ or either one. i.e. can you have it so that Algolia interprets the AND or OR operators?


Hi Tam,

Setting the removeWordsIfNoResults option to ‘allOptional’ will only work if there are no results with all the words present. In that case, it changes the search query to use OR instead of AND so that more results will be produced - meaning if any of the words are found, the results will show those records.

The difference between the index.setSettings and the is that using index.setSettings will make all queries use the optionalWords parameter when no results are found - it will be the default setting. Using will use optionalWords on the frontend with the individual queries to override the default setting.

Of course this will only work if there are no results found for ALL the words, if you want to make all queries use the OR even if there are results found, you could use the optionalWords parameter on each query as seen here:

Another option would be to build an advanced search frontend with filters to achieve what you want. You could have a UI where you click and add terms to the filters.

You can find more information about using AND, OR and other operators in filters here:

Hopefully this gives you some ideas on how to get the results you are looking for.

Best regards,

Thanks Cindy. Where exactly would the code as provided in the documentation that you refer to, be implemented? Would this be in the same node js application that is used to sync the Algolia index with the Firebase database?

const query = 'the query';

const params = {
  'optionalWords': query
};, params, (err, results) => {

Hi Tam, you should put this code (or similar code) wherever you put the code to search Algolia in your backend. If you are using instantSearch or searching from the frontend, you will need to use the js helper as in this codesandbox

You can find the documentation for js helper here:

Hi Cindy

Thanks for the explanation. I have modified your code to talk to my own Algolia index (yeah no great feat :-)).

I’m just struggling to figure out how to incorporate this into my current app.

I am using the InstantSearch component inside my App.js component like so …

 render() {

        return (
                    <h1>AMA with Elastic Search Demo</h1>
                    <InstantSearch indexName="questions" className={classes.Question} searchClient={searchClient}>
                            <Hits className={classes.Question} hitComponent={Hit} />
                    <div id="hits"></div>

I see that you render the page from the index.html file and refer to the instantsearch code with the script element.

Can I add the search const to my App.js file and render it within there?

Hi Tam, Here is a React example codesandbox that might help:

Look in the app.js file. Using createConnector allows us to add the optionalWords Search Parameter in React, so that searching for something like ‘insignia leather’ shows us all items with ‘insignia’ (without ‘leather’), ‘leather’ (without ‘leather’) and ‘insignia leather’ (both keywords).

I hope that helps!