1

When testing the indexing configuration, it would be nice to specifically target a testing index. I obviously don't want to disrupt the live data while I make some configuration adjustments. In Algolia, I can clone an index, and rename it something like mydata_test.

Is there a way to target different indices from different environments?

1 Answer 1

1

You could check your config for an environment variable and change the name of the index that's searched depending on what environment you're in.

On my SearchPlus integration the index name is set as a data attribute on the search component, so it's possible to vary the index based on the environment:

{% set indexName = 'myindex_test' %}

{% if craft.config.craftEnv == 'production' %}
    {% set indexName = 'myindex_live' %}
{% endif %}

<div class="searchplus" data-index-name="{{ indexName }}">
...
</div>

The search JavaScript reads in the data attributes on initialisation and sets up the Algolia index based on the value of data-index-name.

Note: I'm using this with Craft 2. I understand that you can access config values in Craft 3 via craft.app.config.general.<value>.

1
  • This will work perfectly, thanks! I especially like the tip of feeding the index name to JS through a data attribute.
    – Lindsey D
    Commented Mar 28, 2018 at 17:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.