Over a million developers have joined DZone.

Spring Data + Solr Cloud + Zookeeper + MongoDB + Ubuntu Integration

DZone 's Guide to

Spring Data + Solr Cloud + Zookeeper + MongoDB + Ubuntu Integration

This article will show you how to set up a clustered MongoDB to store the data, a Solr Cloud to index the data in MongoDB in real time. Then how to use mongo-connector to replicate data from MongoDB to Solr Cloud. And finally, develop a simple Spring Data Solr application that talks to Solr Cloud to search the data in MongoDB.

· Big Data Zone ·
Free Resource

I had some years of experience with Solr version 4.x. But Solr version 5 and version 6 are much different from the older version. So I decided to refresh my knowledge by integrating Solr Cloud 6.0.1, MongoDB 3.2.7, Spring Data Solr, which is part of Spring Boot 1.4.0.M3.  

My goal is to set up a clustered MongoDB to store the data, a Solr Cloud to index the data in MongoDB in real time. I will use mongo-connector to replicate data from MongoDB to Solr Cloud. Then, I will develop a simple Spring Data Solr application that talks to Solr Cloud to search the data in MongoDB.

Please feel free to give me any feedback or ask me any questions about it.  Hope this article is helpful for your next project.

Install JDK 1.8 If You Don't Have on Your Ubuntu:

$ sudo add-apt-repository ppa:webupd8team/java
$ sudo apt-get update
$ sudo apt-get install oracle-java8-installer

Install MongoDB 3.2.12:

  1. Follow these instructions to install MongoDB on

Ubuntu https://docs.mongodb.com/v3.0/tutorial/install-mongodb-on-ubuntu/

  1. Edit /etc/mongod.conf to setup replica set. I just installed one instance of MongoDB. You can install multiple instances that share the same replica set.
# Where and how to store data.
  dbPath: /var/lib/mongodb
    enabled: true
# where to write logging data.
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

# network interfaces
  port: 27017

# replica sets

  replSetName: rs0
  1. Restart MongoDB
$ sudo service mongod restart
  1. Run mongo to connect mongod server, then run
$ mongo


You will see something like this:

    "set" : "rs0",
    "date" : ISODate("2016-06-14T07:41:49.307Z"),
    "myState" : 1,
    "term" : NumberLong(1),
    "heartbeatIntervalMillis" : NumberLong(2000),
    "members" : [
        "_id" : 0,
        "name" : "ubuntu:27017",
        "health" : 1,
        "state" : 1,
        "stateStr" : "PRIMARY",
        "uptime" : 400763,
        "optime" : {
            "ts" : Timestamp(1465716890, 2227),
            "t" : NumberLong(1)
        "optimeDate" : ISODate("2016-06-12T07:34:50Z"),
        "electionTime" : Timestamp(1465694725, 2),
        "electionDate" : ISODate("2016-06-12T01:25:25Z"),
        "configVersion" : 1,
        "self" : true
    "ok" : 1
  1. Download sample MongoDB JSON data from here:


  1. Import sample data into MongoDB:
$ mongoimport --db test --collection restaurant --drop --file ~/primer-dataset.json
  1. Run mongo, run the following to verify if you have sample data imported correctlly.

use test
  1. The sample data will look like this:
    "_id" : ObjectId("575d1095ae7da76b8fb71b1a"),
    "address" : {
        "building" : "1007",
        "coord" : [
        "street" : "Morris Park Ave",
        "zipcode" : "10462"
    "borough" : "Bronx",
    "cuisine" : "Bakery",
    "grades" : [
        "date" : ISODate("2014-03-03T00:00:00Z"),
        "grade" : "A",
        "score" : 2
        "date" : ISODate("2013-09-11T00:00:00Z"),
        "grade" : "A",
        "score" : 6
        "date" : ISODate("2013-01-24T00:00:00Z"),
        "grade" : "A",
        "score" : 10
        "date" : ISODate("2011-11-23T00:00:00Z"),
        "grade" : "A",
        "score" : 9
        "date" : ISODate("2011-03-10T00:00:00Z"),
        "grade" : "B",
        "score" : 14
    "name" : "Morris Park Bake Shop",
    "restaurant_id" : "30075445"

Install Zookeeper 3.4.6:

  1. Follow instruction of Setting Up a Single ZooKeeper from here (you can install multiple Zookeeper instances if you want):


  1. Start Zookeeper:
$ /opt/zookeeper-3.4.6/bin/zkServer.sh start/stop/restart

Add Solr Configuration Files to Zookeeper

  1. Download my sample code from: https://github.com/lcbdl/resaurant.git
  2. It's a good idea to track the change of you configurations in a version control system so every time you update the files, you commit to both version control system and zookeeper.
  3. You have to have the following in solrconfig.xml:
<schemaFactory class="ClassicIndexSchemaFactory"/>

Solr 6 uses managed_schema by default. But to index MongoDB, we have to have a schema.xml.

So we need this to force Solr to use classic schema.xml:

<requestHandler name="/admin/luke" class="org.apache.solr.handler.admin.LukeRequestHandler" />

mongo-connector uses this to retrieve schema.xml from solr

  1. schema.xml
MongoDB use _id as the primary key name.
   <field name="_id" type="string" indexed="true" stored="true" required="true" multiValued="false" />

   <!-- metadata used by mongo-connector -->
   <field name="_ts" type="long" indexed="true" stored="true" required="true" multiValued="false" />
   <field name="ns" type="string" indexed="true" stored="true" required="true" multiValued="false" />

   <!-- fields for the document -->
   <field name="restaurant_id" type="string" indexed="true" stored="true"/>
   <field name="name" type="text_general" indexed="true" stored="true"/>
   <field name="borough" type="string" indexed="true" stored="true"/>
   <field name="cuisine" type="string" indexed="true" stored="true"/>

   <field name="address.building" type="string" indexed="true" stored="true"/>
   <dynamicField name="address.coord.*" type="float" indexed="false" stored="false"/>
   <field name="address.street" type="string" indexed="true" stored="true"/>
   <field name="address.zipcode" type="string" indexed="true" stored="true"/>

   <dynamicField name="*.date" type="date" indexed="false" stored="false" />
   <dynamicField name="*.grade" type="string" indexed="false" stored="false" />
   <dynamicField name="*.score" type="int" indexed="false" stored="false" />

   <field name="grades.dates" type="date" indexed="true" stored="true" multiValued="true" />
   <field name="grades.grades" type="string" indexed="true" stored="true" multiValued="true" />
   <field name="grades.scores" type="int" indexed="true" stored="true" multiValued="true" />

      Specify what field is the unique key in solr

   <copyField source="name" dest="text"/>
   <copyField source="borough" dest="text"/>
   <copyField source="cuisine" dest="text"/>
   <copyField source="address.building" dest="text"/>
   <copyField source="address.street" dest="text"/>
   <copyField source="address.zipcode" dest="text"/>
   <copyField source="address.coord.*" dest="text"/>

   <copyField source="*.date" dest="grades.dates" />
   <copyField source="*.grade" dest="grades.grades" />
   <copyField source="*.score" dest="grades.scores" />


It's really challenging to map array embedded JSON document in Solr. For the below example, I used:

<dynamicField name="*.date" type="date" indexed="false" stored="false" />


<field name="grades.dates" type="date" indexed="true" stored="true" multiValued="true" />


 <copyField source="*.date" dest="grades.dates" />

Solr only supports dynamic field name with either leading or trailing asterisks. It doesn't support something like grades.*.date.

"grades" : [
    "date" : ISODate("2014-03-03T00:00:00Z"),
    "grade" : "A",
    "score" : 2
    "date" : ISODate("2013-09-11T00:00:00Z"),
    "grade" : "A",
    "score" : 6

Install Solr Cloud

  1. Download Solr-6.0.1.tgz file from:


$ wget http://www.us.apache.org/dist/lucene/solr/6.0.1/solr-6.0.1.tgz
  1. Extract the installation script from the tgz file:
$ tar xzf solr-6.0.1.tgz solr-6.0.1/bin/install_solr_service.sh --strip-components=2
  1. Install 2 Solr instances:
$ sudo ./install_solr_service.sh solr-6.0.1.tgz -i /opt -d /var/solr1 -u <username> -s solr1 -p 8983
$ sudo ./install_solr_service.sh solr-6.0.1.tgz -i /opt -d /var/solr2 -u <username> -s solr2 -p 8984
  1. Update /opt/solr-6.0.1/bin/solr.in.sh
$ vi /opt/solr-6.0.1/bin/solr.in.sh

Add the following line in it:


  1. Start Solr instances:
$ sudo service solr1 start

$ sudo service solr2 start
  1. Check solr1 and solr2 status, and make sure you have output like below:
$ sudo service solr1 status

Found 1 Solr nodes:

Solr process 4922 running on port 8983
    "version":"6.0.1 c7510a0fdd93329ec04c853c8557f4a3f2309eaf - sarowe - 2016-05-23 19:40:37",
    "uptime":"0 days, 21 hours, 37 minutes, 50 seconds",
    "memory":"214.7 MB (%43.8) of 490.7 MB",
  1. Create collection 

Open your browser, and go to this URL:

Install Mongo-connector and Connect Solr and Mongodb

  1. Install pip if you don't have pip in your Ubuntu:
$ apt-get update

$ apt-get install python-pip
  1. Install mongo-connector:
$ pip install mongo-connector
  1. Run mongo-connector (You can create a ubuntu server and start when system start up).

Mongo-connector will populate all existing data from MongoDB to Solr, also update Solr data whenever the data in MongoDB changes.

$ mongo-connector --auto-commit-interval=0 -m localhost:27017 -t http://localhost:8983/solr/restaurant -d solr_doc_manager
  1. Test if you have data in Solr by access this URL, and you will see the result:*:*&wt=json

            "address.street":"West   57 Street",
            "cuisine":"American ",
            "name":"Europa Cafe",

Create a Spring Data Solr Applciation to Run a Query Against Solr Cloud

  1. Download my sample code to you workspace folder:
$ git clone https://github.com/lcbdl/resaurant.git
  1. Import maven project into your Java IDE (I used Spring STS)
  2. Right click on RestaurantRepositoryTest.java, and Run As -> JUnit Test.  

You will see a good test result if everything is fine.

Key Components in the Code

You can find my full sample code here: https://github.com/lcbdl/resaurant

  1. Model class Restaurant.java:
@SolrDocument(solrCoreName="restaurant")  // Solr collection name
public class Restaurant {

    @Field("_id")                         // Specify field name in solr
    @Id                                   // This is required
    private String id;

    private String name;

    private String restaurantId;

    private String borough;

  1. Repository interface RestaurantRepository.java:
public interface RestaurantRepository extends SolrCrudRepository<Restaurant, String> {

    List<Restaurant> findByName(String name);

  1. Spring Boot Main Applciation Class:
public class RestaurantApplication extends SpringBootServletInitializer {

    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(RestaurantApplication.class);

    public static void main(String[] args) {
        SpringApplication.run(RestaurantApplication.class, args);
  1. SpringSolrConfiguration.java:
@EnableSolrRepositories(basePackages = { "ca.knc.restaurant.repository.solr" }, multicoreSupport = true)
public class SpringSolrConfig {

    private String zkHost;

    public SolrClient solrClient() {
        return new CloudSolrClient(zkHost);

    public SolrTemplate solrTemplate(SolrClient solrClient) throws Exception {
        return new SolrTemplate(solrClient);

  1. application.properties:
# SOLR (SolrProperties)
  1. RestaurantRepositoryTest.java
@SpringBootTest(classes = RestaurantApplication.class)
public class RestaurantRepositoryTest {

    private RestaurantRepository restaurantRepository;

    public void findByNameTest() {
        List<Restaurant> restaurants = restaurantRepository.findByName("Morris Park Bake Shop");
        assertTrue(restaurants.size() > 0);
        for (Restaurant r : restaurants) {

spring data ,solr cloud ,zookeeper ,mongodb ,ubuntu

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}