Solr With Scala : Basic Introduction to Embedded Solr

Table of contents
Reading Time: 2 minutes

Hello Folks,

As we discussed earlier Solr with Scala and AkkaHttp in our last blog we have used Solr as a service and tried to hit solr service with the AkkaHttp and spray routes. Here is the link for the Recap.

When we want to do testing for those methods which are creating connection with the remote solr server, have to use Embedded Solr for that.

What is Embedded Solr?

Embedded Solr same interface as Solr without requiring an HTTP connection. When we “embed” Solr into a Java an application, It provides the exact same API as you would use if you were connecting to a remote Solr instance. We can use embedded solr for the in memory testing because when we implement test cases, it should not be depend on any external resources.

How can we use Embedded Solr?

When we wish to use Embedded Solr we have to follow some basic steps for creating an interface with embedded solr.

  1. Add Solr-Core dependency.
  2. Here is a predefined directory structure, follow them :
      |-test_embedded //name of configuration folder
             |-data (Optional if not created than it will create automatically)
  3. Now we are ready to start embedded solr in test set-up and will shut down it after complete testing.
    override def before() = {
     val container = new CoreContainer()
     server = new EmbeddedSolrServer(container, "test_embedded")
    test("test embedded solr"){
      val solrInputDocument = new SolrInputDocument()
      solrInputDocument.addField("id", 1)
      solrInputDocument.addField("name", "test")
      val parameter = new SolrQuery()
      parameter.set("q", "*:*")
      val result = server.query("test_embedded",parameter)
      val docList = result.getResults()
      val doc = docList.get(0)
    override def afterAll(): Unit = {

This is a basic code example for testing the embedded solr. If test case passes successfully, we are ready to do testing for the services.
For more example and code please refer Knoldus Github.
If You have any questions you can contact me here or on Twitter: @anuragknoldus


Written by 

Anurag is the Sr. Software Consultant @ Knoldus Software LLP. In his 3 years of experience, he has become the developer with proven experience in architecting and developing web applications.

2 thoughts on “Solr With Scala : Basic Introduction to Embedded Solr2 min read

Comments are closed.