Solr is scalable, reliable and resilient service providing distributed indexing, replication and load-balanced queriesalong with a centralized configuration for its management. In this tutorial we will learn the basic of setting up Solr and create indexed documents using a Spring Boot application.
The simplest way to start Solr is using Docker:
$ docker run --name my_solr -d -p 8983:8983 -t solr
That will create a new Docker container using the official Solr image, which includes OpenJDK+Solr.
To use Solr, you need to create a “core”, an index for your data. For example:
$ docker exec -it --user=solr my_solr bin/solr create_core -c customer_core Created new core 'customer_core'
Now let’s code with Spring Boot. We need a class that will be stored
package com.masterspringboot.solrdemo;
import org.springframework.data.annotation.Id;
import org.springframework.data.solr.core.mapping.Indexed;
import org.springframework.data.solr.core.mapping.SolrDocument;
@SolrDocument(solrCoreName = "customer_core") public class CustomerDoc {
@Id @Indexed private String id;
@Indexed(name = "username", type = "string") private String username;
@Indexed(name = "address", type = "string") private String address;
@Indexed(name = "phone_number", type = "string") private String phoneNumber;
// Getters and Setters here
}
Then, our Repository Class which links to our Core:
package com.masterspringboot.solrdemo;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.solr.repository.Query;
import org.springframework.data.solr.repository.SolrCrudRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
@Qualifier("userSolrRepo")
public interface SolrRepository extends SolrCrudRepository<CustomerDoc, String> {
@Query(value = "*:*")
List<CustomerDoc> getCustomers();
}
The Repository is used by the following Service Class:
package com.masterspringboot.solrdemo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class CustomerServiceImpl implements CustomerService {
@Autowired SolrRepository userSolrRepo;
@Override
public void storeUser(CustomerDoc doc) {
userSolrRepo.save(doc);
}
@Override
public List<CustomerDoc> getCustomers() {
return userSolrRepo.getCustomers();
}
@Override
public void deleteCustomer(String id) {
userSolrRepo.deleteById(id);
}
}
And, at the top of the pile, we have the Controller class:
package com.masterspringboot.solrdemo;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
@RestController
public class CustomerController {
@Autowired private CustomerService customerService;
@RequestMapping(value = "getCustomers", method = RequestMethod.GET)
@ResponseBody
public List<CustomerDoc> getCustomers() {
return customerService.getCustomers();
}
@RequestMapping(
value = "createCustomer",
method = RequestMethod.POST,
consumes = "application/json")
@ResponseBody
public String createCustomer(@RequestBody CustomerDoc doc) {
customerService.storeUser(doc);
return "Added Customer!";
}
@RequestMapping(value = "deleteCustomer", method = RequestMethod.DELETE)
@ResponseBody
public void deleteCustomer(@RequestParam String id) {
customerService.deleteCustomer(id);
}
}
An application class has been added for the purpose of starting the application:
package com.masterspringboot.solrdemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
That’s all. In terms of dependency, you need to add the following one, in order to be able to build the example:
<?xml version="1.0" encoding="UTF-8"?><project>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-solr</artifactId>
</dependency>
</project>
You can run the application with:
mvn install spring-boot:run
Then you can add a Customer using the POST method of the Controller:
curl -d '{"id":"1", "username":"John Smith", "address":"Park Avenue", "phoneNumber":"1234567"}' -H "Content-Type: application/json" -X POST http://localhost:8080/createCustomer
You can check the customer list as follows:
curl http://localhost:8080/getCustomers
Which will return:
[ { "id": "1", "username": "John Smith", "address": "Park Avenue", "phoneNumber": "1234567" } ]
You can check Solr management console of by browsing into its Admin Console at: http://localhost:8983/

And here is your Document, contained in the “customer_core”:

Congrats! You have just managed to set up Solr and deployed some Documents in it using a Spring Boot application.
Source code available here: https://github.com/fmarchioni/masterspringboot/tree/master/solr/demo-solr
Found the article helpful? if so please follow us on Socials