Solr With Scala : Basic Introduction to Embedded Solr


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 :
    solr
      |-test_embedded //name of configuration folder
             |-conf
                 |-lang
                 |-currency.xml
                 |-elevate.xml         
                 |-managed-schema
                 |-param-.json
                 |-protword.txt
                 |-solrconfig.xml
                 |-stopwords.txt
                 |-synonyms.txt
             |-data (Optional if not created than it will create automatically)
             |-core.properties
      |-solr.xml
    
  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()
     container.load()
     server = new EmbeddedSolrServer(container, "test_embedded")
    }
    
    test("test embedded solr"){
      val solrInputDocument = new SolrInputDocument()
      solrInputDocument.addField("id", 1)
      solrInputDocument.addField("name", "test")
      server.add("test_embedded",solrInputDocument)
    
      val parameter = new SolrQuery()
      parameter.set("q", "*:*")
      val result = server.query("test_embedded",parameter)
      val docList = result.getResults()
      val doc = docList.get(0)
      assert(doc.getFirstValue("name").toString().toLowerCase().contains("test"));
    }
    
    override def afterAll(): Unit = {
     server.close
    }

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

KNOLDUS-advt-sticker

This entry was posted in akka-http, Scala, scalatest, testing, tests and tagged , , , , , . Bookmark the permalink.

2 Responses to Solr With Scala : Basic Introduction to Embedded Solr

  1. Pingback: Solr With Scala : Basic Introduction to Embedded Solr | Ace Infoway

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s