Multiple words rules and query matching

Hi, I’m trying to use rules in order to pin different items based on a query.

What I have now is:

  • Rule1 working if I search hoven, new hoven, …
    • Condition: query contains hoven
    • Consequence: pin “item1”
  • Rule2 working if I search best hoven, the best hoven, …
    • Condition: query contains best hoven
    • Consequence: pin “item2”

What I need is to trigger rule2 and allow the user to insert some words between my keywords.
For example, I want to trigger rule2 if the user search for “best builtin hoven”.
With current rule configurations, rule1 is triggered.

Is there a way to achieve this?

Thanks
Regards, Vincenzo

Hi Vincenzo!

With Algolia Rules, you can’t use wildcards in your queries. We can either:

  • target exact queries,
  • target queries that contain a specific term or sentence,
  • target queries that start with a specific term or sentence,
  • target queries that end with a specific term or sentence.

So, while this works well for your first Rule, it doesn’t for your second Rule.

However, you can still achieve what you want with a bit of front-end code. Algolia Rules have a context feature: you can assign a context to each Rule, and activate them from the front-end with the ruleContexts search parameter.

What I would recommend doing here is using a context-based condition instead of a query condition for your second Rule, and handle the logic to detect matching queries in your front-end.

For example, you could have a bit of JavaScript code like this:

function getContexts(query = '') {
  const tokenizedQuery = query.split(' ');

  const firstTerm = tokenizedQuery[0];
  const lastTerm = tokenizedQuery[tokenizedQuery.length - 1];

  if (firstTerm === 'best' && lastTerm === 'hoven') {
    return ['myContext'];
  }

  return [];
}

Then, you could use getContexts to set the ruleContexts search parameter.

index.search('query', {
  ruleContexts: getContexts('query'),
});

Best,

Hi Sarah, thanks for your answer.
I can check how to implement contexts because there are a too many combinations available and our customer want to easily add/remove rules.
However i got the point.

Thanks
Regards, Vincenzo