<p>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. </p>

<p>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:</p>

<pre><code>{% set indexName = 'myindex_test' %}

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

&lt;div class="searchplus" data-index-name="{{ indexName }}"&gt;
...
&lt;/div&gt;
</code></pre>

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

<p>Note: I'm using this with Craft 2. I understand that you can access config values in Craft 3 via <code>craft.app.config.general.&lt;value&gt;</code>.</p>

CC BY-SA 3.0
2018-03-28T17:31:21.000Z
This will work perfectly, thanks! I especially like the tip of feeding the index name to JS through a `data` attribute.
Lindsey D
CC BY-SA 3.0
2018-03-28T09:43:00.627Z
2018-03-28T09:43:00.627Z
Martin Spain
<p>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 <code>mydata_test</code>.</p>

<p><strong>Is there a way to target different indices from different environments?</strong></p>

<plugin-searchplus>
Search Plus: Target different indices from different environments
# Search Plus: Target different indices from different environments

(Asked by: username_0 on 2018-03-27)

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?**

**Tags:** plugin-searchplus

---

## 1 answers

### Answer by username_1 on 2018-03-28. Score: 1 | Accepted answer

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 %}


...


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..

> **Comments:**
> **username_0**: This will work perfectly, thanks! I especially like the tip of feeding the index name to JS through a `data` attribute.  

---

