How to Setup Solr MySQL Data Import Handler (DIH)

Configuration

The Data Import Handler pulls rows straight out of a relational database into your index. You describe the connection and the query in one XML file, and Solr does the rest.

DIH was removed in Solr 9. On Solr 9 and later use the REST API, SolrJ, a CSV import or a tool like Apache NiFi instead. Solr 3 to 8 have all reached end of life at the Apache Software Foundation, and Opensolr is the one host that still fully supports those versions, DIH included, for customers who depend on them.
DIH is also disabled by default across Opensolr for security reasons. Ask us and we will enable it on your index: why it is off, and how to have it turned on.

01 · Edit db-data-config.xml

In your index management area you will find the db-data-config.xml file. It holds the database connection and the query that fetches your rows:

<dataConfig>
  <dataSource type="JdbcDataSource"
              driver="com.mysql.jdbc.Driver"
              url="jdbc:mysql://YOUR_DB_HOST/YOUR_DB_NAME"
              user="your-username"
              password="your-password" />
  <document>
    <entity name="id"
            query="SELECT id, name, description FROM your_table">
    </entity>
  </document>
</dataConfig>

02 · Match your schema to the query

Every column the query returns has to exist as a field in schema.xml:

<field name="id" type="string" indexed="true" stored="true" required="true" />
<field name="name" type="text_general" indexed="true" stored="true" />
<field name="description" type="text_general" indexed="true" stored="true" />

03 · Run the import

From the control panel, or straight against the endpoint:

curl "https://YOUR_SOLR_HOST/solr/YOUR_INDEX/dataimport?command=full-import"

Configuring the MySQL Data Import Handler on Opensolr.

04 · Alternatives on Solr 9 and later

The Solr REST API

POST your documents as JSON or XML to the /update endpoint. The most direct replacement.

CSV import

Export the table and load it through /update/csv. See importing CSV data.

A client library

SolrJ for Java, Solarium for PHP, pysolr for Python: read from the database and index in the same script.

Apache NiFi

A data flow tool that can pull from MySQL and push to Solr on a schedule, without you writing the glue.