-
Solr XML config includes
30-10-2010 Add a commentIf you ever used Solr in a version controlled project you will probably have run into the same issues as I have: the solr config files contain definitions that you want to have in version control, but they also contain environment specific settings.
You can use the version controlled configs as a distribution version which you edit to suit the environment, but this breaks easy updating to new version.A good example is the data-config.xml file for the DIH (DataImportHandler). The data mapping described by this config is the same for all environments, but the datasource definition itself (for instance a database connection) differs for dev, test and production environments.
A data-config.xml might look like this:<?xml version="1.0" ?><dataConfig><dataSource type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://host/database" user="user" password="***"/>On a recent project I was getting fed up of manually fixing the data-configs after each update, there had to be a better way…
After some research I found a great solution in XML includes. I never noticed it in the Solr manual before, but it is an excellent solution for this issue. It’s so simple it doesn’t need more explanation than an example:<?xml version="1.0" ?><dataConfig><xi:include href="solr/db-connection.xml" xmlns:xi="http://www.w3.org/2001/XInclude"/>The db-connection.xml file:
<dataSource type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://host/database" user="user" password="***"/>I placed db-connection.xml in version control as a distribution file (db-connection.xml.dist) and several data-import configs use the same db-connection config. The only thing I need to do is copy and edit the distribution file once, and from that point onwards I can easily deploy new versions of the data-configs.
This solution also works for other solr configs. Instead of creating a solrconfig.xml for each environment you can create a generic config and include environment specific settings such as memory (buffer) settings.

