Ramdev
Ramdev

Reputation: 385

Is it possible to use environment variables within solrconfig.xml for dataDir variable?

We have a situation where we have to maybe create multiple instances of Solr/Tomcat running on different ports on either a single machine or several different machines. Towards doing this I was wondering if it's possible to specify the dataDir variable (within solrconfig.xml) using an environmentvariable for example like so: <dataDir>${envvar}/path/to/index</dataDir>.

Upvotes: 13

Views: 7133

Answers (4)

kenorb
kenorb

Reputation: 166359

As explained on wiki.apache.org, you can use system property substitution in solrconfig.xml like below:

<dataDir>${data.dir}</dataDir>

Then you can specify values in a properties file:

#solrcore.properties
data.dir=/data/solrindex

Another way is to dictate the data directory during Solr runtime in this manner:

java -Dsolr.data.dir=/data/dir -jar start.jar

and in XML file use the following syntax:

<dataDir>${solr.data.dir:./solr/data}</dataDir>

I think the better method is to define solr.xml within your solr.home, e.g.:

<solr persistent="true" sharedLib="lib">
 <cores adminPath="/admin/cores">
  <core name="core0" instanceDir="core0" dataDir="/var/lib/solr/core0" />
  <core name="core1" instanceDir="core1" dataDir="/var/lib/solr/core1" />
 </cores>
</solr>

Note: I don't think you can use any external variables here.


Finally using JVM system property file (e.g. solr.xml) in conf/Catalina/localhost, for example:

<Context docBase="webapps/solr.war" crossContext="true">
  <Environment name="solr/home" type="java.lang.String" value="/opt/solr/ads_solr" override="true" />
  <Environment name="solr/data/dir" type="java.lang.String" value="/var/lib/solr" override="true" />
</Context>

where solr/home would work, however solr/data/dir won't work without patching your Solr.

See: tomcat_solr.xml.erb

Upvotes: 0

mlissner
mlissner

Reputation: 18166

Yes, you can do this, but you need to do jump through a couple hoops to set this up using system properties passed to the JVM when you start it.

Anywhere you want your environment variable to work in your configuration files, put the variable like this:

${VAR}

Then, when you start your JVM, pass it the variable by doing:

java -DVAR=$your-system-variable

So, to make this concrete, here's what we do:

java -DINSTALL_ROOT=$INSTALL_ROOT -jar -server start.jar

And our config has something like:

<filter class="solr.SynonymFilterFactory" synonyms=${INSTALL_ROOT}/Solr/conf/synonyms.txt />

Works like a charm.

Upvotes: 6

Patrick
Patrick

Reputation: 96

As i'm working on a similar setup, i needed this too. I don't think it's good practise to use ENV variables for this. You are probably better off using the multicore setup or use a property file in solr.xml.

eg.

<core name="core_1" instanceDir="core_1" properties="core1.properties" />

and then in your core1.properties:

config.datadir=/datadir1

and then use that in your solrconfig.xml:

<dataDir>${config.datadir}</dataDir>

Cheers,

Patrick

Upvotes: 8

Jesvin Jose
Jesvin Jose

Reputation: 23078

Go multi-core .

You can tell Solr to deploy a particular index directory as a core. For example, to deploy a Solr index on path_to_instance_directory on http://localhost:8983/solr/coreX, you would do:

http://localhost:8983/solr/admin/cores?action=CREATE&name=coreX&instanceDir=path_to_instance_directory&config=config_file_name.xml&schema=schem_file_name.xml&dataDir=data

You can tell Solr to create, load, swap two running cores, swap a running core with an inactive core etc.

Upvotes: 2

Related Questions