title: Creating or importing Solr data sources programmatically
body: <p>I have a modular application which by nature means that some or all parts are enabled or disabled at any one time, and new ones can be added or removed at any time.</p>
  
  <p>Looking at the Solr documentation, everything about datasources appears to be in XML files buried away in the Solr directories.</p>
  
  <p>I have yet to find an obvious way of adding to that index programmatically (without say modifying those original files). I need to be able to configure Solr to look for data sources in my enabled modules.</p>
  
  <p>Presumably having it traverse my directory structure looking for them is not ideal so I'm guessing a sensible option would be somehow to point solr at say a .php file (or any other script) which would return a single formatted XML file containing the data sources for each module. I guess to do this I would do something similar to the below?</p>
  
  <p>solr-config.xml</p>
  
  <pre><code>&lt;requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler"&gt; 
    &lt;lst name="defaults"&gt;  
      &lt;str name="config"&gt;/var/www/site.com/data-config.php&lt;/str&gt;
    &lt;/lst&gt;
  &lt;/requestHandler&gt;
  </code></pre>
  
  <p>data-config.xml - and create 1-n documents programmatically, pulled from each module</p>
  
  <pre><code>&lt;dataSource type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/collection" user="root" password="***" batchSize="1" /&gt;
    &lt;document name="module_name"&gt;
      &lt;entity name="module_entity" query="SELECT * FROM module_table"&gt;
         &lt;field column="id" name="id" /&gt;
         &lt;field column="name" name="name" /&gt;
         &lt;field column="age" name="age" /&gt;
         &lt;field column="description" name="description" /&gt;
      &lt;/entity&gt;
    &lt;/document&gt;
  &lt;/dataConfig&gt;
  </code></pre>
  
  <p>I'm assuming this way will work, I'll be trying it tomorrow when I'm back at a suitable computer, but in the mean time I thought I'd ask if there was a better way which I'd overlooked?</p>
  
  <p>Edit: Someone has pointed out to me that pointing it at a PHP script will just read the file, not execute it and therefore not get valid XML back. Therefore a more suitable way would be to have a cronjob execute a script which builds an XML file?</p>