Import MongoDB Data into Apache Solr

Ready to get started?

Download a free trial of the MongoDB Driver to get started:

 Download Now

Learn more:

MongoDB Icon MongoDB JDBC Driver

Rapidly create and deploy powerful Java applications that integrate with MongoDB document databases.

Use the CData JDBC Driver for MongoDB in Data Import Handler and create an automated import of MongoDB data to Apache Solr Enterprise Search platform.

The Apache Solr platform is a popular, blazing-fast, open source enterprise search solution built on Apache Lucene.

Apache Solr is equipped with the Data Import Handler (DIH), which can import data from databases and, XML, CSV, and JSON files. When paired with the CData JDBC Driver for MongoDB, you can easily import MongoDB data to Apache Solr. In this article, we show step-by-step how to use CData JDBC Driver in Apache Solr Data Import Handler and import MongoDB data for use in enterprise search.

Create an Apache Solr Core and a Schema for Importing MongoDB

  1. Run Apache Solr and create a Core. > solr create -c CDataCore For this article, Solr is running as a standalone instance in the local environment and you can access the core at this URL: http://localhost:8983/solr/#/CDataCore/core-overview
  2. Create a schema consisting of "field" objects to represent the columns of the MongoDB data to be imported and a unique key for the entity. LastModifiedDate, if it exists in MongoDB, is used for incremental updates. If it does not exist, you cannot do the deltaquery in the later section. Save the schema in the managed-schema file created by Apache Solr.
  3. MongoDBUniqueKey
  4. Install the CData MongoDB JDBC Driver. Copy the JAR and license file (cdata.mongodb.jar and cdata.jdbc.mongodb.lic) to the Solr directory.
    • CData JDBC JAR file: C:\Program Files\CData\CData JDBC Driver for MongoDB ####\lib
    • Apache Solr: solr-8.5.2\server\lib

Now we are ready to use MongoDB data in Solr.

Define an Import of MongoDB to Apache Solr

In this section, we walk through configuring the Data Import Handler.

  1. Modify the Config file of the created Core. Add the JAR file reference and add the DIH RequestHander definition. <lib dir="${solr.install.dir:../../../..}/dist/" regex="solr-dataimporthandler-.*\.jar" /> <requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler"> <lst name="defaults"> <str name="config">solr-data-config.xml</str> </lst> </requestHandler>
  2. Next, create a solr-data-config.xml at the same level. In this article, we retrieve a table from MongoDB, but you can use a custom SQL query to request data as well. The Driver Class and a sample JDBC Connection string are in the sample code below. <dataConfig> <dataSource driver="cdata.jdbc.mongodb.MongoDBDriver" url="jdbc:mongodb:Server=MyServer;Port=27017;Database=test;User=test;Password=Password;"> </dataSource> <document> <entity name="restaurants" query="SELECT Id,MongoDBColumn1,MongoDBColumn2,MongoDBColumn3,MongoDBColumn4,MongoDBColumn5,MongoDBColumn6,MongoDBColumn7,LastModifiedDate FROM restaurants" deltaQuery="SELECT Id FROM restaurants where LastModifiedDate >= '${dataimporter.last_index_time}'" deltaImportQuery="SELECT Id,MongoDBColumn1,MongoDBColumn2,MongoDBColumn3,MongoDBColumn4,MongoDBColumn5,MongoDBColumn6,MongoDBColumn7,LastModifiedDate FROM restaurants where Id=${}"> <field column="Id" name="Id" ></field> <field column="MongoDBColumn1" name="MongoDBColumn1" ></field> <field column="MongoDBColumn2" name="MongoDBColumn2" ></field> <field column="MongoDBColumn3" name="MongoDBColumn3" ></field> <field column="MongoDBColumn4" name="MongoDBColumn4" ></field> <field column="MongoDBColumn5" name="MongoDBColumn5" ></field> <field column="MongoDBColumn6" name="MongoDBColumn6" ></field> <field column="MongoDBColumn7" name="MongoDBColumn7" ></field> <field column="LastModifiedDate" name="LastModifiedDate" ></field> </entity> </document> </dataConfig>
  3. In the query section, set the SQL query that select the data from MongoDB. deltaQuery and deltaImportquery define the ID and the conditions when using incremental updates from the second import of the same entity.
  4. After all settings are done, restart Solr. > solr stop -all > solr start

Run a DataImport of MongoDB Data.

  1. Execute DataImport from the URL below:
  2. Select the "full-import" Command, choose the table from Entity, and click "Execute."
  3. Check the result of the import from the Query.
  4. Try an incremental update using deltaQuery. Modify some data in the original MongoDB data set. Select the "delta-import" command this time from DataImport window and click "Execute."
  5. Check the result of the incremental update.

Using the CData JDBC Driver for MongoDB you are able to create an automated import of MongoDB data into Apache Solr. Download a free, 30 day trial of any of the 200+ CData JDBC Drivers and get started today.