SOLR With Scala: Basic Introduction to Embedded SOLR

综合技术 2017-04-11

SOLR With Scala: Basic Introduction to Embedded SOLR

DZone's Guide to

SOLR With Scala: Basic Introduction to Embedded SOLR

If you want to test methods that are creating connections with a remote Solr server, then Embedded Solr is the way to go.

Free Resource

Join the DZone community and get the full member experience.

Need to build an application around your data? Learn more about dataflow programming for rapid development and greater creativity.

As we discussed in our previous article about Solr with Scala and AkkaHttp , we have used Solr as a service and tried to hit the Solr service with AkkaHttp and spray routes.

When we want to test methods that are creating connections with a remote Solr server, we have to use Embedded Solr.

What Is Embedded Solr?

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

How to Use Embedded Solr

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

  1. Add the Solr-Core dependency.
  2. Follow this predefined directory structure:
      |-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 the test set-up. It will shut down it after completes 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 Embedded Solr. If test case passes successfully, we are ready to test the services. For more examples and code, please refer to the Knoldus GitHub page .

Check out the Exaptive data application Studio. Technology agnostic. No glue code. Use what you know and rely on the community for what you don't. Try the community version .


责编内容by:DZone (源链)。感谢您的支持!


搜索引擎选择: Elasticsearch与Solr 搜索引擎选型调研文档 Elasticsearch简介 * Elasticsearch是一个 实时的 分布式搜索和分析引擎。它可以帮助...
Solr and Zookeeper in Production This blog helps you in setting up SolrCloud with a Zookeeper ensemble, assuming ...
Synchronous or Asynchronous, and Why Wrestle With ... Have you ever wondered why you have to wrestle with CompletableFutures inste...
快速上手写spark代码系列:01-RDD transformation函数入门... 快速上手写spark代码系列:01-RDD transformation函数入门 标签: RDD transformation 函数...
Scala Design Success: Generics Scala’s Generics reside in a language design sweet-spot largely due to three, in...