How to exclude a search result based on specific "exclude" keyword list

I’m looking to find a way to exclude a record in a search result from appearing in certain query based on a
a specific tag/keyword.

The reason for this is because in this instance the page is matching on content in the body, but it is not actually related directly to the search query, so I’d rather have it excluded from appearing within the search for that query.

I seem to remember there was a specific parameter that could be used on the Algolia record to do this, but can’t find anything now. I’m sure it was on Stack Overflow post, as I remember using it before but I can’t be sure.

Hi @james.white,

Thanks for reaching out to Algolia and for helping the community by posting your question to discourse!

From your question, It sounds like you might be looking at two things: (1) excluding the body attribute from being searchable; or, in the alternative, (2) excluding a word from the search query.

(1) Excluding the body attribute: By default, Algolia sets all attributes as searchable, if you do not define a specific list of “searchable attributes”.

If you do not think the body should be searchable at any point, then at the outset define your list of searchableAttributes and exclude body from that list. Here are links to the concept and documentation.

If you do think that body should be searchable sometimes, but just not in this instance, you can use restrictSearchableAttributes attributes and exclude body from the list. Here is a link to the concept and to the documentation.

(2) Excluding a keyword: You can use queryRules and create rule that excludes a typed word from the search, even if it is typed. Here is a link to the concept and an explanation on how to implement using the API or dashboard.

I hope this answered your question. If I misunderstood, please do not hesitate to post again with clarification or even an example of what you were looking to achieve.

Happy Coding!

Hi @ajay.david

Thanks for your reply!

In this case, the body attribute is valid for most searches, so removing it would be a little drastic to adjust a specific search query in this case. However query rules and restrictSearchableAttributes seems to be a possible solution, so I’ll give that a go! This looks like a newer feature that hasn’t been possible before, when I last looked at it!



1 Like