Identifying applied rules on search query

Hello all,

I am using the official Algolia Magento 2 implementation for context.

Is there any way to identify a Rule that is being applied to a search query in instantsearch ?
Let’s say I have an index that contains shampoo products :

  • I create a rule which will pin products shampoo_A and shampoo_B to the 1st and 2nd spot when my search contains the word “hair”.
  • I do not add a “return custom data” statement on my rule configuration.
  • I want to identify if the search “hair” has a rule applied so I can add html content to shampoo_A and shampoo_B based on the “pinned” configurations of the rule.

I know it’s possible to return custom data when creating a rule and using that with the queryRuleCustomData widget (I have a working POC), but let’s say I don’t want to set custom data ?

Thank you !

You can configure getRankingInfo for your search. This will provide additional information about rules, etc. that were used as part of the ranking calculation for that query. Note, however, that this will cause an increase in payload size for each request.

You can see the exact additions to the payload (including the relevant pieces for rules) here:

Most useful to you:

  • Promoted hits are labelled with a "promoted": true field inside the _rankingInfo object. This field is omitted from regular hits.
  • The list of applied Rules is available inside the appliedRules top-level field. This field is omitted when no Rule has been applied (even if enableRules is true).

The params response field also has search parameters after Rule application, and as such reflects the side-effects of the Rules.

1 Like

I should also mention that we’re in the process of moving our community help over to Discord! Please come join us there if you have additional questions!

1 Like

It works perfectly ! I could not have guessed it to be honest. Thank you very much !

For those with the module implementation on Magento 2, I just added getRankingInfo: true to the var searchParameters object in

app/code/MyVendor/MyModule/view/frontend/web/instantsearch.js
1 Like